Skip to content

Instantly share code, notes, and snippets.

@bcahue
Created September 24, 2014 15:04
Show Gist options
  • Save bcahue/4eae86ae1d10364bb66d to your computer and use it in GitHub Desktop.
Save bcahue/4eae86ae1d10364bb66d to your computer and use it in GitHub Desktop.
Converting New SteamID Formats in Python

Seeing how SteamIDs have changed recently, I thought I'd write a Gist to help others (including myself!) decypher how to convert these from one format to the other in Python. First, let's go over basics to convert a 64bit CommunityID into a SteamID:

steamid64ident = 76561197960265728

def commid_to_steamid(commid):
  steamid = []
  steamid.append('STEAM_0:')
  steamidacct = int(commid) - steamid64ident
  
  if steamidacct % 2 == 0:
      steamid.append('0:')
  else:
      steamid.append('1:')
  
  steamid.append(str(steamidacct // 2))
  
  return ''.join(steamid)


>>> commid_to_steamid(76561197994100486)
'STEAM_0:0:16917379'

Remember that Python's // operator is for floor division, so the answer is always rounded down.

And we can do the same in reverse, a SteamID turned into a CommunityID

def steamid_to_commid(steamid):
  sid_split = steamid.split(':')
  commid = int(sid_split[2]) * 2
  
  if sid_split[1] == '1':
      commid += 1
  
  commid += steamid64ident
  return commid


>>> steamid_to_commid('STEAM_0:0:16917379')
76561197994100486

The if statement that assigns the parity bit to the account number is a bit of a cheat here. Since the SteamID is a string, you don't have to use modulus to determine if you need to add the parity bit or not.

Now onto the new SteamID3 format. The formula to convert old SteamIDs to the new SteamID3 format is this: STEAM_X:Y:Z -> [U:1:(Z * 2 + Y)] Be sure to note the brackets [ and ], as they're considered part of the SteamID3 format.

Here's how to convert a SteamID to SteamID3:

def steamid_to_usteamid(steamid):
  steamid_split = steamid.split(':')
  usteamid = []
  usteamid.append('[U:1:')
  
  y = int(steamid_split[1])
  z = int(steamid_split[2])
  
  steamacct = z * 2 + y
  
  usteamid.append(str(steamacct) + ']')
  
  return ''.join(usteamid)


>>> steamid_to_usteamid('STEAM_0:0:16917379')
'[U:1:33834758]'

We simply subtract the SteamID64Identifier from the CommunityID to get the SteamID3 account number. Here's the code:

def commid_to_usteamid(commid):
    usteamid = []
    usteamid.append('[U:1:')
    steamidacct = int(commid) - steamid64ident
    
    usteamid.append(str(steamidacct) + ']')
    
    return ''.join(usteamid)


>>> commid_to_usteamid(76561197994100486)
'[U:1:33834758]'

Finally, I'll show how to convert the SteamID3 into CommunityIDs and SteamIDs. First, here's how to convert SteamID3 to a SteamID:

def usteamid_to_steamid(usteamid):
  for ch in ['[', ']']:
    if ch in usteamid:
      usteamid = usteamid.replace(ch, '')
  
  usteamid_split = usteamid.split(':')
  steamid = []
  steamid.append('STEAM_0:')
  
  z = int(usteamid_split[2])
  
  if z % 2 == 0:
      steamid.append('0:')
  else:
      steamid.append('1:')

  steamacct = z // 2
  
  steamid.append(str(steamacct))
  
  return ''.join(steamid)


>>> usteamid_to_steamid('[U:1:33834758]')
'STEAM_0:0:16917379'

The for loop is used to get rid of the brackets if they exist since they'll mess up our integer calculations. This could be simpified with re but I wanted to do this without importing anything.

Last but not least, here's how to convert a SteamID3 into a CommunityID:

def usteamid_to_commid(usteamid):
  for ch in ['[', ']']:
    if ch in usteamid:
      usteamid = usteamid.replace(ch, '')
  
  usteamid_split = usteamid.split(':')
  commid = int(usteamid_split[2]) + steamid64ident
  
  return commid


>>> usteamid_to_commid('[U:1:33834758]')
76561197994100486

Luckily this one is very easy, since USteamID account numbers already include the parity bit if required. All that's needed is to sanitize the input.

Read more about SteamIDs here.

@bcahue
Copy link
Author

bcahue commented Jun 22, 2015

Sorry this is a bit late, this gist doesn't notify me about comments.

The reason your conversion of SteamID3 to CommunityID was failing was because the math was a little off. To get the CommunityID from a SteamID3, you simply add the amount past the last colon of the SteamID3, which you correctly identified as '33834758'. You then add it to the Steam Identifier, which is a constant number used to calculate SteamIDs. Again, correctly you identified this as '76561197960265728'.

The addition is where this went wrong. 33834758 + 76561197960265728 is not 7656119829861330, but it's 76561197994100486.

@WardPearce
Copy link

Thanks for this script! Used it for https://github.com/WardPearce/dathost, saved me a lot of time.

@homus32
Copy link

homus32 commented Jul 29, 2021

ля, просто время сэкономил, спасибо.

@pinheadtf2
Copy link

Making a project involving steam ids and couldn't figure this out for the life of me, thank you for the guide and thoughtful explanations with examples!!

@s4dstorm
Copy link

s4dstorm commented Jan 7, 2024

instead of
for ch in ['[', ']']:
if ch in usteamid:
usteamid = usteamid.replace(ch, '')
usteamid_split = usteamid.split(':')

why not just
usteamid_split = usteamid.replace('[','').replace(']','').split(':')
?
Anyway, thank you very much

@aligatorr89
Copy link

Nice

@secoldPrivate
Copy link

secoldPrivate commented Feb 1, 2024

Based and steam pilled.
Here is a javascript implementation:


function steamidToSteamID3(steamid) {
    const steamidSplit = steamid.split(':');
    const y = parseInt(steamidSplit[1], 10);
    const z = parseInt(steamidSplit[2], 10);

    const steamacct = z * 2 + y;

    return `[U:1:${steamacct}]`;
}

// Example usage
console.log(steamidToSteamID3('STEAM_0:0:16917379'));

@aligatorr89
Copy link

aligatorr89 commented Feb 1, 2024

@WAZAAAAA0
Copy link

WAZAAAAA0 commented Nov 10, 2024

To anyone looking for how to convert the short lobby ID format to its long ID version, you need a different magic number. None of the Steam ID lookup websites around seem to interpret it right.
Some real examples first:

[L:1:1582082652] = 109775242499238492
[L:1:1763714810] = 109775242680870650

so the formula is:

magic number + m_ulSteamIDLobby short ID = long ID
109775240917155840 + 1582082652 = 109775242499238492
details
  • short ID comes from steam://open/console -> log_matchmaking_callbacks 1 -> create or join a lobby -> copy the m_ulSteamIDLobby value
  • long ID comes from making an in-game lobby that supports invites -> navigating to your own non-offline/non-invisible Steam profile -> right clicking Join Game -> Copy Link Address (example: steam://joinlobby/<gameID>/109775242499238492/<userID>)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment