Snippet Ascii

Nestharus

o-o
Reaction score
84
Here's the new script with proper order of initialization, 2 new functions, and lots of debug. Bribe started it by just making the module, adding the 2 new functions, and adding a couple of comments, I finished it by fixing his initializer, syntax errors, adding a header, and adding all of the debug statements. A2S and S2A (the 2 new functions) were ripped from StringParser.

JASS:

library Ascii
///////////////////////////////////////////////////////////////////
//      function Char2Ascii takes string s returns integer
//          integer ascii = Char2Ascii("F")
//
//      function Ascii2Char takes integer a returns string
//          string char = Ascii2Char('F')
//
//      function A2S takes integer a returns string
//          string rawcode = A2S('CODE')
//
//      function S2A takes string s returns integer
//          integer rawcode = S2A("CODE")
//
///////////////////////////////////////////////////////////////////
    globals
        private integer array i // Integers
        private string array c // Characters
    endglobals

    function Char2Ascii takes string s returns integer
        local integer a = i[StringHash(s) / 0x1F0748 + 0x3EA]
        if a == 47 and s == "\\" then
            set a = 92 // "/" and "\\" have same hash
        elseif a >= 65 and a <= 90 and s != c[a] then
            set a = a+32 //shift to lowercase, upper/lower have same hash
        endif
        
        debug if (((a >= 8 and a <= 13) or (a >= 32 and a <= 126))) then
            return a
        debug endif
        debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "ASCII CONVERSION ERROR: INVALID CHAR " + s)
        debug return 0
    endfunction

    function Ascii2Char takes integer a returns string
        debug if ((a >= 8 and a <= 13) or (a >= 32 and a <= 126)) then
            return c[a]
        debug endif
        debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "ASCII CONVERSION ERROR: INVALID INT " + I2S(a))
        debug return null
    endfunction

    function A2S takes integer a returns string
        local string s = ""
        debug if (a >= 8) then
            loop
                set s = s + c[a - a / 128 * 128]
                set a = a / 128
                exitwhen (a == 0)
            endloop
            return s
        debug endif
        debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "ASCII CONVERSION ERROR: INVALID INT " + I2S(a))
        debug return null
    endfunction

    function S2A takes string s returns integer
        local integer a = 0
        local integer l = StringLength(s)
        debug if (l > 0) then
            loop
                set l = l - 1
                set a = a * 128 + Char2Ascii(SubString(s, l, l + 1))
                exitwhen (l == 0)
            endloop
            return a
        debug endif
        debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "ASCII CONVERSION ERROR: INVALID STR " + s)
        debug return 0
    endfunction

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

    private struct Inits extends array
        implement Init
    endstruct
endlibrary



Can someone update the first post to have this script instead?

Tx.
 

emjlr3

Change can be a good thing
Reaction score
395
this already exists, just not in submission ready form

the real trick is getting from a ascii string back to the ability rawcode:

JASS:

globals
    private string array ASCII
    private integer start=48
    private integer end=122
endglobals
function Convert takes integer String returns nothing
    local string s=""
    local integer array count
    local integer array Base
    local integer array Char
    set Base[1]=String   
    
    if String!=0 then
        set ASCII[48]="0"
        set ASCII[49]="1"
        set ASCII[50]="2"
        set ASCII[51]="3"
        set ASCII[52]="4"
        set ASCII[53]="5"
        set ASCII[54]="6"
        set ASCII[55]="7"
        set ASCII[56]="8"
        set ASCII[57]="9"
        
        set ASCII[65]="A"
        set ASCII[66]="B"
        set ASCII[67]="C"
        set ASCII[68]="D"
        set ASCII[69]="E"
        set ASCII[70]="F"
        set ASCII[71]="G"
        set ASCII[72]="H"
        set ASCII[73]="I"
        set ASCII[74]="J"
        set ASCII[75]="K"
        set ASCII[76]="L"
        set ASCII[77]="M"
        set ASCII[78]="N"
        set ASCII[79]="O"
        set ASCII[80]="P"
        set ASCII[81]="Q"
        set ASCII[82]="R"
        set ASCII[83]="S"
        set ASCII[84]="T"
        set ASCII[85]="U"
        set ASCII[86]="V"
        set ASCII[87]="W"
        set ASCII[88]="X"
        set ASCII[89]="Y"
        set ASCII[90]="Z"
        
        set ASCII[97]="a"
        set ASCII[98]="b"
        set ASCII[99]="c"
        set ASCII[100]="d"
        set ASCII[101]="e"
        set ASCII[102]="f"
        set ASCII[103]="g"
        set ASCII[104]="h"
        set ASCII[105]="i"
        set ASCII[106]="j"
        set ASCII[107]="k"
        set ASCII[108]="l"
        set ASCII[109]="m"
        set ASCII[110]="n"
        set ASCII[111]="o"
        set ASCII[112]="p"
        set ASCII[113]="q"
        set ASCII[114]="r"
        set ASCII[115]="s"
        set ASCII[116]="t"
        set ASCII[117]="u"
        set ASCII[118]="v"
        set ASCII[119]="w"
        set ASCII[120]="x"
        set ASCII[121]="y"
        set ASCII[122]="z"
    
        set Char[1]=ModuloInteger(Base[1], 256)
        set Base[2]=(Base[1]-Char[1])/256
        set Char[2]=ModuloInteger(Base[2], 256)
        set Base[3]=(Base[2]-Char[2])/256
        set Char[3]=ModuloInteger(Base[3], 256)
        set Base[4]=(Base[3]-Char[3])/256
        set Char[4]=ModuloInteger(Base[4], 256)
    
        set count[1]=1
        loop
            exitwhen count[1]==5
            set count[2]=start
            loop
                if Char[count[1]]==count[2] then
                    set s=s+ASCII[count[2]]
                    exitwhen true
                elseif count[2]>end then
                    call BJDebugMsg("Error during ASCII base 10 to string conversion.")
                    return
                endif
                set count[2]=count[2]+1
            endloop
            set count[1]=count[1]+1
        endloop
        call BJDebugMsg("ASCII base 10 to string conversion is: "+SubString(s,3,4)+SubString(s,2,3)+SubString(s,1,2)+SubString(s,0,1))
    endif
