Javascript 将64位Steam ID转换为32位帐户ID
如何将64位steam ID转换为32位帐户ID?Steam说获取数字的前32位,但如何在节点中实现这一点Javascript 将64位Steam ID转换为32位帐户ID,javascript,node.js,steam,Javascript,Node.js,Steam,如何将64位steam ID转换为32位帐户ID?Steam说获取数字的前32位,但如何在节点中实现这一点 我是否需要使用BigNumber来存储64位整数?要将64位Steam ID转换为32位帐户ID,只需从64位ID中减去76561197960265728 这需要节点中的bigNumber: bignumber = require("bignumber.js"); console.log(bignumber('76561197991791363').minus('7656119796026
我是否需要使用BigNumber来存储64位整数?要将64位Steam ID转换为32位帐户ID,只需从64位ID中减去
76561197960265728
这需要节点中的bigNumber:
bignumber = require("bignumber.js");
console.log(bignumber('76561197991791363').minus('76561197960265728'))
要将64位Steam ID转换为32位帐户ID,只需从64位ID中减去
76561197960265728
这需要节点中的bigNumber:
bignumber = require("bignumber.js");
console.log(bignumber('76561197991791363').minus('76561197960265728'))
这是我想到的。我昨天开始学习JavaScript(来自C++背景,不习惯于没有类型的工作),所以如果我用语言做了一些DePy,那么请纠正我。我用我自己的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;
}
这是我想到的。我昨天开始学习JavaScript(来自C++背景,不习惯于没有类型的工作),所以如果我用语言做了一些DePy,那么请纠正我。我用我自己的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都是十进制数,那么这个十进制数将是常量,并且可以这样计算:
1. 0b00000001000100000000000000000001 0b00000000000000000000000000000000 = 76561197960265728
2. Number('76561197960265728'.substr(-16,16)) = 6561197960265728
我也有同样的问题,但不想使用任何像bignumber.js这样的库,因为我的项目非常小,将在web浏览器中使用。最后,我提出了一个优雅的解决方案:
function steamID64toSteamID32 (steamID64) {
return Number(steamID64.substr(-16,16)) - 6561197960265728
}
工作原理: 要获得较低的32位,我们需要将SteamID64字符串转换为数字,但由于JavaScript的精度限制为57位,因此SteamID64将被错误地四舍五入。解决方法是截断最左边的数字以得到一个16位数字,该数字最多使用54位,因此在Javascript中将保持其精度。这是可以接受的,因为最左边的数字来自较高的32位,无论如何都会归零,因此不会丢失任何有价值的数据 为了将剩余的高位归零,我们减去它们所代表的十进制数。如果我们假设我们要转换成的每个SteamID64都是十进制数,那么这个十进制数将是常量,并且可以这样计算:
1. 0b00000001000100000000000000000001 0b00000000000000000000000000000000 = 76561197960265728
2. Number('76561197960265728'.substr(-16,16)) = 6561197960265728
是的,您确实需要使用BigNumber库。Javascript
number
s都是64位IEEE浮点数。注意在Javascript中76561197991791363===76561197991791360
。@YTowOnt9我刚刚注意到了这一点。为什么最后一个3
会被特别选中?这个数字真的是一位数太大了吗?它是关于双精度的内部表示,类似于0.1+0.2!==0.3
。数字存储在您从何处获得15762817
?除非我搞砸了,否则第一组或第二组位都不等于15762817。是的,你确实需要使用一个大数字库。Javascriptnumber
s都是64位IEEE浮点数。注意在Javascript中76561197991791363===76561197991791360
。@YTowOnt9我刚刚注意到了这一点。为什么最后一个3
会被特别选中?这个数字真的是一位数太大了吗?它是关于双精度的内部表示,类似于0.1+0.2!==0.3
。数字存储在您从何处获得15762817
?除非我搞砸了,否则第一组或第二组位都不等于15762817。node-dota2有一些这种功能,我认为有一些不好的信息,这个答案是完美的-我已经浪费了大约5个小时尝试各种算法,包括阀门文档的位移位DE-dota2有一些这种类型的功能,我认为有一些不好的信息,这个答案是完美的-我已经浪费了大约5个小时尝试各种算法,包括阀门文档的位移位