Convert a string to an array of bytes

B

Bit Twiddler

Steve Rindsberg was cool enough to answer the converse of this question
("how to convert an array of bytes to a string").

I thought there would be a built in function for this.

For example, I have a string that looks like this, "4D00FF...".

I'd like to iterate over that string in 2 bytes chunks so that I see it like
"4D", "00", "FF", etc.

StrConv() doesn't seem to do the trick.

Thanks,
BT
 
K

Karl E. Peterson

Bit said:
Steve Rindsberg was cool enough to answer the converse of this
question ("how to convert an array of bytes to a string").

I thought there would be a built in function for this.

For example, I have a string that looks like this, "4D00FF...".

I'd like to iterate over that string in 2 bytes chunks so that I see
it like "4D", "00", "FF", etc.

You'd just do something like this:

<air code>
nByte = 0
Redim Bytes(0 to Len(MyData) \ 2) As Byte
For i = 1 to Len(MyData) - 1 Step 2
Bytes(nByte) = Val("&h" & Mid$(MyData, i, 2))
nByte = nByte + 1
Next i
</air code>
 
S

Steve Rindsberg

Steve Rindsberg was cool enough to answer the converse of this question
("how to convert an array of bytes to a string").

I thought there would be a built in function for this.

For example, I have a string that looks like this, "4D00FF...".

I'd like to iterate over that string in 2 bytes chunks so that I see it like
"4D", "00", "FF", etc.

One approach would be to use Mid$ to extract characters two at a time.

For X = 1 to Len(sString) step 2
debug.print Mid$(sString,x,2)
Next
 
Top