|
| Author |
Message |
dalz
Joined: 04 Oct 2007 Posts: 3
|
Posted: Thu Jun 14, 2007 9:56 am Post subject: convert Hex to Byte |
|
|
Hi,
I'm new in VB6
Anybody knows how to convert Hex to Byte??
Thank you
Archived from group: microsoft>public>vb>syntax |
|
| Back to top |
|
 |
Sinna
Joined: 04 Oct 2007 Posts: 191
|
Posted: Thu Jun 14, 2007 12:20 pm Post subject: Re: convert Hex to Byte |
|
|
dalz wrote:
> Hi,
>
> I'm new in VB6
> Anybody knows how to convert Hex to Byte??
>
> Thank you
>
Perhaps this is not very clean code, but for me it works like a charm:
Dim lData As Long
Dim sHexData As String
sHexData = "FF" 'just initialize to some value
lData = CLng("&H" & sHexData)
Just make sure you check for possible buffer overflows, e.g. passing
"1FFFFFFFF" to the code mentioned above will fail as it is larger than a
Long. Also ensure the string you pass in only contain 0..9 and/or A..F
characters. Look for the Like operator if you want to cope with this.
Sinna |
|
| Back to top |
|
 |
dalz
Joined: 04 Oct 2007 Posts: 3
|
Posted: Thu Jun 14, 2007 11:33 am Post subject: Re: convert Hex to Byte |
|
|
It works..thanks a lot Sinna
Do u know about set key A or B like A0A1A2A3.... for authentication?
I don't understand about this set key. I'm doing project using smart card
reader
Before we can read the data in the card, we need to do authentication first.
I'm still confuse how to set this key and doing the authentication.
Thank you so much |
|
| Back to top |
|
 |
dalz
Joined: 04 Oct 2007 Posts: 3
|
Posted: Thu Jun 14, 2007 1:22 pm Post subject: Re: convert Hex to Byte |
|
|
dalz wrote:
>It works..thanks a lot Sinna
>
>Do u know about set key A or B like A0A1A2A3.... for authentication?
>I don't understand about this set key. I'm doing project using smart card
>reader
>Before we can read the data in the card, we need to do authentication first.
>I'm still confuse how to set this key and doing the authentication.
>
>Thank you so much
how about hex to dec? for 16bytes
example 3A 25 00 00 C5 DA FF FF 3A 25 00 00 00 FF 00 FF
I use this code
Text1.Text=3A2500C5DAFFFF3A25000FF0FF
Text2.Text =Val("&H"+Text1.Text)
but there's error msg overflow
so what should I do?
Thank you |
|
| Back to top |
|
 |
Tony Proctor
Joined: 04 Oct 2007 Posts: 1051
|
Posted: Thu Jun 14, 2007 4:39 pm Post subject: Re: convert Hex to Byte |
|
|
Try this...
Private Sub Command1_Click()
Dim i As Integer
Text2.Text = ""
For i = 1 To Len(Text1.Text) - 1 Step 2
Text2.Text = Text2.Text & CStr(CByte("&h" & Mid$(Text1.Text, i, 2)))
& " "
Next i
End Sub
Private Sub Form_Load()
Text1.Text = "3A2500C5DAFFFF3A25000FF0FF"
End Sub
Tony Proctor
"dalz" wrote in message @uwe...
> dalz wrote:
> >It works..thanks a lot Sinna
> >
> >Do u know about set key A or B like A0A1A2A3.... for authentication?
> >I don't understand about this set key. I'm doing project using smart card
> >reader
> >Before we can read the data in the card, we need to do authentication
first.
> >I'm still confuse how to set this key and doing the authentication.
> >
> >Thank you so much
>
>
>
>
> how about hex to dec? for 16bytes
> example 3A 25 00 00 C5 DA FF FF 3A 25 00 00 00 FF 00 FF
> I use this code
> Text1.Text=3A2500C5DAFFFF3A25000FF0FF
> Text2.Text =Val("&H"+Text1.Text)
>
> but there's error msg overflow
> so what should I do?
> Thank you
> |
|
| Back to top |
|
 |
