-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHexadecimal.gd
427 lines (332 loc) · 9.98 KB
/
Hexadecimal.gd
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
tool
class_name PressAccept_Byter_Hexadecimal
# |=========================================|
# | |
# | Press Accept: Byter |
# | Byte-wise Arbitrary Radix Conversions |
# | |
# |=========================================|
#
# This file contains hexadecimal conversion functions both in signed and
# unsigned forms. In Godot integers are 64 bits and signed using 2s-complement.
#
# 2s-complement is 1s-complement - 1 (thus eliminating the negative zero) and
# is simple enough: the first bit is high, and the rest are the inverse of
# their unsigned counterparts.
#
# The conversion functions assume a hexadecimal string input with their output
# specified in the second-half. E.g. str2octal reads:
#
# hexadecimal string input -> octal string output
#
# This namespace contains the following (ordered by output):
#
# NOTE: numerical values (integers) are returned as Strings if they're large
#
# Binary Array
# - str2array
#
# Binary String
# - str2binary (str2bin)
#
# Signed Integer
# - str2signed (str2int)
#
# Unsigned Integer
# - str2unsigned (str2uint)
#
# Positive Integer (Doesn't Convert To Binary)
# - str2integer (str2pint)
#
# Octal String
# - str2octal (str2oct)
#
# Hexadecimal String
# - signed2str (int2str)
# - unsigned2str (uint2str)
# - to_hexadecimal
#
# Base 36 String
# - str2base36 (str2b36)
#
# Base 62 String
# - str2base62 (str2b62)
#
# Arbitrary Base String
# - str2arbitrary (str2arb)
#
# |------------------|
# | Meta Information |
# |------------------|
#
# Organization Namespace : PressAccept
# Package Namespace : Byter
# Class : Hexadecimal
#
# Organization : Press Accept
# Organization URI : https://pressaccept.com/
# Organization Social : @pressaccept
#
# Author : Asher Kadar Wolfstein
# Author URI : https://wunk.me/ (Personal Blog)
# Author Social : https://incarnate.me/members/asherwolfstein/
# @asherwolfstein (Twitter)
# https://ko-fi.com/asherwolfstein
#
# Copyright : Press Accept: Byter © 2021 The Novelty Factor LLC
# (Press Accept, Asher Kadar Wolfstein)
# License : MIT (see LICENSE)
#
# |-----------|
# | Changelog |
# |-----------|
#
# 1.0.0 06/01/2021 First Release
#
# *************
# | Constants |
# *************
# hexadecimal digits to binary strings
#
# used in the conversion function for fast string replacement
const DICT_HEXADECIMAL_2_BINARY = {
'0': '0000',
'1': '0001',
'2': '0010',
'3': '0011',
'4': '0100',
'5': '0101',
'6': '0110',
'7': '0111',
'8': '1000',
'9': '1001',
'a': '1010',
'b': '1011',
'c': '1100',
'd': '1101',
'e': '1110',
'f': '1111'
}
# powers of 16
#
# used to compute positive integer
const ARR_POWERS_OF_16: Array = [
1 << 0, # 1
1 << 4, # 16
1 << 8, # 256
1 << 12, # 4,096
1 << 16, # 65,536
1 << 20, # 1,048,576
1 << 24, # 16,777,216
1 << 28, # 268,435,456
1 << 32, # 4,294,967,296
1 << 36, # 68,719,476,736
1 << 40, # 1,099,511,627,776
1 << 44, # 17,592,186,044,416
1 << 48, # 281,474,976,710,656
1 << 52, # 4,503,599,627,370,496
1 << 56, # 72,057,594,037,927,936
1 << 60, # 1,152,921,504,606,846,976
]
# max number of hexadecimal digits representable as an integer is 16 (64 bits)
const INT_MAX_DIGITS: int = 16
# ***************************
# | Public Static Functions |
# ***************************
# |----------------------------|
# | String -> Binary Functions |
# |----------------------------|
# convert hexadecimal string representation to a binary string representation
#
# truncate removes leading zeros from conversion (hexadecimal is in sets of 4)
static func str2binary(
hexadecimal_str : String,
truncate : bool = false) -> String:
# some conventions preface hexadecimal_str numbers with 0x
hexadecimal_str = hexadecimal_str.trim_prefix('0x')
hexadecimal_str = hexadecimal_str.to_lower()
var ret: String = ''
for digit in hexadecimal_str:
ret += DICT_HEXADECIMAL_2_BINARY[digit]
if truncate:
ret = ret.lstrip('0')
ret = ret if ret else '0'
return ret
# alias for str2binary
static func str2bin(
hexadecimal_str : String,
truncate : bool = false) -> String:
return str2binary(hexadecimal_str, truncate)
# convert hexadecimal string representation to a boolean array (binary)
static func str2array(
hexadecimal_str : String,
truncate : bool = false) -> Array:
return PressAccept_Byter_Binary.str2array(
str2binary(hexadecimal_str, truncate)
)
# |------------------------------|
# | String -> Integer Conversion |
# |------------------------------|
# convert hexadecimal string representation to a signed integer
#
# 2s-complement is default
static func str2signed(
hexadecimal_str : String,
ones_complement : bool = false,
return_object : bool = false) -> int:
return PressAccept_Byter_Binary.str2signed(
str2binary(hexadecimal_str, false), # truncating = always negative
false,
ones_complement,
return_object
)
# alias for str2signed
static func str2int(
hexadecimal_str : String,
ones_complement : bool = false,
return_object : bool = false) -> int:
return str2signed(hexadecimal_str, ones_complement, return_object)
# convert hexadecimal string representation to an unsigned integer
static func str2unsigned(
hexadecimal_str : String,
return_object : bool = false) -> int:
return PressAccept_Byter_Binary.str2unsigned(
str2binary(hexadecimal_str, true), # doesn't matter
false,
return_object
)
# alias for str2unsigned
static func str2uint(
hexadecimal_str : String,
return_object : bool = false) -> int:
return str2unsigned(hexadecimal_str, return_object)
# convert hexadecimal string representation to a positive integer
#
# NOTE: does not convert to binary as an intemediary (for optimization)
static func str2integer(
hexadecimal_str : String,
return_object : bool = false):
var Basic : Script = PressAccept_Arbiter_Basic
# some conventions preface hexadecimal_str numbers with 0x
hexadecimal_str = hexadecimal_str.trim_prefix('0x')
hexadecimal_str = hexadecimal_str.to_lower()
return PressAccept_Byter_Common.base2integer(
Basic.hexadecimal_to_array(hexadecimal_str),
ARR_POWERS_OF_16,
return_object
)
# alias for str2integer
static func str2pint(
hexadecimal_str : String,
return_object : bool = false):
return str2integer(hexadecimal_str, return_object)
# |---------------------------|
# | String -> Base Conversion |
# |---------------------------|
# convert hexadecimal string representation to octal string representation
static func str2octal(
hexadecimal_str : String,
truncate : bool = false) -> String:
return PressAccept_Byter_Binary.str2octal(
str2binary(hexadecimal_str, truncate)
)
static func str2oct(
hexadecimal_str : String,
truncate : bool = false) -> String:
return str2octal(hexadecimal_str, truncate)
# convert hexadecimal string representation to base-36 string
#
# pass true to signed to use 2s-complement
#
# Test in test_base36.gd
static func str2base36(
hexadecimal_str : String) -> String:
var Base36: Script = load('res://addons/PressAccept/Byter/Base36.gd')
return Base36.integer2str(str2unsigned(hexadecimal_str))
# alias for str2base36
static func str2b36(
hexadecimal_str : String) -> String:
return str2base36(hexadecimal_str)
# convert hexadecimal string representation to base-62 string representation
#
# pass true to signed to use 2s-complement
#
# Test in test_base62.gd
static func str2base62(
hexadecimal_str : String) -> String:
var Base62: Script = load('res://addons/PressAccept/Byter/Base62.gd')
return Base62.integer2str(str2unsigned(hexadecimal_str))
# alias for str2base62
static func str2b62(
hexadecimal_str : String) -> String:
return str2base62(hexadecimal_str)
# convert hexadecimal string representation to arbitrary base string
#
# pass true to signed to use 2s-complement
#
# Test in test_base36.gd
static func str2arbitrary(
hexadecimal_str : String,
from_int : FuncRef) -> String:
return PressAccept_Byter_Binary.str2arbitrary(
str2binary(hexadecimal_str),
from_int
)
# alias for str2base36
static func str2arb(
hexadecimal_str : String,
from_int : FuncRef) -> String:
return str2arbitrary(hexadecimal_str, from_int)
# |------------------------------|
# | Integer -> String Conversion |
# |------------------------------|
# convert signed integer to a hexadecimal string representation
#
# 2s-complement is default
static func signed2str(
int_value, # int | String
ones_complement: bool = false) -> String:
var signed_binary_str: String = PressAccept_Byter_Binary.signed2str(
int_value,
-1,
ones_complement
)
return PressAccept_Byter_Binary.str2hexadecimal(
signed_binary_str,
signed_binary_str[0] == '1' # signed?
)
# alias for signed2str
static func int2str(
int_value, # int | String
ones_complement: bool = false) -> String:
return signed2str(int_value, ones_complement)
# convert unsigned integer to a hexadecimal string representation
static func unsigned2str(
int_value) -> String: # int | String
return PressAccept_Byter_Binary.str2hexadecimal(
PressAccept_Byter_Binary.unsigned2str(int_value, -1),
false
)
# alias for unsigned2str
static func uint2str(
int_value) -> String: # int | String
return unsigned2str(int_value)
# |--------------------------|
# | Any -> String Conversion |
# |--------------------------|
# dynamically typed conversion
static func to_hexadecimal(
from_value,
from_radix = -1) -> String: # radix of from_value, def: signed decimal
var Myself: Script = load('res://addons/PressAccept/Byter/Hexadecimal.gd')
var ENUM_RADIX: Dictionary = PressAccept_Byter_Formats.ENUM_RADIX
from_radix = PressAccept_Byter_Common.normalize_radix(from_radix)
if typeof(from_value) == TYPE_STRING:
if from_radix == ENUM_RADIX.HEXADECIMAL:
return from_value
return PressAccept_Byter_Common.to_base(
from_value,
Myself,
'hexadecimal',
from_radix
)