Search code examples
phpsteamsteam-web-api

Convert SteamID64 to SteamID


I'm looking for a way that I can take a SteamID64 (76561198032122624) and convert it to a SteamID (STEAM_0:0:35928448) in PHP. I've searched this quite a bit and I'm unable to find how to do this. I'm almost sure it's possible since sites like steamid.io are able to find it, but I don't know how.


Solution

  • All the info you need is on Valve's SteamID wiki page:

    Legacy Format

    Steam IDs follow a fairly simple format when represented textually: "STEAM_X:Y:Z", where X, Y and Z are integers.

    • X represents the "Universe" the steam account belongs to. If 'X' is 0, then this is Universe 1 (Public).
    • Y is the lowest bit of the Account ID. Thus, Y is either 0 or 1.
    • Z is the highest 31 bits of the Account ID.

    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)

    My PHP is very rusty, but here's some (untested) pseudocode that should do roughly what's required:

    var steamId64 = 76561198032122624;
    
    var universe = (steamId64 >> 56) & 0xFF;
    if (universe == 1) universe = 0;
    
    var accountIdLowBit = steamId64 & 1;
    
    var accountIdHighBits = (steamId64 >> 1) & 0x7FFFFFF;
    
    // should hopefully produce "STEAM_0:0:35928448"
    var legacySteamId = "STEAM_" + universe + ":" + accountIdLowBit + ":" + accountIdHighBits;