Conversion to 64 bit int

2019-03-01 05:42发布

问题:

So I am working in PHP to convert a Steam Id, which many of you may be familiar with. I have the following steam ID:

STEAM_0:1:1514332

Now, I need to convert this to the 64 bit version, which is also the community ID. After browsing Steams official release on this here: http://developer.valvesoftware.com/wiki/SteamID and after also looking many places online, I have found the following method works for this:

Let X,Y, and Z be defined by the Steam ID: STEAM_X:Y:Z
SteamCommunityID = (Z*2) + 76561197960265728 + Y

So it works! However, where seems to be a mismatch between my ACTUAL community ID, and the one I am generating

Actual:        76561197963294393
PHP generated: 76561197963294000

When reversing the equasion, to get my steam id from the community id, I get: 1514335.5

Here is a simple example of the php in question:

//STEAM_0:1:1514332

$a = (1514332 * 2) + 76561197960265728 + 1;
echo $a; //76561197963294000

Am I doing something wrong?

回答1:

PHP don't have 64bit int on 32-bit binary. It is using floating point here. see how to have 64 bit integer on PHP?

The question include links to BC Math, which can be used for your problem.



回答2:

Personally, I use the following code and it works perfectly for splitting the Steam ID and working out the 64-bit community ID:

$split = explode(":", $steamid); // STEAM_?:?:??????? format

$x = substr($split[0], 6, 1);
$y = $split[1];
$z = $split[2];

$w = ($z * 2) + 0x0110000100000000 + $y;

$w will be the correctly formatted ID. I thought this may be helpful since I found this trying to do the same thing :)



回答3:

I have had the issue. I'm using xampp on windows and its using the 32bit version of php but i just finished making a working script :) it can convert 64bit steamid's to 32bit and back have a mess with it.

<?php
//Starting 64bit steamid
$steamid = "76561198086186258";
echo "Original 64bit steamid: $steamid";

//64bit steamid to 32bit below
$authserver = bcsub($steamid, '76561197960265728') & 1;
$authid = (bcsub($steamid, '76561197960265728')-$authserver)/2;
$steamid = "STEAM_0:$authserver:$authid";
echo "<br/>32bit steamid: $steamid";

//32bit steamid to 64bit below
$id = explode(":", $steamid);
$authserver = $id[1];
$steamid64 = $id[2];
$steamid64 = $steamid64 * 2;
$steamid64 = bcadd($steamid64, 61197960265728);
if ($authserver == 1){$steamid64 = $steamid64 + 1;};
$steamid64 = "765$steamid64";
echo "<br/>new 64bit steamid: $steamid64";
?>


回答4:

try to add (string) before these calculations

$a = (string) (1514332 * 2) + 76561197960265728 + 1;

and it will work always;

demo

response: 76561197963294393



回答5:

Using the BCMath extension, you can do the following:

bcadd(bcadd(bcmul('1514332', '2'), '76561197960265728'), 1)

Which is a conversion of the equation above. It returns a string and not an int, thus avoiding the 32bit int problem.



回答6:

You are not doing anything wrong. The formula is correct (using a calculator I get the 'actual' number).

There must be either a rounding or calculating issue or a simple OS limit for it.



回答7:

You may use the BC Math PHP extension. Here is a snippet:

//STEAM_0:1:1514332

$a = bcadd(bcadd((1514332 * 2), 76561197960265728), 1);
echo $a; //76561197963294393


回答8:

I know that's very old question, but will still answer (in your particular context, not just how to use 64-bit integers).

Don't add 76561197960265728. Just multiply the 3rd part of SteamID by 2, and add 2nd part to that. Then access the community page using the URL http://steamcommunity.com/profiles/[U:1:result of the expression]. In your case, it will be http://steamcommunity.com/profiles/[U:1:3028665].



回答9:

Very old topic but just wanted to share how I solved it since I had 32bit PHP and no possibility of using BCMath on my host. (thanks SiPlus for solution)

Here's a snippet:

# Convert Steam ID to Steam64 ID
$idParts = explode(':', $tradeOwnerSteamId32);
$authServer = intval($idParts[1]);
$accountNumber = intval($idParts[2]);
$tradeOwnerSteamId64 = (string) $accountNumber * 2 + $authServer;

//And using it in URL

$depositorInventory = json_decode(file_get_contents("https://steamcommunity.com/profiles/[U:1:$tradeOwnerSteamId64]/inventory/json/730/2"), true);



标签: php 64bit