Created
October 19, 2020 02:47
-
-
Save DragoniteSpam/ecc39406d8f1cdc860caaff6c5059d21 to your computer and use it in GitHub Desktop.
No not the magical kind.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hex(str) { | |
var result = 0; | |
// special unicode values | |
static ZERO = ord("0"); | |
static NINE = ord("9"); | |
static A = ord("A"); | |
static F = ord("F"); | |
for (var i = 1; i <= string_length(str); i++) { | |
var c = ord(string_char_at(string_upper(str), i)); | |
// you could also multiply by 16 but you get more nerd points for bitshifts | |
result = result << 4; | |
// if the character is a number or letter, add the value | |
// it represents to the total | |
if (c >= ZERO && c <= NINE) { | |
result = result + (c - ZERO); | |
} else if (c >= A && c <= F) { | |
result = result + (c - A + 10); | |
// otherwise complain | |
} else { | |
throw "bad input for hex(str): " + str; | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment