将64位Steam ID转换为32位帐户ID

时间:2014-04-24 03:26:02

标签: javascript node.js steam

如何将64位蒸汽ID转换为32位帐户ID? Steam说取这个数字的前32位,但是你如何在Node中做到这一点?

我是否需要使用BigNumber来存储64位int?

3 个答案:

答案 0 :(得分:4)

要将64位Steam ID转换为32位帐户ID,您只需从64位ID中减去76561197960265728

这需要节点中的bigNumber:

bignumber = require("bignumber.js");
console.log(bignumber('76561197991791363').minus('76561197960265728'))

答案 1 :(得分:1)

这就是我想出的。我昨天开始学习JavaScript(来自C ++背景,不习惯没有类型的工作),所以如果我用语言做了一些derpy,请纠正我。我用我自己的蒸汽ID测试它似乎工作。

// NOTE: Functions can take in a steamID in its packed 64-bit form 
// (community ID starting with 765), its modern form with or without 
// either or both brackets, and its legacy form. SteamID's that 
// contain letters (e.g. STEAM_0... or [U:1...) are not case-sensitive.

// Dependencies: BigInteger library, available from http://silentmatt.com/biginteger/

// Global variable used by all conversion functions
var STEAM_BASELINE = '76561197960265728';

// IN: String containing a steamID in any of the 3 formats
// OUT: String containing the steamID as a community ID (64-bit packed ID)
function ConvertToPacked(inputID)
{
    var output = "unknown";

    // From packed
    if(inputID.match(/^765/) && inputID.length > 15)
    {
        output = inputID;
    }

    // From modern
    else if(inputID.match(/^\[U:1:/i) || inputID.match(/^U:1:/i))
    {
        var numericPortion = inputID.replace(/^\[U:1:|^U:1:/i,'').replace(/\]/,'');
        output = BigInteger.add(numericPortion, STEAM_BASELINE).toString();
    }

    // From legacy
    else if(inputID.match(/^STEAM_0:[0-1]:/i))
    {
        var splitID = inputID.split(":");
        var product = BigInteger.multiply(splitID[2],2);
        var sum = BigInteger.add(product, STEAM_BASELINE);
        output = BigInteger.add(sum, splitID[1]).toString();
    }

    return output;
}

// IN: String containing a steamID in any of the 3 formats
// OUT: String containing the steamID in the modern format (e.g. [U:1:123456])
function ConvertToModern(inputID)
{
    var output = "unknown";

    // From packed
    if(inputID.match(/^765/) && inputID.length > 15)
    {
        output = "[U:1:" + BigInteger.subtract(inputID, STEAM_BASELINE).toString() + "]";
    }

    // From modern
    else if(inputID.match(/^\[U:1:/i) || inputID.match(/^U:1:/i))
    {
        var numericPortion = inputID.replace(/^\[U:1:|^U:1:/i,'').replace(/\]/,'');
        output = "[U:1:" + numericPortion + "]";
    }

    // From legacy
    else if(inputID.match(/^STEAM_0:[0-1]:/i))
    {
        var splitID = inputID.split(":");
        var numeric = BigInteger.add(BigInteger.multiply(splitID[2],2), splitID[1]);
        output = "[U:1:" + numeric.toString() + "]";        
    }

    return output;
}

// IN: String containing a steamID in any of the 3 formats
// OUT: String containing the steamID in the legacy format (e.g. STEAM_0:0:123456)
function ConvertToLegacy(inputID)
{
    var output = "unknown"

    // From packed
    if(inputID.match(/^765/) && inputID.length > 15)
    {        
        var z = BigInteger.divide(BigInteger.subtract(inputID, STEAM_BASELINE), 2);
        var y = BigInteger.remainder(inputID, 2);
        output = 'STEAM_0:' + y.toString() + ':' + z.toString();
    }
    // From modern
    else if(inputID.match(/^\[U:1:/i) || inputID.match(/^U:1:/i))
    {
        var numericPortion = inputID.replace(/^\[U:1:|^U:1:/i,'').replace(/\]/,'');
        var z = BigInteger.divide(numericPortion, 2);
        var y = BigInteger.remainder(numericPortion, 2);
        output = 'STEAM_0:' + y.toString() + ':' + z.toString();
    }
    // From legacy
    else if(inputID.match(/^STEAM_0:[0-1]:/i))
    {
        output = inputID.toUpperCase();
    }

    return output;
}

答案 2 :(得分:0)

我遇到了同样的问题,但是由于我的项目很小,并且不想在Web浏览器中使用,所以不想使用bignumber.js之类的任何库。最后,我想出了一个优雅的解决方案:

function steamID64toSteamID32 (steamID64) {
    return Number(steamID64.substr(-16,16)) - 6561197960265728
}

工作方式:

要获取低32位,我们需要将SteamID64字符串转换为数字,但是由于JavaScript的精度限制为57位,因此SteamID64将被错误舍入。解决方法是截断最左边的数字以获得16位数字,该数字最多使用54位,因此将在Javascript中保留其精度。这是可以接受的,因为最左边的数字来自高32位,无论如何它们将被清零,因此不会丢失任何值。

要将其余的较高位清零,我们减去它们所代表的十进制数。如果我们假设要转换的每个SteamID64都位于public universe中,则该十进制数将是恒定的,并且可以这样计算:

1. 0b00000001000100000000000000000001 0b00000000000000000000000000000000 = 76561197960265728
2. Number('76561197960265728'.substr(-16,16)) = 6561197960265728