mirror of
https://github.com/vgstation-coders/vgstation13.git
synced 2025-12-10 02:16:05 +00:00
Fixed num2hex not padding with the right number of zeros (#19881)
This commit is contained in:
@@ -53,45 +53,32 @@ proc/hex2num(hex)
|
|||||||
|
|
||||||
return(num)
|
return(num)
|
||||||
|
|
||||||
|
//Returns the hex value of a decimal number
|
||||||
proc/num2hex(num, placeholder=2)
|
//len == length of returned string
|
||||||
//Converts a numeral (eg. 255) into a hexadecimal string (eg. "FF")
|
//if len < 0 then the returned string will be as long as it needs to be to contain the data
|
||||||
//The 'placeholder' argument inserts zeroes in front of the string
|
//Only supports positive numbers
|
||||||
// until the string is that length -- eg. 15 in hexadecimal is "F",
|
//if an invalid number is provided, it assumes num==0
|
||||||
// but the placeholder of 2 would make it "0F".
|
/proc/num2hex(var/num, var/len = 2)
|
||||||
|
|
||||||
if(!isnum(num))
|
if(!isnum(num))
|
||||||
CRASH("num2hex not given a numeric argument (user error)")
|
num = 0
|
||||||
return
|
num = round(abs(num))
|
||||||
|
. = ""
|
||||||
|
var/i=0
|
||||||
|
while(1)
|
||||||
|
if(len<=0)
|
||||||
if(!num)
|
if(!num)
|
||||||
return("0") //no computation necessary
|
break
|
||||||
|
else
|
||||||
var/hex = ""
|
if(i>=len)
|
||||||
|
break
|
||||||
var/i = 0
|
var/remainder = num/16
|
||||||
while(16**i < num) i++
|
num = round(remainder)
|
||||||
|
remainder = (remainder - num) * 16
|
||||||
for(var/power = i-1, power >= 0, power--)
|
switch(remainder)
|
||||||
var/val = round( num / (16 ** power) )
|
if(1 to 9)
|
||||||
num -= val * (16 ** power)
|
. = "[remainder]" + .
|
||||||
switch(val)
|
if(10 to 15)
|
||||||
if(0,1,2,3,4,5,6,7,8,9)
|
. = ascii2text(remainder+55) + .
|
||||||
hex += "[val]"
|
else
|
||||||
|
. = "0" + .
|
||||||
if(10)
|
i++
|
||||||
hex += "A"
|
|
||||||
if(11)
|
|
||||||
hex += "B"
|
|
||||||
if(12)
|
|
||||||
hex += "C"
|
|
||||||
if(13)
|
|
||||||
hex += "D"
|
|
||||||
if(14)
|
|
||||||
hex += "E"
|
|
||||||
if(15)
|
|
||||||
hex += "F"
|
|
||||||
|
|
||||||
while(lentext(hex) < placeholder) hex = "0[hex]"
|
|
||||||
|
|
||||||
return(hex)
|
|
||||||
|
|||||||
@@ -2,5 +2,6 @@
|
|||||||
#include "unit_test.dm"
|
#include "unit_test.dm"
|
||||||
#include "circuitboards.dm"
|
#include "circuitboards.dm"
|
||||||
#include "dna_and_disabilities.dm"
|
#include "dna_and_disabilities.dm"
|
||||||
|
#include "hexadecimal.dm"
|
||||||
#include "reagent_recipe_collisions.dm"
|
#include "reagent_recipe_collisions.dm"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
21
code/modules/unit_tests/hexadecimal.dm
Normal file
21
code/modules/unit_tests/hexadecimal.dm
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
/datum/unit_test/num2hex/start()
|
||||||
|
assert_eq(num2hex(0, 1), "0")
|
||||||
|
assert_eq(num2hex(1, 1), "1")
|
||||||
|
assert_eq(num2hex(15, 1), "F")
|
||||||
|
assert_eq(num2hex(16, 1), "0")
|
||||||
|
assert_eq(num2hex(0xABCDE, 1), "E")
|
||||||
|
assert_eq(num2hex(SHORT_REAL_LIMIT-1, 1), "F")
|
||||||
|
|
||||||
|
assert_eq(num2hex(0, 2), "00")
|
||||||
|
assert_eq(num2hex(1, 2), "01")
|
||||||
|
assert_eq(num2hex(15, 2), "0F")
|
||||||
|
assert_eq(num2hex(16, 2), "10")
|
||||||
|
assert_eq(num2hex(0xABCDE, 2), "DE")
|
||||||
|
assert_eq(num2hex(SHORT_REAL_LIMIT-1, 2), "FF")
|
||||||
|
|
||||||
|
assert_eq(num2hex(0, 8), "00000000")
|
||||||
|
assert_eq(num2hex(1, 8), "00000001")
|
||||||
|
assert_eq(num2hex(15, 8), "0000000F")
|
||||||
|
assert_eq(num2hex(16, 8), "00000010")
|
||||||
|
assert_eq(num2hex(0xABCDE, 8), "000ABCDE")
|
||||||
|
assert_eq(num2hex(SHORT_REAL_LIMIT-1, 8), "00FFFFFF")
|
||||||
@@ -70,3 +70,5 @@ var/failed_any_test = FALSE
|
|||||||
CHECK_TICK
|
CHECK_TICK
|
||||||
|
|
||||||
del(world)
|
del(world)
|
||||||
|
|
||||||
|
#define assert_eq(a, b) (a == b || (fail("[__FILE__]:[__LINE__]: assert_eq failed. Expected [b], got [a].")))
|
||||||
|
|||||||
Reference in New Issue
Block a user