IF condition with hex values

Just starting out? Need help? Post your questions and find answers here.
PicardDontoro
New User
New User
Posts: 5
Joined: Fri Aug 06, 2004 2:55 pm

IF condition with hex values

Post 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
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post 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
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post 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
PicardDontoro
New User
New User
Posts: 5
Joined: Fri Aug 06, 2004 2:55 pm

Post 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]
PicardDontoro
New User
New User
Posts: 5
Joined: Fri Aug 06, 2004 2:55 pm

Post by PicardDontoro »

Thanks freedimension,

I understand perfectly that you say, so how I can do it ?

(read my previuous reply)
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post 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
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post 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.
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post 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.
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post 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 :wink:

@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.
Post Reply