Home All Groups Group Topic Archive Search About

Convert hex string to a byte

Author
7 Oct 2005 9:26 PM
fernandez.dan
I'm trying to convert a two digit hex string like the following "80",
"FF", and "0F"

The byte for the hex string "80" will be 10000000

The byte for the hex string "FF" will  be 11111111

The byte for the hex string "0F" will be 00001111

and so forth

I'm kind of new to the Visual Basic 6 world so I am kind of lost with
this.


Thanks

Author
7 Oct 2005 9:31 PM
Karl E. Peterson
fernandez.***@gmail.com wrote:
> I'm trying to convert a two digit hex string like the following "80",
> "FF", and "0F"
>
> The byte for the hex string "80" will be 10000000

  Dim b As Byte
  b = &h80

or

  MyHexByte = "80"
  b = Val("&h" & MyHexByte)

--
Working Without a .NET?
http://classicvb.org/petition
Author
7 Oct 2005 10:09 PM
fernandez.dan
Thanks Karl,

For the quick reply. I have been googling for examples but didn't come
up with anything.

I appreciate it.

What am I trying to do say I have the hex string "1024"

I can use your method above to get my 16 bit value of
00010000 00100100

I was wondering how can I extract the first six bits and the last 10
bits so the
type=000100
length=0000100100

Basically the type is the value of my data and the length is the number
of bytes. In this example the type would be 4 and the length would be
36

I appreciate the help.
Author
7 Oct 2005 10:22 PM
Karl E. Peterson
fernandez.***@gmail.com wrote:
> What am I trying to do say I have the hex string "1024"
>
> I can use your method above to get my 16 bit value of
> 00010000 00100100

16-bit values are Integers.  In that case:

  Dim i As Integer
  i = &h1024

Now, if you meant to say you're after a *string* of
  "00010000 00100100"
That's a different case!  Try something like this:

   Public Function FmtBin(ByVal InVal As Variant) As String
      Dim tmpRet As String
      Dim i As Integer
      Dim pos As Integer
      Dim length As Integer

      ' Determine proper output length, based on vartype
      Select Case VarType(InVal)
         Case vbByte
            length = 8
         Case vbInteger
            length = 16
         Case vbLong
            ' Function only designed to handle Integers, as
            ' (InVal And (2 ^ i)) will overflow when a Long
            ' variable's sign bit is set.  Need to recurse.
            FmtBin = FmtBin(WordHi(InVal)) & " " & FmtBin(WordLo(InVal))
            Exit Function
         Case Else
            ' Function not designed for anything else
            Debug.Print TypeName(InVal)
            Err.Raise 13, "HiLo.FmtBin", "Type mismatch"
            Exit Function
      End Select

      ' Pad output string with all 0's
      tmpRet = String$(length, "0")

      ' Cycle through each position inserting a 1 in
      ' return string if that bit is set.
      For i = (length - 1) To 0 Step -1
         pos = pos + 1
         If InVal And (2 ^ i) Then
            Mid$(tmpRet, pos, 1) = "1"
         End If
      Next i

      ' Add a space between bytes
      Select Case length
         Case 8
            FmtBin = tmpRet
         Case 16
            FmtBin = Left$(tmpRet, 8) & " " & Right$(tmpRet, 8)
      End Select
   End Function

(That's taken directly from http://vb.mvps.org/samples/Twiddle)

> I was wondering how can I extract the first six bits and the last 10
> bits so the
> type=000100
> length=0000100100

You might want to take a look at that bit-twiddling example.  It has just about
everything.

Later...   Karl
--
Working Without a .NET?
http://classicvb.org/petition
Author
8 Oct 2005 12:37 AM
fernandez.dan
Cool Karl,

I will check out the Twiddle article. Just to clarify, I don't want the
string  "00010000 00100100"

Basically I have a hex string given to me something like "1024",
"FFFF", "A012", etc.

In this example I will use "1024". I want to convert this so I can have
two bytes.
Convert the "10" for the first byte will be the following 00010000
Convert the "24" for the 2nd byte will have the following 00100100

So the bits will be 0001000000100100

I want to extract the first six bits ---> 000100
I want to extract the last ten bits ---> 0000100100

Then I will convert to a integer
First six bits in this case 000100 --> 4
Last ten bits 0000100100 ---> 36

Thanks Karl and I will check out the article.
Author
8 Oct 2005 2:28 AM
Larry Serflaten
<fernandez.***@gmail.com> wrote

Show quoteHide quote
>
> Basically I have a hex string given to me something like "1024",
> "FFFF", "A012", etc.
>
> In this example I will use "1024". I want to convert this so I can have
> two bytes.
>
> I want to extract the first six bits ---> 000100
> I want to extract the last ten bits ---> 0000100100
>
> Then I will convert to a integer
> First six bits in this case 000100 --> 4
> Last ten bits 0000100100 ---> 36


See if this helps:

LFS

Private Sub Form_Load()
Dim User As String
Dim Value As Long

User = "1024"
Value = CLng("&H" & User)
MsgBox "Type: " & GetType(Value)
MsgBox "Length: " & GetLength(Value)
End Sub

Private Function GetType(Value As Long) As Long
  GetType = (Value \ &H400)
End Function

Private Function GetLength(Value As Long) As Long
  GetLength = (Value And &H3FF&)
End Function
Author
10 Oct 2005 11:45 PM
Karl E. Peterson
fernandez.***@gmail.com wrote:
Show quoteHide quote
> Cool Karl,
>
> I will check out the Twiddle article. Just to clarify, I don't want
> the string  "00010000 00100100"
>
> Basically I have a hex string given to me something like "1024",
> "FFFF", "A012", etc.
>
> In this example I will use "1024". I want to convert this so I can
> have two bytes.
> Convert the "10" for the first byte will be the following 00010000
> Convert the "24" for the 2nd byte will have the following 00100100
>
> So the bits will be 0001000000100100
>
> I want to extract the first six bits ---> 000100
> I want to extract the last ten bits ---> 0000100100
>
> Then I will convert to a integer
> First six bits in this case 000100 --> 4
> Last ten bits 0000100100 ---> 36
>
> Thanks Karl and I will check out the article.

You should be able to do all that, quite easily, with that code I pointed you to.
--
Working Without a .NET?
http://classicvb.org/petition
Author
13 Oct 2005 3:20 PM
fernandez.dan
Karl E. Peterson wrote:
Show quoteHide quote
> fernandez.***@gmail.com wrote:
> > Cool Karl,
> >
> > I will check out the Twiddle article. Just to clarify, I don't want
> > the string  "00010000 00100100"
> >
> > Basically I have a hex string given to me something like "1024",
> > "FFFF", "A012", etc.
> >
> > In this example I will use "1024". I want to convert this so I can
> > have two bytes.
> > Convert the "10" for the first byte will be the following 00010000
> > Convert the "24" for the 2nd byte will have the following 00100100
> >
> > So the bits will be 0001000000100100
> >
> > I want to extract the first six bits ---> 000100
> > I want to extract the last ten bits ---> 0000100100
> >
> > Then I will convert to a integer
> > First six bits in this case 000100 --> 4
> > Last ten bits 0000100100 ---> 36
> >
> > Thanks Karl and I will check out the article.
>
> You should be able to do all that, quite easily, with that code I pointed you to.
> --
> Working Without a .NET?
> http://classicvb.org/petition

Cool, Thanks everyone. All the information helped me out and it is
solved. Appreciate the advice and info.
Author
7 Oct 2005 9:43 PM
Saga
many ways... here is one:

To test create a new project and place a blank textbox and a button.

Note: Input into functions assumes valid hex data.

One digit hex values (such as "a") will display as 4 binary bits. If
you want the 4 leading zeroes, concatenate a 0 to eh hex digit so
that the function will get two hex digits.

Option Explicit

Private Function HexDigitToBin(ByVal strHex As String) As String

  Dim strBinAr As Variant

  strBinAr = Array("0000", "0001", "0010", "0011", "0100", "0101", _
            "0110", "0111", "1000", "1001", "1010", "1011", "1100",
"1101", _
            "1110", "1111")

  HexDigitToBin = strBinAr(Val("&H" & strHex))

End Function

Private Function HexByteToBin(ByVal strHexByte As String) As String

  If Len(strHexByte) = 1 Then
    HexByteToBin = HexDigitToBin(strHexByte)
  Else
    HexByteToBin = HexDigitToBin(Left$(strHexByte, 1)) &
HexDigitToBin(Right$(strHexByte, 1))
  End If

End Function

Private Sub Command1_Click()
  '

  MsgBox HexByteToBin(Text1.Text)

End Sub



Good luck
Saga

<fernandez.***@gmail.com> wrote in message
Show quoteHide quote
news:1128720379.991425.120010@g43g2000cwa.googlegroups.com...
> I'm trying to convert a two digit hex string like the following "80",
> "FF", and "0F"
>
> The byte for the hex string "80" will be 10000000
>
> The byte for the hex string "FF" will  be 11111111
>
> The byte for the hex string "0F" will be 00001111
>
> and so forth
>
> I'm kind of new to the Visual Basic 6 world so I am kind of lost with
> this.
>
>
> Thanks
>