msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

convert Hex to Byte

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax
Author Message
dalz



Joined: 04 Oct 2007
Posts: 3

PostPosted: Thu Jun 14, 2007 9:56 am    Post subject: convert Hex to Byte Reply with quote

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
View user's profile Send private message
Sinna



Joined: 04 Oct 2007
Posts: 191

PostPosted: Thu Jun 14, 2007 12:20 pm    Post subject: Re: convert Hex to Byte Reply with quote

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
View user's profile Send private message
dalz



Joined: 04 Oct 2007
Posts: 3

PostPosted: Thu Jun 14, 2007 11:33 am    Post subject: Re: convert Hex to Byte Reply with quote

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
View user's profile Send private message
dalz



Joined: 04 Oct 2007
Posts: 3

PostPosted: Thu Jun 14, 2007 1:22 pm    Post subject: Re: convert Hex to Byte Reply with quote

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
View user's profile Send private message
Tony Proctor



Joined: 04 Oct 2007
Posts: 1051

PostPosted: Thu Jun 14, 2007 4:39 pm    Post subject: Re: convert Hex to Byte Reply with quote

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
View user's profile Send private message
Sinna



Joined: 04 Oct 2007
Posts: 191

PostPosted: Thu Jun 14, 2007 8:09 pm    Post subject: Re: convert Hex to Byte Reply with quote

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
View user's profile Send private message
dalz via VBMonster.com



Joined: 04 Oct 2007
Posts: 7

PostPosted: Fri Jun 15, 2007 7:23 am    Post subject: Re: convert Hex to Byte Reply with quote

Thanks a lot to u both..It's working
I just have to figured out the set key now. Many thanks

--
Message posted via VBMonster.com
http://www.vbmonster.com/Uwe/Forums.aspx/vb-syntax/200706/1
Back to top
View user's profile Send private message
dalz via VBMonster.com



Joined: 04 Oct 2007
Posts: 7

PostPosted: Fri Jun 15, 2007 8:45 am    Post subject: Re: convert Hex to Byte Reply with quote

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
View user's profile Send private message
"Rick Rothstein \



Joined: 04 Oct 2007
Posts: 1584

PostPosted: Fri Jun 15, 2007 7:40 pm    Post subject: Re: convert Hex to Byte Reply with quote

> 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
View user's profile Send private message
Larry Serflaten



Joined: 04 Oct 2007
Posts: 2644

PostPosted: Fri Jun 15, 2007 9:50 pm    Post subject: Re: convert Hex to Byte Reply with quote

"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
View user's profile Send private message
dalz via VBMonster.com



Joined: 04 Oct 2007
Posts: 7

PostPosted: Mon Jun 18, 2007 10:44 am    Post subject: Re: convert Hex to Byte Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Related Topics:
convert byte to string Hello I have the following : dim s as string dim b as byte b has a value of 97 (ordinal value of character "a" ) How can "s" get the value from b ? Thanks :)

String Functions with Double-Byte Characters (My apoligies if someone you are getting this twice. I tried putting it in using a web based system on but i don't know if it worked or not. I'm running from and actual client now, so I can actually use newsgroups reliably.) Here is

Converting a Hex string or byte array to a double I have read a load of data from a file into memory in a series of bytes. I know that 8 of these bytes represent an IEEE 754 double. Is there a simple way of turning 8 bytes into a double? Thanks

How to cast a buffer pointer into a byte array Hi all, I'm developing a VC++.NET ATL in-process COM which dynamically allocates a buffer in memory. After filling this buffer (with bitmap image data) it is needed in the VB6 application which will reference this COM I'm writing. The VB application needs

Convert Seconds to Time How would I convert a value of seconds to a time value? Any help is greatly appreciated! Best Regards, Brad
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group