Forum

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

English Handle SteamIDs Easily in Lua

6 replies
To the start Previous 1 Next To the start

old Handle SteamIDs Easily in Lua

Hajt
User On Online

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 On Online

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 On Online

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

old Re: Handle SteamIDs Easily in Lua

Earthkin
User Off Offline

Quote
The idea that you need to "save space" in this case is ridiculous. Use the string, there is no reason to convert it to a number. What you're doing is just silly and unnecessary. Sorting strings is very fast. There is no reason for any of this.

What are you doing with these IDs? I can't think of any use that requires anything more than comparing two IDs to check if they are the same or not. This can easily be done with Lua's "==" operator.

For some reason you are subtracting a large number from this new number you made by "tostring(steamid)". Why? What is this number and what are you trying to achieve?

You do realize that Lua still stores this as a 64-bit double correct? So what have you achieved? You have converted a steamid into ... something else. You have lost information along the way.

Reading the steamid page it looks like the significant part of the ID is 31-bits long. I haven't found out if this number is unique across universes or not. If so all you need to do is extract those bits to store your number. Without integers and bitwise operations this process won't be so simple or clean but it can be done. But why would this even be necessary? Give me one use case.
edited 1×, last 20.09.25 01:17:32 am

old Re: Handle SteamIDs Easily in Lua

MikuAuahDark
User Off Offline

Quote
Unfortunately doing
tonumber
on the 64-bit SteamID is enough to cause data loss in Lua.

To make it clear, it's important to know that Lua's number is double-precision (it occupies 8-byte of memory). This means it can store up to 2^53 integers. This also means
tonumber
ing the 64-bit SteamID string means you'll loss the Y component and around 5 bits of the account number.

The only way to perform arithmetic on the SteamID safely in Lua is either invent 64-bit integer library in Lua or if you're using file cs2d LuaJIT for Dedicated Server , using boxed 64-bit integer.
1
2
3
4
---@param s string
function SteamID64To32(s)
	return loadstring("return "..s.."ULL")()
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview