Home All Groups Group Topic Archive Search About

How to split Hex String and fill byte buffer in VB 6.0

Author
3 Nov 2008 2:55 PM
IJALAB
Hi,

I am a beginner in VB6.0
I have a hex string like 01020304
I want to split the above sting with 2 chars and fill the byte buffer.
For example

Dim tempBuffer(3) as byte
TempBuffer(0) = 04
TempBuffer(1) = 03
TempBuffer(2) = 02
TempBuffer(3) = 01

How can i do as stated above with VB6.0 string manipulation functions.
Please help me..

Thanks
Balaji

Author
3 Nov 2008 3:13 PM
Bill McCarthy
Hi Balaji,

There's a couple of ways to do this.  One way is to convert the value to a
long then right shift the value and bitwise And the last byte, eg:

Dim s As String
Dim value As Long
Dim i As Long
Dim b(0 To 3) As Byte

s = "&H01020304"
value = CLng(s)


For i = 3 To 0 Step -1
   b(i) = value And &HFF
   value = value \ &H100
Next i


Alternatively you could probably use string functions like Mid.






Show quoteHide quote
"IJALAB" <balaji.d***@gmail.com> wrote in message
news:fd27efa4-5dec-4133-ad56-41f21b7d101e@l33g2000pri.googlegroups.com...
> Hi,
>
> I am a beginner in VB6.0
> I have a hex string like 01020304
> I want to split the above sting with 2 chars and fill the byte buffer.
> For example
>
> Dim tempBuffer(3) as byte
> TempBuffer(0) = 04
> TempBuffer(1) = 03
> TempBuffer(2) = 02
> TempBuffer(3) = 01
>
> How can i do as stated above with VB6.0 string manipulation functions.
> Please help me..
>
> Thanks
> Balaji
Author
3 Nov 2008 3:14 PM
Dave O.
The easiest way is to use Mid$ to separate the elements and then CByte to
convert the string values into numerical values

x = "01020304"
TempBuffer(0) = CByte("&H" & Mid$(x,7,2))
TempBuffer(1) = CByte("&H" & Mid$(x,5,2))
TempBuffer(2) = CByte("&H" & Mid$(x,3,2))
TempBuffer(3) = CByte("&H" & Mid$(x,1,2))

The "&H" tells the system the numbers are in Hex.
If all your numbers are below 10 you need not bother with the "&H" part.

I leave it to you to work out how to make a loop so it'll cope with a string
of any length

Regards
Dave O.

Show quoteHide quote
"IJALAB" <balaji.d***@gmail.com> wrote in message
news:fd27efa4-5dec-4133-ad56-41f21b7d101e@l33g2000pri.googlegroups.com...
> Hi,
>
> I am a beginner in VB6.0
> I have a hex string like 01020304
> I want to split the above sting with 2 chars and fill the byte buffer.
> For example
>
> Dim tempBuffer(3) as byte
> TempBuffer(0) = 04
> TempBuffer(1) = 03
> TempBuffer(2) = 02
> TempBuffer(3) = 01
>
> How can i do as stated above with VB6.0 string manipulation functions.
> Please help me..
>
> Thanks
> Balaji
Author
6 Dec 2008 4:43 AM
Bhushan Deshpande
*** Sent via Developersdex http://www.developersdex.com ***
Author
6 Dec 2008 6:58 AM
Larry Serflaten

Author
6 Dec 2008 9:01 AM
Mike Williams

Author
11 Dec 2008 11:07 AM
Bhushan Deshpande
Hi Bill,
I am doing a similar type work
I am receiiving a string from serial port which is also in hex format.
I want to store it in a byte array using VB6.0
Please tell me howw to do that.
Can I use similar function?

Reply ASAP
thanks in advance


*** Sent via Developersdex http://www.developersdex.com ***
Author
11 Dec 2008 9:22 PM
Karl E. Peterson
Bhushan Deshpande wrote:
> I am doing a similar type work
> I am receiiving a string from serial port which is also in hex format.

Bytes aren't formatted.  They're just bytes.

> I want to store it in a byte array using VB6.0
> Please tell me howw to do that.

Directly.  Format them as hex if you want and as needed.

If you're turning the bytes that come across the interface into a string that looks
like hex, and would like to provide an example of such a string, many here could
show you how to quickly break them down to their individual bytes.
--
..NET: It's About Trust!
http://vfred.mvps.org