Snippet Ascii

Reaction score
333
Provides two functions. Char2Ascii takes a string with one character and returns the ASCII code for that character. Ascii2Char takes an ASCII code and returns a string containing the character for that code.

The return value for Char2Ascii is undefined for invalid inputs and unsupported characters, but will usually be 0. Ascii2Char will simply return null for invalid inputs.

Presumably, the performance of this is better than linear search based solutions.

Here is the code:

JASS:
library Ascii initializer Init
    globals
        private integer array Ints
        private string array Chars
    endglobals

    function Char2Ascii takes string s returns integer
        local integer i = Ints[StringHash(s) / 0x1F0748 + 0x3EA]
        if i == 47 and s == "\\" then
            set i = 92
        elseif i &gt;= 65 and i &lt;= 90 and s != Chars<i> then
            set i = i+32
        endif
        return i
    endfunction

    function Ascii2Char takes integer i returns string
        return Chars<i>
    endfunction

    private function Init takes nothing returns nothing
        set Ints[931] = 8
        set Ints[1075] = 9
        set Ints[1586] = 10
        set Ints[1340] = 12
        set Ints[412] = 13
        
        set Ints[198] = 32
        set Ints[1979] = 33
        set Ints[1313] = 34
        set Ints[1003] = 35
        set Ints[1264] = 36
        set Ints[983] = 37
        set Ints[1277] = 38
        set Ints[306] = 39
        set Ints[904] = 40
        set Ints[934] = 41
        set Ints[917] = 42
        set Ints[1972] = 43
        set Ints[1380] = 44
        set Ints[1985] = 45
        set Ints[869] = 46
        
        set Ints[1906] = 47
        
        set Ints[883] = 48
        set Ints[1558] = 49
        set Ints[684] = 50
        set Ints[582] = 51
        set Ints[668] = 52
        set Ints[538] = 53
        set Ints[672] = 54
        set Ints[1173] = 55
        set Ints[71] = 56
        set Ints[277] = 57
        
        set Ints[89] = 58
        set Ints[1141] = 59
        set Ints[39] = 60
        set Ints[1171] = 61
        set Ints[51] = 62
        set Ints[305] = 63
        set Ints[0] = 64
        
        set Ints[222] = 65
        set Ints[178] = 66
        set Ints[236] = 67
        set Ints[184] = 68
        set Ints[1295] = 69
        set Ints[1390] = 70
        set Ints[1276] = 71
        set Ints[203] = 72
        set Ints[1314] = 73
        set Ints[209] = 74
        set Ints[1315] = 75
        set Ints[170] = 76
        set Ints[1357] = 77
        set Ints[1343] = 78
        set Ints[1397] = 79
        set Ints[1420] = 80
        set Ints[1419] = 81
        set Ints[1396] = 82
        set Ints[1374] = 83
        set Ints[1407] = 84
        set Ints[499] = 85
        set Ints[1465] = 86
        set Ints[736] = 87
        set Ints[289] = 88
        set Ints[986] = 89
        set Ints[38] = 90
        
        set Ints[1230] = 91
        set Ints[1636] = 93
        set Ints[1416] = 94
        set Ints[1917] = 95
        set Ints[217] = 96
        set Ints[833] = 123
        set Ints[1219] = 124
        set Ints[553] = 125
        set Ints[58] = 126
        
        set Chars[8] = &quot;\b&quot;
        set Chars[9] = &quot;\t&quot;
        set Chars[10] = &quot;\n&quot;
        set Chars[12] = &quot;\f&quot;
        set Chars[13] = &quot;\r&quot;
        
        set Chars[32] = &quot; &quot;
        set Chars[33] = &quot;!&quot;
        set Chars[34] = &quot;\&quot;&quot;
        set Chars[35] = &quot;#&quot;
        set Chars[36] = &quot;$&quot;
        set Chars[37] = &quot;%&quot;
        set Chars[38] = &quot;&amp;&quot;
        set Chars[39] = &quot;&#039;&quot;
        set Chars[40] = &quot;(&quot;
        set Chars[41] = &quot;)&quot;
        set Chars[42] = &quot;*&quot;
        set Chars[43] = &quot;+&quot;
        set Chars[44] = &quot;,&quot;
        set Chars[45] = &quot;-&quot;
        set Chars[46] = &quot;.&quot;
        
        set Chars[47] = &quot;/&quot;
        
        set Chars[48] = &quot;0&quot;
        set Chars[49] = &quot;1&quot;
        set Chars[50] = &quot;2&quot;
        set Chars[51] = &quot;3&quot;
        set Chars[52] = &quot;4&quot;
        set Chars[53] = &quot;5&quot;
        set Chars[54] = &quot;6&quot;
        set Chars[55] = &quot;7&quot;
        set Chars[56] = &quot;8&quot;
        set Chars[57] = &quot;9&quot;
        
        set Chars[58] = &quot;:&quot;
        set Chars[59] = &quot;;&quot;
        set Chars[60] = &quot;&lt;&quot;
        set Chars[61] = &quot;=&quot;
        set Chars[62] = &quot;&gt;&quot;
        set Chars[63] = &quot;?&quot;
        set Chars[64] = &quot;@&quot;
        
        set Chars[65] = &quot;A&quot;
        set Chars[66] = &quot;B&quot;
        set Chars[67] = &quot;C&quot;
        set Chars[68] = &quot;D&quot;
        set Chars[69] = &quot;E&quot;
        set Chars[70] = &quot;F&quot;
        set Chars[71] = &quot;G&quot;
        set Chars[72] = &quot;H&quot;
        set Chars[73] = &quot;I&quot;
        set Chars[74] = &quot;J&quot;
        set Chars[75] = &quot;K&quot;
        set Chars[76] = &quot;L&quot;
        set Chars[77] = &quot;M&quot;
        set Chars[78] = &quot;N&quot;
        set Chars[79] = &quot;O&quot;
        set Chars[80] = &quot;P&quot;
        set Chars[81] = &quot;Q&quot;
        set Chars[82] = &quot;R&quot;
        set Chars[83] = &quot;S&quot;
        set Chars[84] = &quot;T&quot;
        set Chars[85] = &quot;U&quot;
        set Chars[86] = &quot;V&quot;
        set Chars[87] = &quot;W&quot;
        set Chars[88] = &quot;X&quot;
        set Chars[89] = &quot;Y&quot;
        set Chars[90] = &quot;Z&quot;
        
        set Chars[92] = &quot;\\&quot;
        
        set Chars[97] = &quot;a&quot;
        set Chars[98] = &quot;b&quot;
        set Chars[99] = &quot;c&quot;
        set Chars[100] = &quot;d&quot;
        set Chars[101] = &quot;e&quot;
        set Chars[102] = &quot;f&quot;
        set Chars[103] = &quot;g&quot;
        set Chars[104] = &quot;h&quot;
        set Chars[105] = &quot;i&quot;
        set Chars[106] = &quot;j&quot;
        set Chars[107] = &quot;k&quot;
        set Chars[108] = &quot;l&quot;
        set Chars[109] = &quot;m&quot;
        set Chars[110] = &quot;n&quot;
        set Chars[111] = &quot;o&quot;
        set Chars[112] = &quot;p&quot;
        set Chars[113] = &quot;q&quot;
        set Chars[114] = &quot;r&quot;
        set Chars[115] = &quot;s&quot;
        set Chars[116] = &quot;t&quot;
        set Chars[117] = &quot;u&quot;
        set Chars[118] = &quot;v&quot;
        set Chars[119] = &quot;w&quot;
        set Chars[120] = &quot;x&quot;
        set Chars[121] = &quot;y&quot;
        set Chars[122] = &quot;z&quot;
        
        set Chars[91] = &quot;[&quot;
        set Chars[93] = &quot;]&quot;
        set Chars[94] = &quot;^&quot;
        set Chars[95] = &quot;_&quot;
        set Chars[96] = &quot;`&quot;
        set Chars[123] = &quot;{&quot;
        set Chars[124] = &quot;|&quot;
        set Chars[125] = &quot;}&quot;
        set Chars[126] = &quot;~&quot;
    endfunction
endlibrary</i></i>


Here is an example usage (converting strings to rawcodes and vice versa):

JASS:
library Example initializer Init requires Ascii
    function String2Rawcode takes string s returns integer
        local integer i = StringLength(s)
        local integer j = 1
        local integer k = 0
        loop
            exitwhen i == 0
            set i = i-1
            set k = k+Char2Ascii(SubString(s, i, i+1))*j
            set j = j*256
        endloop
        return k
    endfunction
    
    function Rawcode2String takes integer i returns string
        local string c = &quot;&quot;
        loop
            exitwhen i == 0
            set c = Ascii2Char(i - (i / 256) * 256)+c
            set i = i / 256
        endloop
        return c
    endfunction
    
    private function Init takes nothing returns nothing
        call BJDebugMsg(I2S(String2Rawcode(&quot;hfoo&quot;))) // Displays &quot;1751543663&quot;
        call BJDebugMsg(I2S(&#039;hfoo&#039;)) // Also displays &quot;1751543663&quot;
        call BJDebugMsg(Rawcode2String(&#039;hfoo&#039;))  // Displays &quot;hfoo&quot;
        call BJDebugMsg(Rawcode2String(&#039;hpea&#039;))  // Displays &quot;hpea&quot;
    endfunction
endlibrary
 

Jesus4Lyf

Good Idea™
Reaction score
397
A_winner_is_you.jpg


I like it. :)
 

Azlier

Old World Ghost
Reaction score
461
Ah, an ASCII snippet that has actual use. Much better than Vestras' ASCII.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
I'm wondering how you have determined the equation and which index of Ints should be used.
But maybe it's too much too explain.

It seems helpful for debugging purposes, any other practical use ?
 

quraji

zap
Reaction score
144
Pretty neat. Did you manually get the readouts of all the hashes? I hope you had fun.
 

Jesus4Lyf

Good Idea™
Reaction score
397
Approved. :thup:

From time to time, being able to convert to ascii is useful, even if only for debugging or doing something the wrong way.

Either way, you're doing it right. This looks like a useful resource. If I ever wanted an ASCII snippet, I'm sure I'd arrive at this one satisfied. O(1) complexity both ways. Well done.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Hmm, i want to display a rawcode 'XXXX' as a string, and not the decimal one.
And to be honest i'm lost with your code, how can i do that ?

You should really post this kind of function.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Well, ok this function is just to get one character and not to convert rawcodes.
I'm really curious about the use, because in my first comment i misunderstood the point of this script.
So i don't see the usefulness, even for debugging purpose.

I use it (base 64) :
http://www.wc3c.net/showthread.php?t=106622

But it's a shame that the object editor allow special characters but not the trigger editor.
Meh i will simply don't use them, anyway it's pretty useless.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Hmm i didn't read the example :banghead:
Well, anyway to be honest BaseConversion is more usefull for me.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
String2Rawcode and Rawcode2String should be included inside the library imho.
Because i imagine only one useful case, display a rawcode as a string.
 

Sickle

New Member
Reaction score
13
None, really. And that is quite enough.

String2Rawcode is nice sometimes, though. Especially for debugging.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Bump.

I use RawCode2String (for debugging purpose), and i'm bored to incorporate this function inside the library each time ...
 

Nestharus

o-o
Reaction score
84
Initializer needs to be put into a module onInit. One of my resources is using this and it's going to be unapproved if this doesn't get fixed soon. I'll have to do a very simple edit and submit it at THW and use mine instead ;|, so please update. Make less work for me and make less work for everyone else ;(.

edit
Nvm, looks like you haven't been active in a long time so someone'll work on the minor update at THW.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top