• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

HexFormat

JASS:
library HexFormat/* v1.0
*************************************************************************************
*
*    Allows conversion of integers to hex strings and vice versa.
*
*************************************************************************************
*
*    */ uses /*
*
*    */ Table /* hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
*
*************************************************************************************
*
*    API
*
*    function I2Hex takes integer i returns string
*        - converts integer to hexadecimal string
*
*    function Hex2I takes string shex returns integer
*        - converts hexadecimal string to integer
*
*************************************************************************************
*
*    Credits
*
*    Bribe - Table
*
*************************************************************************************/
    globals
        private Table hex
    endglobals
    
    function I2Hex takes integer i returns string
        local string result = hex.string[i]
        local string negative = ""
        local integer ti = i
        local integer i2 = 0
        if i < 0 then
            set negative = "-"
        endif
        if result == null then
            loop
                set i2 = ModuloInteger(i, 16)
                set i = i/16
                set result = hex.string[i2] + result
                exitwhen i == 0
            endloop
            set result = negative + result
            set hex.string[ti] = result
            set hex[StringHash(result)] = ti
        endif
        return result
    endfunction
    
    function Hex2I takes string shex returns integer
        local integer result = hex[StringHash(shex)]
        local integer i = 0
        local integer len
        local string s
        if 0 == result then
            set len = StringLength(shex)
            loop
                set s = SubString(shex, i, i + 1)
                set result = result + (hex[StringHash(s)] * R2I(Pow(16, len - (i + 1))))
                set i = i + 1
                exitwhen i == len
            endloop
            set hex.string[result] = shex
            set hex[StringHash(shex)] = result
        endif
        return result        
    endfunction
    
    private module Initializer
        private static method onInit takes nothing returns nothing
            call init()
        endmethod
    endmodule
    
    private struct HF extends array
        static method init takes nothing returns nothing
            local integer i = 0
            set hex = Table.create()
            set hex.string[0] = "0"
            set hex.string[1] = "1"
            set hex.string[2] = "2"
            set hex.string[3] = "3"
            set hex.string[4] = "4"
            set hex.string[5] = "5"
            set hex.string[6] = "6"
            set hex.string[7] = "7"
            set hex.string[8] = "8"
            set hex.string[9] = "9"
            set hex.string[10] = "A"
            set hex.string[11] = "B"
            set hex.string[12] = "C"
            set hex.string[13] = "D"
            set hex.string[14] = "E"
            set hex.string[15] = "F"
            loop
                set hex[StringHash(hex.string[i])] =i
                set i = i + 1
                exitwhen i == 16
            endloop
        endmethod
        
        implement Initializer
    endstruct
endlibrary

Demo:
JASS:
struct Tester extends array
    static method onInit takes nothing returns nothing
        local integer i = 0
        loop
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, I2Hex(i))
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, I2S(Hex2I(I2Hex(i))))
            set i = i + 1
            call TriggerSleepAction(0.1)
            exitwhen i == 255
        endloop
    endmethod
endstruct
 
Top