Page 1 of 1
IF condition with hex values
Posted: Fri Aug 06, 2004 3:18 pm
by PicardDontoro
Hi,
I've this code:
Code: Select all
test1.w = $FFFF
If test1 = $FFFF
Debug "test1 = $ffff"
Else
Debug "test1 <> $ffff"
EndIf
test2.b = $FF
If test2 = $FF
Debug "test2 = $ff"
Else
Debug "test2 <> $ff"
EndIf
test3.l = $FFFFFFFF
If test3 = $FFFFFFFF
Debug "test3 = $ffffffff"
Else
Debug "test3 <> $ffffffff"
EndIf
test4.w = $1234
If test4 = $1234
Debug "test4 = $1234"
Else
Debug "test4 <> $1234"
EndIf
Why IF condition for test1 and test2 don't work ?
TIA
Posted: Fri Aug 06, 2004 3:39 pm
by GreenGiant
The value you're putting into the variable is bigger than that type of variable can hold. Look under 'Variables, Types and Operators' in the manual
Posted: Fri Aug 06, 2004 4:06 pm
by freedimension
GreenGiant wrote:The value you're putting into the variable is bigger than that type of variable can hold. Look under 'Variables, Types and Operators' in the manual
Nope, that isn't the reason. You could have seen this also if you'd had a look at the code.
The reason is simple: $FF and $FFFF are longs and the Long-values for them are different then the Byte- and Word-Values.
With the comparison, all values are internally converted to Long.
Code: Select all
a.b = $FF
b.w = $FFFF
Debug $FF
Debug a
Debug $FFFF
Debug b
Posted: Fri Aug 06, 2004 4:13 pm
by PicardDontoro
GreenGiant wrote:The value you're putting into the variable is bigger than that type of variable can hold. Look under 'Variables, Types and Operators' in the manual
OK, thanks for your reply.
I know that you say, but I work on memory raw data.
Here a simply example:
Code: Select all
If PeekW(?mydata) = $FFFF
Debug "=$"+RSet(Hex(PeekW(?mydata)),4,"0")
Else
Debug "<>$"+RSet(Hex(PeekW(?mydata)),4,"0")
EndIf
DataSection
mydata:
Data.w $FFFF
EndDataSection
DataSection is a data raw file that I load in an allocated memory and I must verify if a specified memory address contains the word $ffff.
[/code]
Posted: Fri Aug 06, 2004 4:19 pm
by PicardDontoro
Thanks freedimension,
I understand perfectly that you say, so how I can do it ?
(read my previuous reply)
Posted: Fri Aug 06, 2004 4:23 pm
by GreenGiant
Nope, that isn't the reason. You could have seen this also if you'd had a look at the code
The fact is that $FF equals 255 and was being put into byte variable which can only handle a value up to 127. Similarly, $FFFF is 65535 and was being put into a word variable which can only hold numbers up to 32767. If you alter the hexidecimal values to be within the range of the variable type, then it will work.
Code: Select all
a.b = $F
b.w = $FFF
Debug $F
Debug a
Debug $FFF
Debug b
Posted: Fri Aug 06, 2004 4:32 pm
by freedimension
PicardDontoro wrote:Thanks freedimension,
I understand perfectly that you say, so how I can do it ?
(read my previuous reply)
Only use Longs for that and treat them like Bytes, that should work. If you run into trouble don't hesitate to ask further questions.
Posted: Fri Aug 06, 2004 5:24 pm
by GreenGiant
Here, this is how to make your code work
Code: Select all
If PeekL(?mydata) = $FFFF
Debug "=$"+RSet(Hex(PeekW(?mydata)),4,"0")
Else
Debug "<>$"+RSet(Hex(PeekW(?mydata)),4,"0")
EndIf
DataSection
mydata:
Data.l $FFFF
EndDataSection
This works because a long is big enough to fit the $FFFF value in.
Posted: Fri Aug 06, 2004 5:46 pm
by freedimension
GreenGiant wrote:Here, this is how to make your code work
Code: Select all
If PeekL(?mydata) = $FFFF
Debug "=$"+RSet(Hex(PeekW(?mydata)),4,"0")
Else
Debug "<>$"+RSet(Hex(PeekW(?mydata)),4,"0")
EndIf
DataSection
mydata:
Data.l $FFFF
EndDataSection
This works because a long is big enough to fit the $FFFF value in.
:roll: You could also put some Delay(100) into the code to make it even more slow
@PicardDontoro: Just don't use Words or Bytes, they are hardly ever needed (99% of my variables are surely Longs). Mostly they are needed for compatibility reasons with the WinAPI or to save memory in large Arrays.