Advertisement
Dimenticare

parsing a hex string

Oct 13th, 2018
1,372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// int hex(string);
  2. // if the script name "hex" collides with a variable or a resource somewhere
  3. // just rename it
  4.  
  5. var result=0;
  6.  
  7. // special unicode values
  8. var ZERO=ord("0");
  9. var NINE=ord("9");
  10. var A=ord("A");
  11. var F=ord("F");
  12.  
  13. for (var i=1; i<=string_length(argument0); i++){
  14.     var c=ord(string_char_at(string_upper(argument0), i));
  15.     // you could also multiply by 16 but you get more nerd points for bitshifts
  16.     result=result<<4;
  17.     // if the character is a number or letter, add the value
  18.     // it represents to the total
  19.     if (c>=ZERO&&c<=NINE){
  20.         result=result+(c-ZERO);
  21.     } else if (c>=A&&c<=F){
  22.         result=result+(c-A+10);
  23.     // otherwise complain
  24.     } else {
  25.         // this will make the browser behave badly but you can leave it in
  26.         // actual game maker if you want
  27.         //show_error("bad input for hex(str)", true);
  28.     }
  29. }
  30.  
  31. return result;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement