# How can you convert a byte of a string to an integer?

GebhartBob used Ask the Experts™
on
We need to examine the bit structure of a specific byte in a string. If we can get the byte converted to an integer, there's no problem examining bits.

I can get the byte isolated easily enough, as a 1-byte string, with a Left or Mid. But how do you convert a one-byte string to an integer?
Comment
Watch Question

Do more with

EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
cint(ByteVariable)

Thats what i understand. I think I am not clear with the problem
Commented:
Sounds to me like the ASC function is what you are looking for...you can also look into the StrConv to do the entire string if you'd like or the CopyMemory API call as well.

Dim strChar As String
Dim intChar As Integer

strChar = "S"
intChar = ASC(strChar)

Take care,
Shannon

Commented:
This is easy

integervalue = cint ("&H" & SingleByte)

tell the function that the number is in hex and it will convert that into an integer.

I think that is what you wanted?

Commented:
For Shannon---

I am ashamed that I had to ask. Thanks very much. Your approach works just fine.

For Chris Matthews---

Thanks for the suggestion, but it won't work. Consider this code:

Dim x As Integer
Dim str As String
str = "ABC"
x = CInt("&H" & Mid(str, 2, 1))

In the above, the byte to examine is an ASCII "B". That makes your CInt code line evaluate to x = CInt("&HB"), which is not what is needed. If you examine "x" at the end of the four lines above, you'll see 11, which indeed is a Hex "B". We need x to be 66, the equivalent of an ASCII "B".

Commented:
We've all been there, simple thing with a simple answer, but just can't think at that moment.  Don't ya just hate that.

Glad I could help.

Take care,
Shannon

Do more with

Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.