Sinna
Joined: 04 Oct 2007 Posts: 191
|
Posted: Thu Jun 14, 2007 8:09 pm Post subject: Re: convert Hex to Byte |
|
|
dalz wrote:
> dalz wrote:
>> It works..thanks a lot Sinna
>>
>> Do u know about set key A or B like A0A1A2A3.... for authentication?
>> I don't understand about this set key. I'm doing project using smart card
>> reader
>> Before we can read the data in the card, we need to do authentication first.
>> I'm still confuse how to set this key and doing the authentication.
>>
>> Thank you so much
>
>
>
>
> how about hex to dec? for 16bytes
> example 3A 25 00 00 C5 DA FF FF 3A 25 00 00 00 FF 00 FF
> I use this code
> Text1.Text=3A2500C5DAFFFF3A25000FF0FF
> Text2.Text =Val("&H"+Text1.Text)
>
> but there's error msg overflow
> so what should I do?
> Thank you
>
What about this?
Dim sText As String: sText = "00 11 AA FC DD EE 34"
Dim sData() As String, bData() As Byte
Dim iByte As Long, lByteCount As Long
sData = Split(Trim(sText), " ")
lByteCount = UBound(sData) + 1
ReDim bData(lByteCount - 1) As Byte
For iByte = 0 To lByteCount - 1
bData(iByte) = CByte("&H" & sData(iByte))
Next iByte
Sinna |
|
| Back to top |
|
 |
dalz via VBMonster.com
Joined: 04 Oct 2007 Posts: 7
|
|
| Back to top |
|
 |
dalz via VBMonster.com
Joined: 04 Oct 2007 Posts: 7
|
Posted: Fri Jun 15, 2007 8:45 am Post subject: Re: convert Hex to Byte |
|
|
Now I can read the card Serial Number
The problem is the card Serial Number should be 3851693428
but my result is 11636
3851693428 and 11636 are having same Hex = 2D74
What should I do to have the result 3851693428 from 2D74?
Thanks a lot...
--
Message posted via VBMonster.com
http://www.vbmonster.com/Uwe/Forums.aspx/vb-syntax/200706/1 |
|
| Back to top |
|
 |
"Rick Rothstein \
Joined: 04 Oct 2007 Posts: 1584
|
Posted: Fri Jun 15, 2007 7:40 pm Post subject: Re: convert Hex to Byte |
|
|
> Then how to convert this E5942D74 to 3851693428?
You can use this function which will handle up to a 96-bit number...
Function Hex2Dec(ByVal HexString As String) As Variant
Dim X As Integer
Dim BinStr As String
If Left$(HexString, 2) Like "&[hH]" Then
HexString = Mid$(HexString, 3)
End If
If Len(HexString) <= 12 Then
Const BinValues = "0000000100100011" & _
"0100010101100111" & _
"1000100110101011" & _
"1100110111101111"
For X = 1 To Len(HexString)
BinStr = BinStr & Mid$(BinValues, _
4 * Val("&h" & Mid$(HexString, X, 1)) + 1, 4)
Next
For X = 0 To Len(BinStr) - 1
Hex2Dec = Hex2Dec + Val(Mid(BinStr, _
Len(BinStr) - X, 1)) * 2 ^ X
Next
Else
' Number is too big, handle error here
End If
End Function
Rick |
|
| Back to top |
|
 |
Larry Serflaten
Joined: 04 Oct 2007 Posts: 2644
|
Posted: Fri Jun 15, 2007 9:50 pm Post subject: Re: convert Hex to Byte |
|
|
"Rick Rothstein (MVP - VB)" wrote
>
> You can use this function which will handle up to a 96-bit number...
Deja vu, why do two conversions when only one is needed?
LFS
Private Function Hx2Dec(ByRef Hexidecimal As String) As Variant
' Converts up to 24 hexidecimal characters to a decimal value
Dim idx As Long
Dim hx As String
Const code = "123456789ABCDEF"
hx = UCase$(Hexidecimal)
' Remove &H prefix and leading 0's
idx = 1
Do
Select Case Mid(hx, idx, 1)
Case "&", "H", "0"
idx = idx + 1
Case Else
Exit Do
End Select
Loop
hx = Mid$(hx, idx)
' Validate input
If hx Like "*[!0-9A-F]*" Then Exit Function
' Check size
idx = Len(hx)
If idx > 24 Then Exit Function
' Convert
Hx2Dec = CDec(0)
Do While idx > 0
Hx2Dec = (Hx2Dec * 16) + (InStr(code, Mid$(hx, idx, 1)))
idx = idx - 1
Loop
End Function |
|
| Back to top |
|
 |
dalz via VBMonster.com
Joined: 04 Oct 2007 Posts: 7
|
Posted: Mon Jun 18, 2007 10:44 am Post subject: Re: convert Hex to Byte |
|
|
Rick Rothstein (MVP - VB) wrote:
>> Then how to convert this E5942D74 to 3851693428?
>
>You can use this function which will handle up to a 96-bit number...
>
>Function Hex2Dec(ByVal HexString As String) As Variant
> Dim X As Integer
------------------
Thanks a lot all..It's working
--
Message posted via http://www.vbmonster.com
|
|
| Back to top |
|
 |
|