HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Rawcode2Integer - Integer2Rawcode?

10-09-2008, 06:28 AM#1
BoNe
Okay, I've stumbled across a problem.

I've managed a Rawcode2Integer script:

Collapse JASS:
function Rawcode2Integer takes integer r1, integer r2, integer r3, integer r4 returns integer
    return R2I(r1 * Pow(256, 3) + r2 * Pow(256, 2) + r3 * Pow(256, 1) + r4 * Pow(256, 0))
endfunction

But, I don't know if it would work (arithmetic is right?) and I don't know how I would do the opposite to this (Integer2Rawcode).

I would appreciate any help.

EDIT: Oh yeah, for those people that want to know, r1,r2,r3 and r4 are just the ASCII integer in a raw code.

So basically an example of use will be:

Collapse JASS:
call Rawcode2Integer('A','0','0','0')

EDIT2: I should also mention that this is simply turning base 256 (Wc3 Raw Code base) into base 10. The main problem is turning base 10 into base 256; I'm not sure how I would do it in JASS.
10-09-2008, 06:47 AM#2
Tide-Arc Ephemera
I was about to say "the opposite of powers is rooting" but then quickly realized WC3 doesn't support anything other than square roots... also I thought WC3 used hex like every other sanely made object, oh wells I'm probably wrong.
10-09-2008, 06:52 AM#3
BoNe
Heh, I'm pretty sure it's not hex. I had a lengthy chat with Earth-Fury about this.

EDIT: After testing, I found out that the arithmetic is slightly off. Can somebody see the solution?

EDIT2: After testing further, I've found that they were all wrong by 48. To solve the problem I just added a + 48 to the end of the code. Does anybody know why I must do this?
10-09-2008, 08:35 AM#4
ToukoAozaki
Quote:
Collapse JASS:
function Rawcode2Integer takes integer r1, integer r2, integer r3, integer r4 returns integer
    return R2I(r1 * Pow(256, 3) + r2 * Pow(256, 2) + r3 * Pow(256, 1) + r4 * Pow(256, 0))
endfunction

A few things about the code.
1. You should avoid using Pow(), since it's slow. Also, using Pow() disqualifies for a 4-digit 256-base code (see #2). Use constants directly instead. (65536, or 256*256 instead of Pow(256,2))
2. You're using too large value for 32-bit floating point representation. Any integer value above 16777215 is NOT be guaranteed to be precisely represented via floating point representation (a.k.a. real in JASS). Note that any digit will never be 0 in an appropriate rawcode (since ASCII value of 0 represents a null character). This means 256^3 (16777216) digit is NOT 0. As a result, you will always get a value greater than 16777216, which is above the acceptable upper-bound for integer precision.
Due to this issue, your function yielded inappropriate results.

Collapse JASS:
function Rawcode2Integer takes integer r1, integer r2, integer r3, integer r4 returns integer
    return r1 * 16777216 + r2 * 65536 + r3 * 256 + r4
endfunction

With the revised function above, the expression 'A000' == Rawcode2Integer('A','0','0','0') is guaranteed to be true.

Reversing the result is not a difficult thing. The problem is, you cannot have four return values. Following are possible choices:
1. using a temporary global integer array for result
2. using a string for result (delimiters needed)
3. other possible ways

Once this gets clear, I can help you further.
10-09-2008, 09:30 AM#5
BoNe
Hmm, you're right. Thanks for that. I'm thinking of using strings for the reverse, as it fits with what I'm trying to do.
10-09-2008, 09:34 AM#6
dead_or_alivex
Check out this link (the snippet at the bottom in particular).
10-09-2008, 11:49 AM#7
ToukoAozaki
Collapse JASS:
function Integer2Rawcode takes integer cd returns string
    return I2S(ModuloInteger(cd/16777216,256))+","+I2S(ModuloInteger(cd/65536,256))+","+I2S(ModuloInteger(cd/256,256))+","+I2S(ModuloInteger(cd,256))
endfunction

This would be the complete function. Value of each 256-base digits is delimited by ','.
ex)
Understanding of "74,80,68,66":
digits74806866
value256^3256^2256^1256^0

All it does is just converting number bases. This is the same way dead_or_alivex posted.
10-09-2008, 11:52 AM#8
Tide-Arc Ephemera
Just wondering but... As cool as this is, what's could it possibly be useful for?
10-09-2008, 12:14 PM#9
Blubb-Tec
I made something similar once, in order to make a command and conquer like building system, where you buy a building and have it then 'in-stock' as an item ready to place it somewhere on your map.
I then had a dummy-unit you could buy, and at the end of the dummy's name I put the rawcode for the equivalent item. So I had dummies like:
BuildingDummy I000
BuildingDummy I001
And when a dummy was bought, I simply converted those 4 characters into the rawcode ID for an item.
Attached testmap only uses StringToIntcode, this one:
Expand JASS:
Attached Files
File type: w3xcnc-building-system.w3x (24.0 KB)
10-09-2008, 01:39 PM#10
BoNe
@ToukoAozaki; Thank you so much for your help, I appreciate it.
10-09-2008, 02:07 PM#11
ADOLF
1) all native function takes rawcode as integer (CreateUnit takes player id, integer unitid, real x, real y, real face returns unit)

// or string (lightning as exsample)

2) int==rawcode... 'Hpal' - just another notation. or use return bug)

3) if your rawcode are string... i do not know what for it. show all code or explain what for it is necessary to do by string

read too http://www.wc3campaigns.net/showthread.php?t=99954
10-09-2008, 02:17 PM#12
Tide-Arc Ephemera
Quote:
Originally Posted by ADOLF
3) if your rawcode are string... i do not know what for it. show all code or explain what for it is necessary to do by string
S2I("Put string here")
Also you could use the string codes for debugging purposes I guess... like, detecting what abilities are cast and when 'n' stuffs.
10-09-2008, 02:23 PM#13
Vexorian
Quote:
Collapse JASS:
function Integer2Rawcode takes integer cd returns string
    return I2S(ModuloInteger(cd/16777216,256))+","+I2S(ModuloInteger(cd/65536,256))+","+I2S(ModuloInteger(cd/256,256))+","+I2S(ModuloInteger(cd,256))
endfunction
That would generate totally random stuff...
10-09-2008, 02:31 PM#14
BoNe
Quote:
Originally Posted by ADOLF
1) all native function takes rawcode as integer (CreateUnit takes player id, integer unitid, real x, real y, real face returns unit)

I know it takes an integer, but the integer doesn't have to be specifically the classic raw code, and the whole point of this is to make sure I can use base 256 as my unit ID's. For example,

call CreateUnit(Player(0), 'h000', 0.0, 0.0, 0.0)

is equal to...

call CreateUnit(Player(0), 1747988528, 0.0, 0.0, 0.0)

Quote:
Originally Posted by ADOLF
2) int==rawcode... 'Hpal' - just another notation. or use return bug)

Quote:
Originally Posted by ADOLF
3) if your rawcode are string... i do not know what for it. show all code or explain what for it is necessary to do by string

What do you mean?

EDIT: Vexorian, I will test that code now.
EDIT2: It actually doesn't work as it is supposed to, but the numbers arn't random.
10-09-2008, 09:57 PM#15
ToukoAozaki
Quote:
Originally Posted by BoNe
EDIT2: It actually doesn't work as it is supposed to, but the numbers arn't random.

As far as I tested, it successfully returns ASCII values. Please report how it behaves wrong so I can help.