Last active
August 29, 2015 14:09
-
-
Save tanelih/7dc5ad7c5044af1119ea to your computer and use it in GitHub Desktop.
Simple SteamID conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* See https://developer.valvesoftware.com/wiki/SteamID for details. | |
*/ | |
package steamid | |
import "strconv" | |
const ( | |
ACCOUNT_ID_BITS = 32 | |
ACCOUNT_TYPE_BITS = 4 | |
ACCOUNT_INSTANCE_BITS = 20 | |
ACCOUNT_UNIVERSE_BITS = 8 | |
DEFAULT_ACCOUNT_TYPE int64 = 1 | |
DEFAULT_ACCOUNT_UNIVERSE int64 = 1 | |
DEFAULT_ACCOUNT_INSTANCE int64 = 1 | |
) | |
func pad(binStr string, toLength int) string { | |
delta := toLength - len(binStr) | |
for i := 0; i < delta; i++ { | |
binStr = "0" + binStr | |
} | |
return binStr | |
} | |
func ToAccountID(steamID int64) (int64, error) { | |
padding, err := FromAccountID(0) | |
if err != nil { | |
return 0, err | |
} | |
return (steamID - padding), nil | |
} | |
func FromAccountID(accountID int64) (int64, error) { | |
var ( | |
idBin = pad(strconv.FormatInt(accountID, 2), ACCOUNT_ID_BITS) | |
typeBin = pad(strconv.FormatInt(DEFAULT_ACCOUNT_TYPE, 2), ACCOUNT_TYPE_BITS) | |
instanceBin = pad(strconv.FormatInt(DEFAULT_ACCOUNT_INSTANCE, 2), ACCOUNT_INSTANCE_BITS) | |
universeBin = pad(strconv.FormatInt(DEFAULT_ACCOUNT_UNIVERSE, 2), ACCOUNT_UNIVERSE_BITS) | |
) | |
return strconv.ParseInt(universeBin + typeBin + instanceBin + idBin, 2, 64) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment