A Bodnar

I am trying to figure out how I can find hex data in a string or memorystream.

I have tried with a string:

firstString.IndexOfAny(Conversion.Hex(&H0))

firstString.IndexOf(Conversion.Hex(&H0))

firstString.LastIndexOfAny(Conversion.Hex(&H0))

and these work, but I want to find something like &HFF00AA55

It does not work when I try to use more than one character following the &H

I have had no luck doing something like this in a memorystream either.

Can someone help explain how to do this

Thank you,

Angela Bodnar



Re: Visual Basic Language find hex in a string or memorystream

KevinBB

The IndexOfAny method can only look for single characters. You can pass an array of characters and it will return the index of the first instance of any one of those characters. IndexOfAny and LastIndexOfAny won't work with multiple hex digits because they can only take one character (in this case, 0-9 and A-F).

The IndexOf method can look for a whole string, such as "FF00AA55". If your string contains "&HFF00AA55", you may need to add the "&H" to the front of the string before calling IndexOf.

firstString.IndexOf("&H" & Conversion.Hex(&HFF00AA55))





Re: Visual Basic Language find hex in a string or memorystream

Angela Bodnar

I tried this and it did not work, it returned -1

firstString.IndexOf("&H" & Conversion.Hex(&HFF00AA55))

I have:

Dim firstString As String = &HFF00AA550011

MsgBox(Conversion.Hex(firstString)) 'this works to display as hex - show FF00AA550011

Any other ideas





Re: Visual Basic Language find hex in a string or memorystream

KevinBB

I would recommend programing with Option Strict On. (Right now you are using Option Strict Off.) The code above won't compile with Option Strict On. The whole problem is due to data type confusion, and Option Strict On will help you fix the data type problems. The code is mixing String and Long (64-bit integer), and it doesn't work.

Regarding Dim firstString As String = &HFF00AA550011, here is what is really happening:

Dim firstString As String = CStr(CLng(&HFF00AA550011))

VB sees &HFF00AA550011 as a 64-bit integer, and converts that to a string ("280378322780177").

 

Here is probably what you want:

Dim firstString As String = "FF00AA550011"

or possibly:

Dim firstString As String = Conversion.Hex(&HFF00AA550011)

 

Once you do one of these, firstString.IndexOf("FF00AA55") will return the index you want. Again, Option Strict On will force you to clear up any data type confusion.

 





Re: Visual Basic Language find hex in a string or memorystream

Angela Bodnar

This is what worked after I set explict on:

Dim firstString As String = Conversion.Hex(&HFF00AA550011)

MsgBox(firstString.IndexOf(Conversion.Hex(&HFF00AA55)))

Thanks for the suggestions!





Re: Visual Basic Language find hex in a string or memorystream

Angela Bodnar

I have another question regarding this:

I am trying to use a hex string of

&HFF00AA550000001C1000210000000021155AA00FF

but this gives me an overflow error. It seems that I can only have a hex string of this length:

&HFF00AA550000001C

anymore characters and I get the overflow error.

I am able to use:

&HFF00AA550000001C & &H1000210000000021 & &H155AA00FF

but I'm not sure this works.

Why is this and how can I get around it





Re: Visual Basic Language find hex in a string or memorystream

KevinBB

The reason and confusion for all of this is due to data types -- how is the data being stored internally (You are still not clear about data types )

There is a subtle but important difference between a value and its representation. For example, the value 42 can be represented in these ways:

hexadecimal: 2A

decimal: 42

binary: 101010

In all three cases, the underlying value is the SAME, only the representation is different.

You also have to know how you want to treat a value: as a number, or as a string. In VB, &H2A and "2A" are very different. One is a number, the other is a string. You can convert the number to a string using CStr or Conversion.Hex. And you can convert the string to a number using CLng or Long.Parse. VB conveniently allows you to convert from one to the other, but remember that they are NOT the same thing.

Now for your actual examples. The following two values are quite different:

&HFF00AA550000001C -- this is a 64-bit integer value, expressed in hexadecimal notation. VB stores this value in a Long (64-bit) integer. VB doesn't let you add any more hex digits to the value, because VB doesn't have a longer integer data type to store the value in (causing an overflow error).

"FF00AA550000001C" -- this is a string. It just so happens that all the characters in the string are hex digits (0-9, A-F). VB stores this value in a String. A string can be arbitrarily long. You can add as many more characters to the end of a string as you like.

&HFF00AA550000001C can be stored in a 64-bit integer (Long). A Long is the largest integer that .NET provides. When you have the following statement:

Dim firstString As String = Conversion.Hex(&HFF00AA550000001C)

VB.NET first converts the hex value to a Long, and then converts it to a String.

But when you have an expression like this:

&HFF00AA550000001C & &H1000210000000021 & &H155AA00FF

VB.NET converts each individual value to a Long, and then converts each Long to a String, and concatenates all the strings together. You end up with one long string of hex digits: "&HFF00AA550000001C1000210000000021155AA00FF"

How you want to handle the values (as numbers or strings) will depend on what you ultimately want to do with them. If you are looking for numbers, you may want to consider using a Byte array instead of 64-bit Longs. A Byte array can be arbitrarily long.





Re: Visual Basic Language find hex in a string or memorystream

Angela Bodnar

Sorry for the delay, but I was on vacation last week.

Thank you for the detailed explaination.

I am not storing this data internally. I am simply going to send it over a socket, along with other string

data. The host expects to receive this in hex format, that's why I am using the &H. I will also be receiving

similar hex data from the host that I must interpret. So I do not believe I want it in the long format.

So, this is actually what I want, the "long string of hex digits":

But when you have an expression like this:

&HFF00AA550000001C & &H1000210000000021 & &H155AA00FF

VB.NET converts each individual value to a Long, and then converts each Long to a String, and concatenates all the strings together. You end up with one long string of hex digits: "&HFF00AA550000001C1000210000000021155AA00FF"

Is there a better way for me to do this

Thanks.