Skip to content

[Cookbook] Num to Hex

Camilo edited this page Aug 21, 2021 · 1 revision

You can convert a Number to an Hexadecimal representation using this simple snippet.

var HEX = ["0", "1", "2", "3", "4", "5", "6", "7",
           "8", "9" ,"a", "b", "c", "d", "e", "f"]

class Str {  
  static hex(number) {
    var string = ""
    while(number != 0) {
      string = HEX[number & 0xF] + string
      number = number >> 4
    }
    return "0x%(string)"
  }
}


System.print(Str.hex(1024))