Forum

> > CS2D > Scripts > Handle SteamIDs Easily in Lua
Forums overviewCS2D overview Scripts overviewLog in to reply

English Handle SteamIDs Easily in Lua

4 replies
To the start Previous 1 Next To the start

old Handle SteamIDs Easily in Lua

Hajt
User Off Offline

Quote
Lua doesn't support 64-bit integers, so SteamID64 strings are really annoying to work with.

I just convert them to SteamID32 - it fits in a native Lua integer (4 bytes) and is way easier to handle, example:

1
2
3
4
5
function SteamID64To32(s) 
    return tonumber(s) - 76561197960265728 
end

print(SteamID64To32("76561197960287930") )

Devs probably won't switch SteamID64 strings to 32-bit integers, so converting in Lua is usually the easiest way.

https://developer.valvesoftware.com/wiki/SteamID
edited 2×, last 16.09.25 10:38:29 pm

old Re: Handle SteamIDs Easily in Lua

Hajt
User Off Offline

Quote
Well, using numbers is usually better for speed and memory. An integer takes about 4b, while a 17-char string uses at least 17b, plus a bit more if you add \r\n. Sorting numbers is simpler and faster because it just compares numeric values directly.

However, for small datasets, this difference doesn't really matter. Modern PCs are quite good at comparing strings, so the performance gap is often very small.

old Re: Handle SteamIDs Easily in Lua

Hajt
User Off Offline

Quote
@user Mami Tomoe: Nope, but remember - never store 64-bit SteamIDs as Lua numbers, otherwise you will lose precision. Converting them to strings is perfectly safe.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- Convert SteamID64 string to SteamID32 number
function SteamID64To32(sid64)
    -- sid64 must be string!
    return tonumber(sid64) - 76561197960265728
end

-- Convert SteamID32 number back to SteamID64 string
function SteamID32To64(sid32)
    return tostring(sid32 + 76561197960265728)
end

-- Example usage
local sid64 = "76561197960287930"  -- always as string
local sid32 = SteamID64To32(sid64)

print("SteamID32:", sid32)  -- number, safe for math
print("SteamID64:", SteamID32To64(sid32))  -- back to string, no precision lost
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview