Skip to content

Instantly share code, notes, and snippets.

@tinkerer-red
Last active October 16, 2024 20:24
Show Gist options
  • Select an option

  • Save tinkerer-red/2b14dec6543a4ac820c45d2f86741359 to your computer and use it in GitHub Desktop.

Select an option

Save tinkerer-red/2b14dec6543a4ac820c45d2f86741359 to your computer and use it in GitHub Desktop.
Hex String To Int64, and Binary String To Int64
// 1. Upper Bound Positive (64-bit max positive value)
show_debug_message((0b0111111111111111111111111111111111111111111111111111111111111111 == __binaryTo64Bit("0b0111111111111111111111111111111111111111111111111111111111111111")) ? "Success" : $"{0b0111111111111111111111111111111111111111111111111111111111111111} != {__binaryTo64Bit("0b0111111111111111111111111111111111111111111111111111111111111111")}");
// 2. Lower Bound Positive (64-bit min positive value)
show_debug_message((0b1 == __binaryTo64Bit("0b1")) ? "Success" : $"{0b1} != {__binaryTo64Bit("0b1")}");
// 3. Upper Bound Negative (64-bit min negative value)
show_debug_message((0b1000000000000000000000000000000000000000000000000000000000000000 == __binaryTo64Bit("0b1000000000000000000000000000000000000000000000000000000000000000")) ? "Success" : $"{0b1000000000000000000000000000000000000000000000000000000000000000} != {__binaryTo64Bit("0b1000000000000000000000000000000000000000000000000000000000000000")}");
// 4. Lower Bound Negative (64-bit max negative value)
show_debug_message((0b1111111111111111111111111111111111111111111111111111111111111111 == __binaryTo64Bit("0b1111111111111111111111111111111111111111111111111111111111111111")) ? "Success" : $"{0b1111111111111111111111111111111111111111111111111111111111111111} != {__binaryTo64Bit("0b1111111111111111111111111111111111111111111111111111111111111111")}");
// 5. Zero
show_debug_message((0b0000 == __binaryTo64Bit("0b0000")) ? "Success" : $"{0b0000} != {__binaryTo64Bit("0b0000")}");
// 6. Near Zero Positive
show_debug_message((0b0010 == __binaryTo64Bit("0b0010")) ? "Success" : $"{0b0010} != {__binaryTo64Bit("0b0010")}");
// 7. Near Zero Negative
show_debug_message((0b1111111111111111111111111111111111111111111111111111111111111110 == __binaryTo64Bit("0b1111111111111111111111111111111111111111111111111111111111111110")) ? "Success" : $"{0b1111111111111111111111111111111111111111111111111111111111111110} != {__binaryTo64Bit("0b1111111111111111111111111111111111111111111111111111111111111110")}");
// 8. Large Positive (close to max positive)
show_debug_message((0b0111111111111111111111111111111111111111111111111111000000000000 == __binaryTo64Bit("0b0111111111111111111111111111111111111111111111111111000000000000")) ? "Success" : $"{0b0111111111111111111111111111111111111111111111111111000000000000} != {__binaryTo64Bit("0b0111111111111111111111111111111111111111111111111111000000000000")}");
// 9. Large Negative (close to min negative)
show_debug_message((0b1000000000000000000000000000000000000000000000010000000000000000 == __binaryTo64Bit("0b1000000000000000000000000000000000000000000000010000000000000000")) ? "Success" : $"{0b1000000000000000000000000000000000000000000000010000000000000000} != {__binaryTo64Bit("0b1000000000000000000000000000000000000000000000010000000000000000")}");
// 10. Mid-Range Positive
show_debug_message((0b0100000000000000000000000000000000000000000000000000000000000000 == __binaryTo64Bit("0b0100000000000000000000000000000000000000000000000000000000000000")) ? "Success" : $"{0b0100000000000000000000000000000000000000000000000000000000000000} != {__binaryTo64Bit("0b0100000000000000000000000000000000000000000000000000000000000000")}");
// 11. Mid-Range Negative
show_debug_message((0b1011000000000000000000000000000000000000000000000000000000000000 == __binaryTo64Bit("0b1011000000000000000000000000000000000000000000000000000000000000")) ? "Success" : $"{0b1011000000000000000000000000000000000000000000000000000000000000} != {__binaryTo64Bit("0b1011000000000000000000000000000000000000000000000000000000000000")}");
// 12. 32-bit Maximum Unsigned Value
show_debug_message((0b11111111111111111111111111111111 == __binaryTo64Bit("0b11111111111111111111111111111111")) ? "Success" : $"{0b11111111111111111111111111111111} != {__binaryTo64Bit("0b11111111111111111111111111111111")}");
// 13. 32-bit Minimum Unsigned Value
show_debug_message((0b00000000000000000000000000000000 == __binaryTo64Bit("0b00000000000000000000000000000000")) ? "Success" : $"{0b00000000000000000000000000000000} != {__binaryTo64Bit("0b00000000000000000000000000000000")}");
// 14. Positive Middle Bound (32-bit upper half of 64-bit)
show_debug_message((0b0000000000000000000000000000000011111111111111111111111111111111 == __binaryTo64Bit("0b0000000000000000000000000000000011111111111111111111111111111111")) ? "Success" : $"{0b0000000000000000000000000000000011111111111111111111111111111111} != {__binaryTo64Bit("0b0000000000000000000000000000000011111111111111111111111111111111")}");
// 15. Negative Middle Bound (32-bit lower half of 64-bit)
show_debug_message((0b1111111111111111111111111111111100000000000000000000000000000000 == __binaryTo64Bit("0b1111111111111111111111111111111100000000000000000000000000000000")) ? "Success" : $"{0b1111111111111111111111111111111100000000000000000000000000000000} != {__binaryTo64Bit("0b1111111111111111111111111111111100000000000000000000000000000000")}");
// 16. Small Negative Value
show_debug_message((0b1111111111111111111111111111111111111111111111111111111111111001 == __binaryTo64Bit("0b1111111111111111111111111111111111111111111111111111111111111001")) ? "Success" : $"{0b1111111111111111111111111111111111111111111111111111111111111001} != {__binaryTo64Bit("0b1111111111111111111111111111111111111111111111111111111111111001")}");
// 17. Small Positive Value
show_debug_message((0b1010 == __binaryTo64Bit("0b1010")) ? "Success" : $"{0b1010} != {__binaryTo64Bit("0b1010")}");
// 18. Edge Case Positive
show_debug_message((0b0001000000000000000000000000000000000000000000000000000000000000 == __binaryTo64Bit("0b0001000000000000000000000000000000000000000000000000000000000000")) ? "Success" : $"{0b0001000000000000000000000000000000000000000000000000000000000000} != {__binaryTo64Bit("0b0001000000000000000000000000000000000000000000000000000000000000")}");
// 19. Edge Case Negative
show_debug_message((0b1111111111111111111111111111111100000000000000000000000000000001 == __binaryTo64Bit("0b1111111111111111111111111111111100000000000000000000000000000001")) ? "Success" : $"{0b1111111111111111111111111111111100000000000000000000000000000001} != {__binaryTo64Bit("0b1111111111111111111111111111111100000000000000000000000000000001")}");
// 20. Overflow Case
//show_debug_message((0b111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 == __binaryTo64Bit("0b111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")) ? "Success" : $"{0b111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111} != {__binaryTo64Bit("0b111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")}");
// 21. Mixed High and Low Values
show_debug_message((0b0001001000110100010101100111100010010000101010111100110111101111 == __binaryTo64Bit("0b0001001000110100010101100111100010010000101010111100110111101111")) ? "Success" : $"{0b0001001000110100010101100111100010010000101010111100110111101111} != {__binaryTo64Bit("0b0001001000110100010101100111100010010000101010111100110111101111")}");
// 22. Random Positive
show_debug_message((0b0111111111111111111111111111111111111111101010101010101010101010 == __binaryTo64Bit("0b0111111111111111111111111111111111111111101010101010101010101010")) ? "Success" : $"{0b0111111111111111111111111111111111111111101010101010101010101010} != {__binaryTo64Bit("0b0111111111111111111111111111111111111111101010101010101010101010")}");
// 23. Random Negative
show_debug_message((0b1000000000000000000000000000000000000000010101010101010101010101 == __binaryTo64Bit("0b1000000000000000000000000000000000000000010101010101010101010101")) ? "Success" : $"{0b1000000000000000000000000000000000000000010101010101010101010101} != {__binaryTo64Bit("0b1000000000000000000000000000000000000000010101010101010101010101")}");
// 24. Maximum Negative Near Overflow
show_debug_message((0b1111111111111111111111111111111111111111111111111111111111111101 == __binaryTo64Bit("0b1111111111111111111111111111111111111111111111111111111111111101")) ? "Success" : $"{0b1111111111111111111111111111111111111111111111111111111111111101} != {__binaryTo64Bit("0b1111111111111111111111111111111111111111111111111111111111111101")}");
// 25. Maximum Positive 63-bit Boundary
show_debug_message((0b0011111111111111111111111111111111111111111111111111111111111111 == __binaryTo64Bit("0b0011111111111111111111111111111111111111111111111111111111111111")) ? "Success" : $"{0b0011111111111111111111111111111111111111111111111111111111111111} != {__binaryTo64Bit("0b0011111111111111111111111111111111111111111111111111111111111111")}");
// 26. Edge Case Overflow Handling
//show_debug_message((0b111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000 == __binaryTo64Bit("0b111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000")) ? "Success" : $"{0b111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000} != {__binaryTo64Bit("0b111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000")}");
// 27. Low Boundary Overflow
//show_debug_message((0b00011111111111111111111111111111111111111111111111111111111111111111 == __binaryTo64Bit("0b00011111111111111111111111111111111111111111111111111111111111111111")) ? "Success" : $"{0b00011111111111111111111111111111111111111111111111111111111111111111} != {__binaryTo64Bit("0b00011111111111111111111111111111111111111111111111111111111111111111")}");
// 28. Large Positive with Overflow
show_debug_message((0b0001111111111111111111111111111111111111111111111111111111111111 == __binaryTo64Bit("0b0001111111111111111111111111111111111111111111111111111111111111")) ? "Success" : $"{0b0001111111111111111111111111111111111111111111111111111111111111} != {__binaryTo64Bit("0b0001111111111111111111111111111111111111111111111111111111111111")}");
// 29. Invalid Negative with Overflow
//show_debug_message((0b1111111111111111111111111111111111111111111111111111111100000001 == __binaryTo64Bit("0b1111111111111111111111111111111111111111111111111111111100000001")) ? "Success" : $"{0b11111111111111111111111111111111111111111111111111111111000000010b1111111111111111111111111111111111111111111111111111111100000001} != {__binaryTo64Bit("0b11111111111111111111111111111111111111111111111111111111000000010b1111111111111111111111111111111111111111111111111111111100000001")}");
// 30. Positive Out of Range
show_debug_message((0b0001001000110100010101100111100010011010101111001101111011110000 == __binaryTo64Bit("0b0001001000110100010101100111100010011010101111001101111011110000")) ? "Success" : $"{0b0001001000110100010101100111100010011010101111001101111011110000} != {__binaryTo64Bit("0b0001001000110100010101100111100010011010101111001101111011110000")}");
// 31. Upper Half Range Positive
show_debug_message((0b1111000011110000111100001111000011110000111100001111000011110000 == __binaryTo64Bit("0b1111000011110000111100001111000011110000111100001111000011110000")) ? "Success" : $"{0b1111000011110000111100001111000011110000111100001111000011110000} != {__binaryTo64Bit("0b1111000011110000111100001111000011110000111100001111000011110000")}");
// 32. Large Negative Beyond Upper Bound
show_debug_message((0b1111111111111111111111111111111111111111111111111111111111111111 == __binaryTo64Bit("0b1111111111111111111111111111111111111111111111111111111111111111")) ? "Success" : $"{0b1111111111111111111111111111111111111111111111111111111111111111} != {__binaryTo64Bit("0b1111111111111111111111111111111111111111111111111111111111111111")}");
// 1. Upper Bound Positive (64-bit max positive value)
show_debug_message((0x7FFFFFFFFFFFFFFF == __hexTo64Bit("0x7FFFFFFFFFFFFFFF")) ? "Success" : $"{0x7FFFFFFFFFFFFFFF} != {__hexTo64Bit("0x7FFFFFFFFFFFFFFF")}");
// 2. Lower Bound Positive (64-bit min positive value)
show_debug_message((0x1 == __hexTo64Bit("0x1")) ? "Success" : $"{0x1} != {__hexTo64Bit("0x1")}");
// 3. Upper Bound Negative (64-bit min negative value)
show_debug_message((0x8000000000000000 == __hexTo64Bit("0x8000000000000000")) ? "Success" : $"{0x8000000000000000} != {__hexTo64Bit("0x8000000000000000")}");
// 4. Lower Bound Negative (64-bit max negative value)
show_debug_message((0xFFFFFFFFFFFFFFFF == __hexTo64Bit("0xFFFFFFFFFFFFFFFF")) ? "Success" : $"{0xFFFFFFFFFFFFFFFF} != {__hexTo64Bit("0xFFFFFFFFFFFFFFFF")}");
// 5. Zero
show_debug_message((0x0 == __hexTo64Bit("0x0")) ? "Success" : $"{0x0} != {__hexTo64Bit("0x0")}");
// 6. Near Zero Positive
show_debug_message((0x2 == __hexTo64Bit("0x2")) ? "Success" : $"{0x2} != {__hexTo64Bit("0x2")}");
// 7. Near Zero Negative
show_debug_message((0xFFFFFFFFFFFFFFFE == __hexTo64Bit("0xFFFFFFFFFFFFFFFE")) ? "Success" : $"{0xFFFFFFFFFFFFFFFE} != {__hexTo64Bit("0xFFFFFFFFFFFFFFFE")}");
// 8. Large Positive (close to max positive)
show_debug_message((0x7FFFFFFFFFFFF000 == __hexTo64Bit("0x7FFFFFFFFFFFF000")) ? "Success" : $"{0x7FFFFFFFFFFFF000} != {__hexTo64Bit("0x7FFFFFFFFFFFF000")}");
// 9. Large Negative (close to min negative)
show_debug_message((0x8000000000010000 == __hexTo64Bit("0x8000000000010000")) ? "Success" : $"{0x8000000000010000} != {__hexTo64Bit("0x8000000000010000")}");
// 10. Mid-Range Positive
show_debug_message((0x4000000000000000 == __hexTo64Bit("0x4000000000000000")) ? "Success" : $"{0x4000000000000000} != {__hexTo64Bit("0x4000000000000000")}");
// 11. Mid-Range Negative
show_debug_message((0xB000000000000000 == __hexTo64Bit("0xB000000000000000")) ? "Success" : $"{0xB000000000000000} != {__hexTo64Bit("0xB000000000000000")}");
// 12. 32-bit Maximum Unsigned Value
show_debug_message((0xFFFFFFFF == __hexTo64Bit("0xFFFFFFFF")) ? "Success" : $"{0xFFFFFFFF} != {__hexTo64Bit("0xFFFFFFFF")}");
// 13. 32-bit Minimum Unsigned Value
show_debug_message((0x00000000 == __hexTo64Bit("0x00000000")) ? "Success" : $"{0x00000000} != {__hexTo64Bit("0x00000000")}");
// 14. Positive Middle Bound (32-bit upper half of 64-bit)
show_debug_message((0x00000000FFFFFFFF == __hexTo64Bit("0x00000000FFFFFFFF")) ? "Success" : $"{0x00000000FFFFFFFF} != {__hexTo64Bit("0x00000000FFFFFFFF")}");
// 15. Negative Middle Bound (32-bit lower half of 64-bit)
show_debug_message((0xFFFFFFFF00000000 == __hexTo64Bit("0xFFFFFFFF00000000")) ? "Success" : $"{0xFFFFFFFF00000000} != {__hexTo64Bit("0xFFFFFFFF00000000")}");
// 16. Small Negative Value
show_debug_message((0xFFFFFFFFFFFFFFF9 == __hexTo64Bit("0xFFFFFFFFFFFFFFF9")) ? "Success" : $"{0xFFFFFFFFFFFFFFF9} != {__hexTo64Bit("0xFFFFFFFFFFFFFFF9")}");
// 17. Small Positive Value
show_debug_message((0xA == __hexTo64Bit("0xA")) ? "Success" : $"{0xA} != {__hexTo64Bit("0xA")}");
// 18. Edge Case Positive
show_debug_message((0x1000000000000000 == __hexTo64Bit("0x1000000000000000")) ? "Success" : $"{0x1000000000000000} != {__hexTo64Bit("0x1000000000000000")}");
// 19. Edge Case Negative
show_debug_message((0xFFFFFFFF00000001 == __hexTo64Bit("0xFFFFFFFF00000001")) ? "Success" : $"{0xFFFFFFFF00000001} != {__hexTo64Bit("0xFFFFFFFF00000001")}");
// 20. Overflow Case
//show_debug_message((0xFFFFFFFFFFFFFFFFFFFFFFFF == __hexTo64Bit("0xFFFFFFFFFFFFFFFFFFFFFFFF")) ? "Success" : $"{0xFFFFFFFFFFFFFFFFFFFFFFFF} != {__hexTo64Bit("0xFFFFFFFFFFFFFFFFFFFFFFFF")}");
// 21. Mixed High and Low Values
show_debug_message((0x1234567890ABCDEF == __hexTo64Bit("0x1234567890ABCDEF")) ? "Success" : $"{0x1234567890ABCDEF} != {__hexTo64Bit("0x1234567890ABCDEF")}");
// 22. Random Positive
show_debug_message((0x7FFFFFFFFFAAAAAA == __hexTo64Bit("0x7FFFFFFFFFAAAAAA")) ? "Success" : $"{0x7FFFFFFFFFAAAAAA} != {__hexTo64Bit("0x7FFFFFFFFFAAAAAA")}");
// 23. Random Negative
show_debug_message((0x8000000000555555 == __hexTo64Bit("0x8000000000555555")) ? "Success" : $"{0x8000000000555555} != {__hexTo64Bit("0x8000000000555555")}");
// 24. Maximum Negative Near Overflow
show_debug_message((0xFFFFFFFFFFFFFFFD == __hexTo64Bit("0xFFFFFFFFFFFFFFFD")) ? "Success" : $"{0xFFFFFFFFFFFFFFFD} != {__hexTo64Bit("0xFFFFFFFFFFFFFFFD")}");
// 25. Maximum Positive 63-bit Boundary
show_debug_message((0x3FFFFFFFFFFFFFFF == __hexTo64Bit("0x3FFFFFFFFFFFFFFF")) ? "Success" : $"{0x3FFFFFFFFFFFFFFF} != {__hexTo64Bit("0x3FFFFFFFFFFFFFFF")}");
// 26. Edge Case Overflow Handling
//show_debug_message((0xFFFFFFFF0000000000000000 == __hexTo64Bit("0xFFFFFFFF0000000000000000")) ? "Success" : $"{0xFFFFFFFF0000000000000000} != {__hexTo64Bit("0xFFFFFFFF0000000000000000")}");
// 27. Low Boundary Overflow
//show_debug_message((0x1FFFFFFFFFFFFFFFF == __hexTo64Bit("0x1FFFFFFFFFFFFFFFF")) ? "Success" : $"{0x1FFFFFFFFFFFFFFFF} != {__hexTo64Bit("0x1FFFFFFFFFFFFFFFF")}");
// 28. Large Positive with Overflow
show_debug_message((0x1FFFFFFFFFFFFFFF == __hexTo64Bit("0x1FFFFFFFFFFFFFFF")) ? "Success" : $"{0x1FFFFFFFFFFFFFFF} != {__hexTo64Bit("0x1FFFFFFFFFFFFFFF")}");
// 29. Invalid Negative with Overflow
show_debug_message((0xFFFFFFFFFFFFFF01 == __hexTo64Bit("0xFFFFFFFFFFFFFF01")) ? "Success" : $"{0xFFFFFFFFFFFFFF01} != {__hexTo64Bit("0xFFFFFFFFFFFFFF01")}");
// 30. Positive Out of Range
show_debug_message((0x123456789ABCDEF0 == __hexTo64Bit("0x123456789ABCDEF0")) ? "Success" : $"{0x123456789ABCDEF0} != {__hexTo64Bit("0x123456789ABCDEF0")}");
// 31. Upper Half Range Positive
show_debug_message((0xF0F0F0F0F0F0F0F0 == __hexTo64Bit("0xF0F0F0F0F0F0F0F0")) ? "Success" : $"{0xF0F0F0F0F0F0F0F0} != {__hexTo64Bit("0xF0F0F0F0F0F0F0F0")}");
// 32. Large Negative Beyond Upper Bound
show_debug_message((0xFFFFFFFFFFFFFFFF == __hexTo64Bit("0xFFFFFFFFFFFFFFFF")) ? "Success" : $"{0xFFFFFFFFFFFFFFFF} != {__hexTo64Bit("0xFFFFFFFFFFFFFFFF")}");
function __binaryTo64Bit(_binaryString) {
// Strip the "0b" prefix and underscores
_binaryString = string_replace_all(string_delete(_binaryString, 1, 2), "_", "");
// Initialize high and low 32-bit parts
var highValue = 0;
var lowValue = 0;
var len = string_length(_binaryString);
// Parse binary manually and split into high and low 32-bit parts
for (var i = 0; i < len; i++) {
var _char = string_char_at(_binaryString, i + 1);
var digit;
if (_char == "0") {
digit = 0;
} else if (_char == "1") {
digit = 1;
} else {
throw "Invalid character in binary string: " + _char;
}
if (len - i > 32) { // Assign to highValue if in the first 32 bits
highValue = (highValue << 1) | digit;
} else { // Assign to lowValue for the remaining bits
lowValue = (lowValue << 1) | digit;
}
}
// Apply 2's complement for highValue if needed
if (highValue >= 0x80000000) {
highValue -= 0x100000000; // Convert to signed 32-bit
}
// Combine high and low parts into a 64-bit signed integer
var result = highValue * 0x100000000 + lowValue;
return result;
}
function __hexTo64Bit(_hexString) {
// Strip the "0x" prefix and underscores
_hexString = string_replace_all(string_delete(_hexString, 1, 2), "_", "");
// Initialize the high and low 32-bit parts
var highValue = 0;
var lowValue = 0;
var len = string_length(_hexString);
// Parse hex manually and split into high and low 32-bit parts
for (var i = 0; i < len; i++) {
var _char = ord(string_char_at(_hexString, i + 1));
var digit;
if (_char >= ord("0") && _char <= ord("9")) {
digit = _char - ord("0");
} else if (_char >= ord("a") && _char <= ord("f")) {
digit = _char - ord("a") + 10;
} else if (_char >= ord("A") && _char <= ord("F")) {
digit = _char - ord("A") + 10;
} else {
throw "Invalid character in hex string: " + chr(_char);
}
if (len - i > 8) { // Assign to highValue if in the first 8 hex digits
highValue = (highValue << 4) | digit;
} else { // Assign to lowValue for the last 8 hex digits
lowValue = (lowValue << 4) | digit;
}
}
// Apply 2's complement for highValue if needed
if (highValue >= 0x80000000) {
highValue -= 0x100000000; // Convert to signed 32-bit
}
// Combine high and low parts into a 64-bit signed integer
var result = highValue * 0x100000000 + lowValue;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment