0

I'm trying to convert Steam ID 32bit to Steam ID 64bit.

My javascript function is not working right but same function in python works fine. Python function is copied from How can I get a steamid 64 from a steamID in python

function steamidTo64(steamid) {
    let steam64id = 76561197960265728; // Valve's magic constant
    let id_split = steamid.split(":");
    steam64id += parseInt(id_split[2]) * 2;
    if (id_split[1] == "1") {
        steam64id += 1;
    }
    return steam64id;
}

Using input of STEAM_1:1:191000236 function should return 76561198342266201, but instead it returns 76561198342266200

Using input of STEAM_1:1:3645504 function should return 76561197967556737, but it returns 76561197967556740

Using input of STEAM_0:0:570629725 function should return 76561199101525178, but it returns 76561199101525180

  • 1
    Numeric literals with absolute values equal to 2^53 or greater are too large to be represented accurately as integers. – kelsny Nov 18 '22 at 20:53

1 Answers1

0

You will need to use BigInt since 76561197960265728 is greater than 9007199254740992 (1 digit longer), and so arithmetic operations on this number are inaccurate and can lead to incorrect results.

function steamidTo64(steamid) {
    let steam64id = 76561197960265728n;
    
    const id_split = steamid.split(":");

    steam64id += BigInt(id_split[2]) * 2n;

    if (id_split[1] === "1") steam64id += 1n;

    return steam64id;
}

// Result is BigInt
console.log(steamidTo64("STEAM_1:1:191000236").toString());
kelsny
  • 23,009
  • 3
  • 19
  • 48