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.
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.