0

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.

Cludas18
  • 65
  • 3
  • 12
  • 1
    possible duplicate of [Steam: Convert SteamID64 to SteamID using PHP](http://stackoverflow.com/questions/28516932/steam-convert-steamid64-to-steamid-using-php) – LukeH Jul 07 '15 at 14:48
  • Didn't see that post, but I found some useful information there. – Cludas18 Jul 07 '15 at 15:56

3 Answers3

7

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;
LukeH
  • 263,068
  • 57
  • 365
  • 409
1
function steamid64_to_steamid2($steamid64) {
    $accountID = bcsub($steamid64, '76561197960265728');
    return 'STEAM_0:'.bcmod($accountID, '2').':'.bcdiv($accountID, 2);
}
ilia
  • 19
  • 1
  • 7
    Welcome to Stack Overflow! While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Filnor Dec 13 '17 at 09:32
0
<?php 

$steamid64="76561198237914532"; //YOUR STEAM ID 64

echo "<-- By BigBossPT to VynexGaming.com -->";
echo "<br><br>Steamid32: ".getSteamId32($steamid64);
echo "<br><br>Steamid64: ".getSteamID64(getSteamId32($steamid64)); // 76561197985756607 
echo "<br><br>Thanks for Gio! Website that i found: https://facepunch.com/showthread.php?t=1238157";
//OBTER STEAM ID 64 

function getSteamID64($id) {
    if (preg_match('/^STEAM_/', $id)) {
        $parts = explode(':', $id);
        return bcadd(bcadd(bcmul($parts[2], '2'), '76561197960265728'), $parts[1]);
    } elseif (is_numeric($id) && strlen($id) < 16) {
        return bcadd($id, '76561197960265728');
    } else {
        return $id; // We have no idea what this is, so just return it.
    }
}


function parseInt($string) {
    //    return intval($string);
        if(preg_match('/(\d+)/', $string, $array)) {
            return $array[1];
        } else {
            return 0;
        }
    }
function getSteamId32($id){
    // Convert SteamID64 into SteamID

    $subid = substr($id, 4); 
    $steamY = parseInt($subid);
    $steamY = $steamY - 1197960265728; //76561197960265728

    if ($steamY%2 == 1){
    $steamX = 1;
    } else {
    $steamX = 0;
    }

    $steamY = (($steamY - $steamX) / 2);
    $steamID = "STEAM_0:" . (string)$steamX . ":" . (string)$steamY;
    return $steamID;

}
?>