Skip to content

Instantly share code, notes, and snippets.

@tanelih
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanelih/7dc5ad7c5044af1119ea to your computer and use it in GitHub Desktop.
Save tanelih/7dc5ad7c5044af1119ea to your computer and use it in GitHub Desktop.
Simple SteamID conversion
/**
* 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