Cludas18
Cludas18

Reputation: 55

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.

Upvotes: 0

Views: 10735

Answers (3)

Marco Vieira
Marco Vieira

Reputation: 21

<?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;

}
?>

Upvotes: 0

ilia
ilia

Reputation: 19

function steamid64_to_steamid2($steamid64) {
    $accountID = bcsub($steamid64, '76561197960265728');
    return 'STEAM_0:'.bcmod($accountID, '2').':'.bcdiv($accountID, 2);
}

Upvotes: 1

LukeH
LukeH

Reputation: 269498

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;

Upvotes: 7

Related Questions