Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Convert hex string to an integer
- * Credits: https://forums.electricimp.com/discussion/comment/2877/#Comment_2877
- * @param hex The hex to convert.
- * @return int
- */
- function hexToInteger (hex) {
- local result = 0;
- local shift = hex.len() * 4;
- // For each digit..
- for (local d = 0; d < hex.len(); d++) {
- local digit;
- // Convert from ASCII Hex to integer
- if (hex[d] >= 0x61)
- digit = hex[d] - 0x57;
- else if (hex[d] >= 0x41)
- digit = hex[d] - 0x37;
- else
- digit = hex[d] - 0x30;
- // Accumulate digit
- shift -= 4;
- result += digit << shift;
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment