| 02-02-2004, 05:20 PM | #1 |
I'm having problems with this code, which is supposed to convert a hexadecimal byte to a decimal value: Code:
function HexToDec takes string b returns integer
local string charmap = "0123456789abcdef"
local integer i = 1
loop
exitwhen SubString( b, 1, 2 ) == SubString( charmap, i, i + 1 ) or i > 16
set i = i + 1
endloop
if i > 16 then
set i = 256
endif
local integer x = i
set i = 1
loop
exitwhen SubString( b, 2, 3 ) == SubString( charmap, i, i + 1 ) or i > 16
set i = i + 1
endloop
return x + i
endfunctionWhen I try to save the map with this code (no code calls this code) worldedit tells me this about my code: Expected 'endif' - This points to the row local integer x = i , right after the endif in my code Expected a name - This points to the row return x + i Any clues to why this happens and how to fix it are appreciated (as well as ways to make the code smaller). |
| 02-02-2004, 06:52 PM | #2 |
heh, simple Jass only allows declerations of local variables in the top of the function, before any other code (except initialization-related-code). That is the cause of both the errors. So just move the "local integer x" to the top of the script, set it to "i" at the spot it's currently at...and voila, it should work. Cubasis |
| 02-02-2004, 11:30 PM | #3 |
also jass supports direct conversion between hex and dec by using the 0x preface before your hex number 0xff==255 |
| 02-03-2004, 01:58 PM | #4 |
Thanks a lot for the help. However now I'm having other problems with the code. I get very weird results when I send data to it. Sending a null string I get 272 (256 + 16, expected this, will put some kind of error check for this) Sending one letter (any) gives 16. Sending two letters gives 0. The updated code (after the help) looks like this: Code:
function HexToDec takes string b returns integer
local string charmap = "0123456789abcdef"
local integer i = 1
local integer x = 1
loop
exitwhen SubString( b, 1, 1 ) == SubString( charmap, i, i ) or i > 16
call DisplayTextToForce( GetPlayersAll(), SubString( charmap, i, i ) )
set i = ( i + 1 )
endloop
set i = i - 1
if i > 16 then
set i = 16
endif
set x = ( i * 16 )
set i = 1
loop
exitwhen SubString( b, 2, 2 ) == SubString( charmap, i, i ) or i > 16
set i = i + 1
endloop
set i = i - 1
return ( i + x )
endfunctionHelp is as always greatly appreciated. |
| 02-03-2004, 02:47 PM | #5 |
Hmm, your code is heavily messy. But that's allright, as every bug you fix gives you more knowledge about JASS. Firstly, the SubString native is not the same as the SubStringBJ that GUI uses. I reccomend you to either learn what SubStringBJ does and why, or just use SubStringBJ. Basicly, chars in strings are zero-based, so it starts at 0. Also, while SubStringBJ returns both the beginning char and ending char (allowing you to call it with SubStringBJ( 1, 1 )), SubString returns all characters from and with the starting char "Too" the ending char (not with). So to use SubString in your example, you'd have to do sumtin like this: SubString( 0, 1 ), This would return the first char of the string. So overall... use SubStringBJ unless you're sure you know the other one well enough. Secondly, in that same first loop, why in hell do you add 33 to i?... i am sure you mean: set i = i + 1, like you do in the second loop, but this bug causes the first loop to not do it's job, cause it instantly finishes the "i > 16" condition. Anyhow, it should now run alright i guess if you fix all SubString references and stuff. But i still don't understand why you just don't use JASS's internal HEX convertor or use one of the Jass Vault pre-made functions. As i bet they're more optimized than this. Anyhow Cubasis |
