Jump to content
Sign in to follow this  
bgolab

Unsigned variables

Recommended Posts

bgolab

Hi All,

I have a code fragment in C and I want to convert it to AutoIt:

table is unsigned char

================

unsigned int x, y, z;

y = key;

x=0

for (i = 0; i<3; i++) {

x = (x & 0xffffff00) | table;

y ^= x;

y += x;

x <<= 8;

y ^= x;

x <<= 8;

y -= x;

x <<= 8;

y ^= x;

z = y & 0x1f;

y = (y << z) | (y >> (32 - z));

}

return y;

It works fine in C but there is a problem in AutoIt. During bit operation (i.e. BitXOR which represents ^) some variables like y (declared in C as unsigned) are treated as signed in AutoIt.

How to fix it in AutoIt?

Bogdan

Share this post


Link to post
Share on other sites
PsaltyDS

Hi All,

I have a code fragment in C and I want to convert it to AutoIt:

It works fine in C but there is a problem in AutoIt. During bit operation (i.e. BitXOR which represents ^) some variables like y (declared in C as unsigned) are treated as signed in AutoIt.

How to fix it in AutoIt?

Bogdan

You didn't show your AutoIt code, so there's no telling what you did wrong. BitXOR works fine:

$y = 0x0FF
$x = 0xFFFFFF00
$z = BitXOR($x, $y)
$sAns = "0x" & Hex($z, 8)
MsgBox(64, "Answer", $sAns)

Can you post some AuotIt code that gives an unexpected answer?

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
bgolab

You didn't show your AutoIt code, so there's no telling what you did wrong. BitXOR works fine:

$y = 0x0FF
$x = 0xFFFFFF00
$z = BitXOR($x, $y)
$sAns = "0x" & Hex($z, 8)
MsgBox(64, "Answer", $sAns)

Can you post some AuotIt code that gives an unexpected answer?

:P

The code in AutoIT

$y=$key

$x=0

For $i=0 To 2 Step 1

$xx=BitAND($x,0xffffff00)

$x=BitOR($xx, Asc($znak[$i]))

$y=BitXOR($y, $x)

$y=$y+$x

$x=BitShift($x,-8)

$y=BitXOR($y, $x)

$x=BitShift($x,-8)

$y=$y-$x

$x=BitShift($x,-8)

$y=BitXOR($y, $x)

$z=BitAND($y,0x1F)

$y1=BitShift($y,-$z)

$y2=BitShift($y,32-$z)

$y=BitOR($y1, $y2)

Next

Return $y

Share this post


Link to post
Share on other sites
SkinnyWhiteGuy

I've had the same problem before, answer for me was right before returning, convert your number $y to hex using the builtin Hex() function, then use the following function to convert that into an unsigned number:

Func Hex2Dec($hex)
    Local $dec = 0, $i
    For $i = 0 To StringLen($hex) - 1
        $dec += Dec(StringMid($hex, StringLen($hex)-$i,1)) * (16 ^ $i)
    Next
    Return $dec
EndFunc

Share this post


Link to post
Share on other sites
PsaltyDS

I've had the same problem before, answer for me was right before returning, convert your number $y to hex using the builtin Hex() function, then use the following function to convert that into an unsigned number:

Hmm... Is this what you expected from that?

$x = 0xFFFFFF00
ConsoleWrite("Debug: $x = " & $x & @LF) ; = -256
$y = Hex2Dec($x)
ConsoleWrite("Debug: $y = " & $y & @LF) ; = 598

Func Hex2Dec($hex)
    Local $dec = 0, $i
    For $i = 0 To StringLen($hex) - 1
        $dec += Dec(StringMid($hex, StringLen($hex)-$i,1)) * (16 ^ $i)
    Next
    Return $dec
EndFunc

:P

Edit: Oops, I see it now. Have to pass string representation by changing the first line to: $x = Hex(0xFFFFFF00, 8)

Then I get $x = $x = FFFFFF00; $y = 4294967040.

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
bgolab

Hmm... Is this what you expected from that?

$x = 0xFFFFFF00
ConsoleWrite("Debug: $x = " & $x & @LF) ; = -256
$y = Hex2Dec($x)
ConsoleWrite("Debug: $y = " & $y & @LF) ; = 598

Func Hex2Dec($hex)
    Local $dec = 0, $i
    For $i = 0 To StringLen($hex) - 1
        $dec += Dec(StringMid($hex, StringLen($hex)-$i,1)) * (16 ^ $i)
    Next
    Return $dec
EndFunc

:P

Edit: Oops, I see it now. Have to pass string representation by changing the first line to: $x = Hex(0xFFFFFF00, 8)

Then I get $x = $x = FFFFFF00; $y = 4294967040.

---

I'll try it. I suspect a problem with "y -= x;" because signed operation /AutoIt/ involves a correction (probably nased on CPU's "overflow" bit).

Maybe it is a good idea to simulate CPU operation using Autoit in this case. I mean generic summer and substractor:) Just joking...

Anyway, unsigned variables seem to useful even in AutoIt...

Bogdan

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×