# And-ing a hex string with a hex value

Hi folks,

A “bit” of confusion here, but I’m trying to compare a hex string with a hex value to set a True / False state. For example

``````  If (Val("&hD31C") And &h4000) = &h4000 Then
// it's True
End If``````

However, I get an error:

``  Type mismatch error. Expected Boolean but got Double``

I know it’s a Saturday and my brain would rather be playing Diablo III, but I didn’t think it was that angry with me

Can someone hit me with the obvious stick on this?

Val() returns a double, so you probably need to cast it to an integer first?

``````dim tmp as integer = Val("&hD31C")
if (tmp And &h4000) = &h4000 then

end if``````

``` if Bitwise.BitAnd ( Val("&hD31C"), &h4000) = &h4000 Then // it's True msgbox "True" End If ```

One thing to consider, Bitwise.BitAnd is about 10x slower than plain “And” so if you are doing high performance code, it’s to be avoided. (But if you are doing high performance code, you shouldn’t be using Val() either… )

I am sorry, I may be mistaken, but from what I understand reading the LR, regular And does not seem suited to perform anything but a boolean evaluation with a boolean result. As far as I see, Tim needs a bitwise And that gives a numeric result to compare to &H4000.

The LR is wrong if that’s what it says:

``````  dim foo as integer = &hF000
dim bar as integer = &hFF00

dim foobar as integer = foo AND bar
MsgBox hex(foobar)``````

Shows “F000

 My LR clearly says it does bitwise operations, e.g. “And: Used to perform a logical comparison of two boolean expressions and a bitwise And comparison of two integer expressions.”

[quote=197473:@Michael Diehr]The LR is wrong if that’s what it says:

``````  dim foo as integer = &hF000
dim bar as integer = &hFF00

dim foobar as integer = foo AND bar
MsgBox hex(foobar)``````

Shows “F000

 My LR clearly says it does bitwise operations, e.g. “And: Used to perform a logical comparison of two boolean expressions and a bitwise And comparison of two integer expressions.”[/quote]

Alright. Thank you. Casting did not work, but your code is probably the solution. Instead of using Val and a literal directly, using variables makes it work :

[code] dim foo as integer = Val("&hD31C")
dim bar as integer = &h4000

if (foo AND bar) = &H4000 then
MsgBox “True”
end if[/code]

Yeah, the compiler has some weirdnesses with this kind of stuff.
For example, CType works:

``MsgBox hex( Ctype(val("&hd31c") , Integer) and &h4000 )``

But Integer casting doesn’t:

``MsgBox hex(  Integer(val("&hd31c"))  and &h4000 )   // won't compile``

Okay, so the obviousness of this one didn’t escape me, it’s truly odd… I had already tried integer casting and when that also didn’t work, I went and played Diablo III :D.

I’m back now and feeling much better.

Thanks guys.

You played Diablo III and feel much better? I had a hardcore character with over 172 hours vested, and died from walking into a situation while my chat window was focused (was in town typing to a friend, and clicked back on the portal to confront a rift guardian). All the key-presses went to the chat field instead of the action bar, in the 3 seconds it took to kill me. By the time I figured out what had happened… too late. I was not feeling better… and actually took to Xojo for the escape. I haven’t touched the game since (that was season one).

[quote=197476:@Michael Diehr]Yeah, the compiler has some weirdnesses with this kind of stuff.
For example, CType works:

``MsgBox hex( Ctype(val("&hd31c") , Integer) and &h4000 )``

But Integer casting doesn’t:

``` MsgBox hex( Integer(val("&hd31c")) and &h4000 ) // won't compile ```[/quote]
Not weird at all
http://www.xojo.com/blog/en/2015/06/casting-vs-using-ctype.php

Is that the rhythm stick or the ugly stick?

It’s a selfie stick