• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

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