-
Notifications
You must be signed in to change notification settings - Fork 71
/
class_Conversions.ahk
136 lines (108 loc) · 3.04 KB
/
class_Conversions.ahk
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class Conversions {
HexToInt(HexString) {
if !(SubStr(HexString, 1, 2) = "0x") {
Throw, Exception("HexToInt can not convert non-hex (" HexString ") to int")
}
Base := StrLen(HexString) - 2 ; Subtract out the 0x
Decimal := 0
for k, Character in StrSplit(SubStr(HexString, 3)) {
CharacterCode := Asc(Character)
if (Asc("0") <= CharacterCode && CharacterCode <= Asc("9")) {
CharacterCode := (CharacterCode - 48)
}
else if (Asc("a") <= CharacterCode && CharacterCode <= Asc("f")) {
CharacterCode := (CharacterCode - 97) + 10
}
else if (Asc("A") <= CharacterCode && CharacterCode <= Asc("F")) {
CharacterCode := (CharacterCode - 65) + 10
}
Decimal += CharacterCode * (16 ** --Base)
}
return Decimal
}
BinToInt(BinString) {
if !(SubStr(BinString, 1, 2) = "0b") {
Throw, Exception("BinToInt can not convert non-binary (" BinString ") to int")
}
Base := StrLen(BinString) - 2
Decimal := 0
for k, Character in StrSplit(SubStr(BinString, 3)) {
Decimal += Character * (2 ** --Base)
}
return Decimal
}
OctToInt(OctString) {
if !(SubStr(OctString, 1, 2) = "0o") {
Throw, Exception("OctToInt can not convert non-octal (" OctString ") to int")
}
Base := StrLen(OctString) - 2
Decimal := 0
for k, Character in StrSplit(SubStr(OctString, 3)) {
Decimal += Character * (8 ** --Base)
}
return Decimal
}
IntToHex(Int, NoZeros := True) {
if !(IsNumber(Int)) {
return "0x00"
}
static HexCharacters := ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]
End := (NoZeros ? "" : "0x")
HexString := ""
Quotient := Int
loop {
Remainder := Mod(Quotient, 16)
HexString := HexCharacters[Remainder + 1] HexString
Quotient := Floor(Quotient / 16)
} until (Quotient = 0)
loop % 2 - StrLen(HexString) {
HexString := "0" HexString
}
if (Mod(StrLen(HexString), 2)) {
HexString := "0" HexString
}
return End HexString
}
IsNumber(Value) {
if Value is Integer
return true
return false
}
IsFloat(Value) {
if Value is Float
return true
return false
}
FloatToBinaryInt(Float) {
VarSetCapacity(Buffer, 8, 0)
NumPut(Float, &Buffer + 0, 0, "Double")
return NumGet(&Buffer + 0, 0, "UInt64")
}
SplitIntoBytes32(Integer) {
Array := []
loop, % 4 {
Array.Push((Integer >> ((A_Index - 1) * 8)) & 0xFF)
}
return Array
}
SplitIntoBytes64(Integer) {
FirstFour := SplitIntoBytes32((Integer & 0x00000000FFFFFFFF) >> 0)
LastFour := SplitIntoBytes32((Integer & 0x7FFFFFFF00000000) >> 32)
return [FirstFour[1], FirstFour[2], FirstFour[3], FirstFour[4], LastFour[1], LastFour[2], LastFour[3], LastFour[4]]
}
NumberSizeOf(Number, ReturnNumber := true) {
static Sizes := {8: "I8", 16: "I16", 32: "I32", 64: "I64"}
loop 64 {
NextBit := Number & (1 << (64 - A_Index))
if (NextBit) {
Length := A_Index - 1
break
}
}
NewLength := 64 - Length
while !(Sizes.HasKey(NewLength)) {
NewLength++
}
return (ReturnNumber ? NewLength : Sizes[NewLength])
}
}