String to Hex
String to Hex
String to Hex
String to Hex
String to Hex
String to Hex String to Hex String to Hex String to Hex String to Hex String to Hex String to Hex String to Hex
String to Hex String to Hex
String to Hex
Go Back  Xtreme Visual Basic Talk > > > String to Hex


Reply
 
Thread Tools Display Modes
  #1  
Old 08-11-2005, 11:20 AM
torpkevuk's Avatar
torpkevuk torpkevuk is offline
Contributor
 
Join Date: Oct 2003
Location: Nebraska, USA
Posts: 640
Default String to Hex


I have this code for a String -> Hex conversion

Code:
Function StringToHex(ByVal str As String) As String Dim x As Long, hexchar As String For x = 1 To Len(str) hexchar = Hex$(Asc(Mid$(str, x, 1))) If Len(hexchar) = 1 Then hexchar = "0" & hexchar StringToHex = StringToHex & hexchar Next x End Function

However, this results in the following hex values coming out

00 09 00 25 54 3A 11 08 00 00 00 10

When I ~should~ have

00 09 00 25 D4 BA 91 08 00 00 00 10

Can anyone shed some light on this for me?
Reply With Quote
  #2  
Old 08-11-2005, 12:22 PM
John's Avatar
John John is offline
Bit Flipper
 
Join Date: Feb 2002
Location: The Inner Loop
Posts: 5,550
Default

Any reason in particular you posted this in the .NET communications section? Are you even using .NET? That code you posted uses all VB6 commands.

Anyway, here is a VB.NET function to do it:
Code:
Public Function StringToHex(ByVal str As String) As String Dim ret As String For i As Integer = 0 To str.Length - 1 ret &= Asc(str.SubString(i,1)).ToString("x").ToUpper & " " Next return ret.Trim() End Function
For comparison, here is a version without using the VB only Asc function:
Code:
Public Function StringToHex(ByVal str As String) As String Dim bytes() As Byte Dim ret As String bytes = Encoding.ASCII.GetBytes(str) For Each b As Byte In bytes ret &= b.ToString("x").ToUpper & " " Next Return ret.Trim() End Function
__________________
Subclassing|Magnetic Forms|Operator Overloading (VB2K5)|QuickSnip.NET

"These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy

Last edited by John; 08-11-2005 at 12:43 PM.
Reply With Quote
  #3  
Old 08-11-2005, 12:45 PM
torpkevuk's Avatar
torpkevuk torpkevuk is offline
Contributor
 
Join Date: Oct 2003
Location: Nebraska, USA
Posts: 640
Default

Yes, I'm using VB.NET, I googled for a string->hex function, and this is what I found.. it seemed to work except for the with the result I was receiving :\

The code you've given me there however comes up with the same problem though:

I should be getting
00 09 00 25 D4 BB D4 08 00 00 00 7D
and i'm getting (from your code)
0 9 0 25 54 3B 54 80 0 0 7D


Maybe a little background will help. I'm polling a Controller attached via RS232, I'm running Free Serial Port Monitor 3.31 by HHD Software to see what is being sent/received on the com port. I send some hex values to the controller, and it replies with (in this example, it changes according to timestamp etc.) 00 09 00 25 D4 BB D4 08 00 00 00 7D in the serial port monitor. But my data capture is getting it back like 0 9 0 25 54 3B 54 80 0 0 7D

The code I got the original SerialToHex code from said it was ANSI.. do I maybe need to use Unicode? If so, how?

Last edited by torpkevuk; 08-11-2005 at 12:54 PM.
Reply With Quote
  #4  
Old 08-11-2005, 01:30 PM
John's Avatar
John John is offline
Bit Flipper
 
Join Date: Feb 2002
Location: The Inner Loop
Posts: 5,550
Default

ANSI is not the same as ASCII for character codes over 127 (decimal). From the documentation I turned up ANSI is the default encoding so you may just be able to use the second function I posted (without Asc) and change ASCII to Default:
Code:
Public Function StringToHex(ByVal str As String) As String Dim bytes() As Byte Dim ret As String bytes = Encoding.Default.GetBytes(str) For Each b As Byte In bytes ret &= b.ToString("x").ToUpper & " " Next Return ret.Trim() End Function
I don't have any data to test this with however so you may want to look at the Encoding Namespace documentation in MSDN or the framework SDK.
__________________
Subclassing|Magnetic Forms|Operator Overloading (VB2K5)|QuickSnip.NET

"These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
Reply With Quote
  #5  
Old 08-15-2005, 10:32 AM
torpkevuk's Avatar
torpkevuk torpkevuk is offline
Contributor
 
Join Date: Oct 2003
Location: Nebraska, USA
Posts: 640
Default

I'm still not having any luck with this, and resorted to searching the web for a string/binary/hex ocx, i would have thought I'd find one, but I couldn't.

I figure now maybe I should try string->binary then binary->hex.

Can anyone give me any code samples for this? I feel like I'm bashing my head against a brick wall on something that should be fairly simple
Reply With Quote
  #6  
Old 08-15-2005, 11:28 AM
torpkevuk's Avatar
torpkevuk torpkevuk is offline
Contributor
 
