• 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.

StringCharByte

JASS:
library StringCharByte/* v1.0
*************************************************************************************
*
*    Allows you to retrieve the byte of a character or
*    retrieve a character of a byte
*
*************************************************************************************
*
*  */ requires /*
*
*   */Table/* hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
*
*************************************************************************************
*
*    API
*
*    struct StringCharByte extends array
*
*        static method byte takes string s returns integer
*            - retrieves the byte of a character
*
*        static method char takes integer byte returns string
*            - retrieves the character of a byte
*
*************************************************************************************
*
*    Credits
*
*    Bribe - Table
*
*************************************************************************************/
    private module SCBInit
        private static method onInit takes nothing returns nothing
            call init()
        endmethod
    endmodule
    struct StringCharByte extends array
        private static Table tb
        static method byte takes string s returns integer
            return tb[StringHash(SubString(s, 0, 1))]
        endmethod
        static method char takes integer byte returns string
            return tb.string[byte]
        endmethod
        
        private static method init takes nothing returns nothing
            local string chars = /*
            */"!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" +/*
            */"[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¦ÇüéâäàåçêëèïîìÄÅÉæÆôö" +/*
            */"òûùÿÖÜ¢£¥PƒáíóúñѪº¿¬¬½¼¡«»"
            local integer start = 33
            local integer end = StringLength(chars)
            local integer i = 0
            local string s = ""
            set tb = Table.create()
            loop
                set s = SubString(chars, i, i + 1)
                set tb[StringHash(s)] = start + i
                set tb.string[start + i] = s
                exitwhen i == end
                set i = i + 1
            endloop
            set tb[StringHash("ß")] = 225
            set tb.string[225] = "ß"
            set tb[StringHash("µ")] = 230
            set tb.string[230] = "µ"
            set tb[StringHash("±")] = 241
            set tb.string[241] = "±"
            set tb[StringHash("÷")] = 246
            set tb.string[246] = "÷"
            set tb[StringHash("˜")] = 247
            set tb.string[247] = "˜"
            set tb[StringHash("°")] = 248
            set tb.string[248] = "°"
            set tb[StringHash("·")] = 249
            set tb.string[249] = "·"
        endmethod
        
        implement SCBInit
    endstruct
endlibrary

Demo code:
JASS:
struct Tester extends array
    static method onInit takes nothing returns nothing
        call BJDebugMsg(I2S(StringCharByte.byte("hahaha"))) // 104
        call BJDebugMsg(StringCharByte.char(104)) // h
    endmethod
endstruct
 
Top