繁体   English   中英

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

[英]Convert 64 bit Steam ID to 32 bit account ID

如何将64位Steam ID转换为32位账户ID? Steam表示采用数字的前32位,但是您如何在Node中做到这一点?

我需要使用BigNumber存储64位int吗?

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

这需要节点中的bigNumber:

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

这是我想出的。 我昨天开始学习JavaScript(来自C ++背景,不是非常习惯没有类型的工作),所以如果我对这种语言做得很卑鄙,请改正我。 我用自己的Steam 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;
}

我遇到了同样的问题,但是不想使用任何像bignumber.js这样的库,因为我的项目很小,将在Web浏览器中使用。 最后,我想出了一个优雅的解决方案:

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

这个怎么运作:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM