3

I have a list of steam IDS that look like this:

STEAM_1:0:61759597
STEAM_1:1:20263946
STEAM_1:0:105065707

For all the calls to the steam API you need to give it a steamID64, those look like this:

76561198083784922
76561198000793621
76561198170397142

The steam page shows an example of how to convert from SteamID to SteamID64 https://developer.valvesoftware.com/wiki/SteamID

As a 64-bit integer
Given the components of a Steam ID, a Steam ID can be converted to it's 64-bit integer form as follows: 
((Universe << 56) | (Account Type << 52) | (Instance << 32) | Account ID)
Worked Example:
Universe: Public (1)
Account Type: Clan (7)
Instance: 0
Account ID: 4
64-bit integer value: 103582791429521412

My question is how do I implement this in python. I don't understand anything that going on here.

To make my question super clear I want to start with STEAM_1:0:61759597 from that resolve the SteamID64 which is 76561198083784922

I know this is possible because there are a number of websites that do this: https://steamid.io/, http://steamidfinder.com/, https://steamid.co/

So many question is: what does this algorithm do and how do I implement it in python?

UPDATE

This is the code i have now, not working as expected:

steamID = "STEAM_1:0:61759597"
X = int(steamID[6:7])
Y = int(steamID[8:9])
Z = int(steamID[10:])

#x is 1
#y is 0
#z is 61759597

print(X,Y,Z)

print((X << 56) | (Y << 52) | (Z << 32) | 4)
#current output: 265255449329139716
#desired output: 76561198083784922
Keatinge
  • 4,330
  • 6
  • 25
  • 44

2 Answers2

3

There is an explanation in a steam forum post of how to convert, we only care about the last two numbers:

ID3 is therefore the offset from the ID64 base number (76561197960265728), and is the account number. First ever made account: (its 76561197960265728 + 1, 76561197960265728 does not exist)

    ID64 = 76561197960265728 + (B * 2) + A
    ID3 = (B * 2) + A
    ID32 = STEAM_0:A:B

So all you need is:

def to_steam64(s):
    return ((b * 2) + a) + 76561197960265728

To go the reverse route, from steam64:

def from_steam64(sid):
    y = int(sid) - 76561197960265728
    x = y % 2 
    return "STEAM_0:{}:{}".format(x, (y - x) // 2)

The is a conversion table here

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
1

The implementation is actually exactly the same as shown on the Steam wiki:

>>> (1 << 56) | (7 << 52) | (0 << 32) | 4
103582791429521412

<< and | are bitwise operators, performing a left shift and a bitwise OR, respectively. You can learn a lot more about bitwise operations on Wikipedia.

As far as translating arbitrary Steam IDs to the 64-bit system, I found this gist:

def steamid_to_64bit(steamid):
    steam64id = 76561197960265728 # I honestly don't know where
                                    # this came from, but it works...
    id_split = steamid.split(":")
    steam64id += int(id_split[2]) * 2 # again, not sure why multiplying by 2...
    if id_split[1] == "1":
        steam64id += 1
    return steam64id

In [14]: steamid_to_64bit("STEAM_1:0:61759597")
Out[14]: 76561198083784922

In [15]: steamid_to_64bit("STEAM_1:1:20263946")
Out[15]: 76561198000793621

In [16]: steamid_to_64bit("STEAM_1:0:105065707")
Out[16]: 76561198170397142
MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Thanks for the response, that code is for one specific example right? How do I implement it for any steamID like `STEAM_1:0:61759597` – Keatinge Apr 06 '16 at 23:10
  • 1
    @Racialz no, I believe you. I guess it was just a random drive-by... At any rate, check out my edit. – MattDMo Apr 06 '16 at 23:21
  • This looks like it working, just going to do a few more tests to ensure and then i'll mark it as the answer. As for that seemingly arbitrary number, I was seeing that a lot too when I was trying to solve this on my own before I asked on here. Just edit the line `if id_split[1] = "1":` to replace the = with == to help other people with the same problem in the future – Keatinge Apr 06 '16 at 23:25
  • I think the formula is for modern formats, the OP's are legacy. – Padraic Cunningham Apr 06 '16 at 23:26
  • @Racialz yes, I just caught that myself. I also renamed some variables. The present version should work fine. – MattDMo Apr 06 '16 at 23:26
  • Okay it's marked as the answer, I'm just so surprised that something that I assumed would be common since the steam api only accepts steamid64s would have such a cryptic and weird solution. Thanks for the help – Keatinge Apr 06 '16 at 23:32
  • 1
    @Racialz same here. I can understand the bit-shifting, as that's done quite frequently in C and C++, which is probably what Steam is written in, but I honestly don't understand the Python implementation, and I've been programming in Python for a long time. Some more research is needed, for sure. I also posted my results for your other two IDs in my answer, so you can double-check them against yours. – MattDMo Apr 06 '16 at 23:35