-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgosteamconv.go
65 lines (54 loc) · 2.06 KB
/
gosteamconv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Package gosteamconv provides methods to convert steamids between strings (STEAM_X:Y:Z)
// and integers like the Steam community id.
package gosteamconv
import (
"errors"
"strconv"
)
// ErrIntTooSmall returned by package functions can be tested against this error
var ErrIntTooSmall = errors.New("64-bit steamid int should be bigger than 76561197960265728")
// SteamStringToInt32 takes a steamid string "STEAM_X:Y:Z" and converts it to a 32-bit integer.
func SteamStringToInt32(steamString string) (int, error) {
Y, err := strconv.Atoi(steamString[8:9])
if err != nil {
return int(0), err
}
Z, err := strconv.Atoi(steamString[10:])
if err != nil {
return int(0), err
}
return (Z * 2) + Y, nil
}
// SteamStringToInt64 takes a steamid string "STEAM_X:Y:Z" and converts it to a 64-bit integer.
func SteamStringToInt64(steamString string) (int64, error) {
Y, err := strconv.Atoi(steamString[8:9])
if err != nil {
return int64(0), err
}
Z, err := strconv.Atoi(steamString[10:])
if err != nil {
return int64(0), err
}
return int64((Z * 2) + 76561197960265728 + Y), nil
}
// SteamInt64ToString takes a 64-bit integer and converts it to a steamid string format "STEAM_X:Y:Z"
// The argument must be bigger than 76561197960265728, or it will return an error.
func SteamInt64ToString(steamInt int64) (string, error) {
if steamInt <= 76561197960265728 {
return string(""), ErrIntTooSmall
}
steamInt = steamInt - 76561197960265728
remainder := steamInt % 2
steamInt = steamInt / 2
return "STEAM_0:" + strconv.FormatInt(remainder, 10) + ":" + strconv.FormatInt(steamInt, 10), nil
}
// SteamInt32ToString takes a 32-bit integer and converts it to a steamid string format "STEAM_X:Y:Z"
// The argument must be bigger than 0, or it will return an error.
func SteamInt32ToString(steamInt int32) (string, error) {
if steamInt <= 0 {
return string(""), errors.New("32 bit steamid int should be bigger than 0")
}
remainder := steamInt % 2
steamInt = steamInt / 2
return "STEAM_0:" + strconv.FormatInt(int64(remainder), 10) + ":" + strconv.FormatInt(int64(steamInt), 10), nil
}