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.

@homus32
Copy link

homus32 commented Jul 29, 2021

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

@pinhead-tf2
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

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