Join Date: Oct 2003
Location: Nebraska, USA
Posts: 640
Default

In case it helps, heres some of the code:

Code:
Public Function String2Hex(ByVal str As String) As String Dim bytes() As Byte Dim ret As String bytes = Encoding.Default.GetBytes(str) For Each b As Byte In bytes ret &= b.ToString("x").ToUpper & " " Next Return ret.Trim() End Function

Thats the code I'm using to convert the string to hex. The string (in this instance) is "...%ÜÅ57...V" and is being read over an RS232 I'm putting the incoming data straight into the StringToHex function (if i try and show it in a message box, it just shows as a little square)

In my serial port sniffer I get:
00 09 00 25 DC C5 35 37 02 00 00 56

And in my program, I get:
00 09 00 25 5C 45 35 37 02 00 00 56 (actually, I get it incorrectly formatted, but this is how it shows)

I know that what I get in the serial port sniffer is correct, because part of it is a timestamp (25 DC C5 35) which is converted to binary, then split up, I then perform a function on it which converts the binary to a date/time and it comes out right, with the data from MY StringToHex the date/time comes out wrong.

Its like the StringToHex function doesn't like to convert properly where the first part of the 2 in the hex is a letter (ie. it shows 5C instead of DC and 45 instead of C5) All the rest seem to convert fine. Where am I going wrong?
Reply With Quote
  #7  
Old 08-15-2005, 11:39 AM
OnErr0r's Avatar
OnErr0rString to Hex OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

In your data conversion you appear to be missing the high bits of each byte. The only two bytes in which the high bits are set are DC and C5.


11011100 11000101 DCC5

01011100 01000101 5C45
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #8  
Old 08-15-2005, 11:43 AM
torpkevuk's Avatar
torpkevuk torpkevuk is offline
Contributor
 
Join Date: Oct 2003
Location: Nebraska, USA
Posts: 640
Default

Quote:
Originally Posted by OnErr0r
In your data conversion you appear to be missing the high bits of each byte. The only two bytes in which the high bits are set are DC and C5.


11011100 11000101 DCC5

01011100 01000101 5C45
But why? and how can I fix it??
Reply With Quote
  #9  
Old 08-15-2005, 11:45 AM
OnErr0r's Avatar
OnErr0rString to Hex OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

As John mentioned, you should checkout the Encoding Namespace.

Alternatively, consider using a byte array as the buffer and forget about converting from a string.

Btw, you can shorten the code a little:

ret &= b.ToString("X ")
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #10  
Old 08-15-2005, 12:34 PM
torpkevuk's Avatar
torpkevuk torpkevuk is offline
Contributor
 
Join Date: Oct 2003
Location: Nebraska, USA
Posts: 640
Default

Quote:
Originally Posted by OnErr0r
As John mentioned, you should checkout the Encoding Namespace.

Alternatively, consider using a byte array as the buffer and forget about converting from a string.

Btw, you can shorten the code a little:

ret &= b.ToString("X ")
Sorry to be such a pain, but I'm looking through this encoding namespace stuff, and like most stuff from microsoft, it tends to read like stereo instructions in chinese to me.. i tend to learn things easier by looking at them and seeing how they work.. any chance someone could give me a small code sample on how to get the string into a byte array? thanks again
Reply With Quote
  #11  
Old 08-15-2005, 03:40 PM
OnErr0r's Avatar
OnErr0rString to Hex OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

Rather than provide code right now, could you please explain why you aren't using a byte array in the first place.

For instance: Public Function String2Hex(ByRef bIn() As Byte) As String
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #12  
Old 08-15-2005, 04:43 PM
torpkevuk's Avatar
torpkevuk torpkevuk is offline
Contributor
 
Join Date: Oct 2003
Location: Nebraska, USA
Posts: 640
Default

Quote:
Originally Posted by OnErr0r
Rather than provide code right now, could you please explain why you aren't using a byte array in the first place.

For instance: Public Function String2Hex(ByRef bIn() As Byte) As String
To be honest, I dont think I even really thought about it.. figured it would be easier to send a string to the function and receive the hex back in a string
Reply With Quote
  #13  
Old 08-15-2005, 05:18 PM
OnErr0r's Avatar
OnErr0rString to Hex OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

Easier maybe, but probably not as efficient.

For anyone who MUST do this conversion, here are several methods:

Code:
' Ugly and probably not very efficient: For i = 0 To s.Length() -1 b(i) = Convert.ToByte(s.Chars(i)) Next i

Code:
' Better, but makes an array twice as big as we'd like: b = Encoding.Unicode.GetBytes(s) ' Use even numbers indices of the returned byte array

Code:
' Probably best: b = Encoding.GetEncoding("iso-8859-1").GetBytes(s) ' Special codepage which supports extended ASCII
__________________
Quis custodiet ipsos custodues.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
String to Hex
String to Hex
String to Hex String to Hex
String to Hex
String to Hex
String to Hex String to Hex String to Hex String to Hex String to Hex String to Hex String to Hex
String to Hex
String to Hex
 
String to Hex
String to Hex
 
-->