endfunction


highly doubt this is anything approaching optimized, but its functional, and wouldn't mind some TLC
 

Nestharus

o-o
Reaction score
84
A2S and S2A already do that but better... lol

They were ripped from StringParser, and in StringParser, they were used to convert ascii between string and integer form.
 

emjlr3

Change can be a good thing
Reaction score
395
call Convert(1093753167) = "A1YO"

unless I missed something in your script you don't go that direction...
 

emjlr3

Change can be a good thing
Reaction score
395
yes, but it doesn't convert the base 10 ASCII numerical representation to its characters as provided in the object editor by pressing ctrl+d

what if I did A2S(1093753167) with your system - I would never get the desired "A1YO" output - which my example provides above

i.e. - I2S('A1YO')=1093753167
 

Nestharus

o-o
Reaction score
84
'hpea' can be represented in base 10....

numbers in wc3 can be in base 8, hexadecimal, ascii, or base 10 form, and they can be messed with any which way

You can do 'hpea'+1 :\


When displaying them, it automatically displays them in base 10 form.

So yes, you can do 1093753167.

'A1YO' == 1093753167


You can plug in a number in whatever base you like... if you plugged in 'a', it'd return "a". If you plugged in the ordinal value of 'a', it'd still return "a".
 

Nestharus

o-o
Reaction score
84
Ok, in 2 days, if the first post isn't updated by then, I'm submitting post #21 as a separate resource ;P. This is why I never go to threads and just submit it >.>.
 

tooltiperror

Super Moderator
Reaction score
231
Think about what you're saying.

You are demanding moderators to edit the post of another user to modify the script submitted by another user so that they can use yours instead, when you have created a nearly different resource.

Yeah, makes perfect sense.

protip: 'threteaning' moderators doesn't really work.
 

Nestharus

o-o
Reaction score
84
Wasn't threatening, just getting tired of waiting since TheDamien isn't around ;D.

And the resource I submitted is just TheDamien's Ascii with debug statements and initialization inside of a module initializer + A2S and S2A. The arrays and so on are still the same. The main meat of the work is still the same ;P.

This is why I figured it'd be best to just keep it under the original thread ;P.

If it's better if I just submit it as another resource and have this one graveyarded, I'm good with that too, either way.

I just need to say at the moment that TheDamien's script is unsafe to use for certain resources. For example, if you use it with my Scrambler, you will be flooded with BigInt errors and Scrambler will be totally broken.

So either way it's going to need to be updated with the script I posted. I just figured that it'd be nicer to have it in TheDamien's thread as it's still really his work rather than on a separate thread by me or Bribe.

This is still TheDamien's work as none of his design was recoded. We literally cnp'd his script, moved init into a module initializer, cut the stuff out of StringParser and put it into Ascii, added debug statements, and changed the var names to be short ;P.

I don't like to post separate resources unless I coded them from scratch myself and I think that cnping a resource, making little changes, and then resubmitting it is pretty uncool ;P.


Regardless of the route, the script needs to be updated to the one I put up : | (both Bribe and I worked on it, probably me much more than Bribe).
 

emjlr3

Change can be a good thing
Reaction score
395
if I have a chance I will check it out in game tonight and see - then go from there

from what I have heard, moderation, in the form of approving submitted resources in this section, isn't exactly fast anymore - just be patient
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top