Solved

Converting ASCII to Hex error

Posted on 2004-09-27
3
374 Views
Last Modified: 2010-04-23
Hi

My program opens up the comm port and receives data from another computer. The ascii text received i convert to hex by

strHex = Hex(Asc(character))

but it doesnt convert correctly.

If the other computer sent 01 in hex I will receive 01 in hex, if they send FF in hex I receive 7F in hex

If we take this down to binary
FF = 11111111
7F = 01111111

The conversion from ascii to a number seems to take away the first bit

this is the same with many other hex values with a beginning 1 at the front taken away

The error comes at the point of converting the character to an ascii number

82 hex = 130 dec

but i get 02 dec

again it removed the beginning bit.

my comm port settings are
baud = 19200 (yes the same as the other computer)
comm = 1
parity = none
databit = 8
stopbit = 1

i have tried most other values of my comm settings but only the ones listed above gives the closest hex values back

anyone have any ideas

I use the rs232 class (the one microsoft gives you) for serial port communication

thanks
Adam
0
Comment
Question by:adam_pedley
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
ptakja earned 500 total points
ID: 12165974
Try this routine:

It expects a long.  If you are receiving a string, you can convert it like this:

Dim long as Int64 = convert.ToInt64(YourDataAsString)

' Convert from decimal to hexadecimal
' if you pass the Digits argument, the result is truncated to that number of
' digits
'
' you should always specify Digits if passing negative values

Function Hex(ByVal value As Long, Optional ByVal digits As Short = -1) As String
    ' convert to base-16
    Dim res As String = Convert.ToString(value, 16).ToUpper()

    ' truncate or extend the number
    If digits > 0 Then
        If digits < res.Length Then
            ' truncate the result
            res = res.Substring(res.Length - digits)
        ElseIf digits > res.Length Then
            ' we must extend the result to the left
            If value >= 0 Then
                ' extend with zeroes if positive
                res = res.PadLeft(digits, "0"c)
            Else
                ' extend with "F"s if negative
                res = res.PadLeft(digits, "F"c)
            End If
        End If
    End If
    ' return to caller
    Return res
End Function

Jeff
0
 
LVL 4

Author Comment

by:adam_pedley
ID: 12166051
Sorry, nope didn't work

there is a possibility that the ascii characters coming into my program could be stripped of their beginning 1.
0
 
LVL 4

Author Comment

by:adam_pedley
ID: 12166107
OK

I found a solution. Just another stupid microsoft stuff up once again. I found a serial port comm program built in vb .net that reads all the information back perfectly.

And when reading through the revisions of this class i found

'  Rev.4 (24/05/2002)
'  Fixed problem with ASCII Encoding truncating 8th bit

here are the details for the class
'===================================================
   '
   '      Module                        :      Rs232
   '      Description            :      Class for handling RS232 comunication with VB.Net
   '      Created                        :      10/08/2001 - 8:45:25
   '      Author                        :      Corrado Cavalli (corrado@mvps.org)
   '      WebSite                        :      www.codeworks.it/net/index.htm
'===================================================

I have seen vb .net 2005 and they are creating a serial port namespace although in beta 1 its not complete yet, so hopefully next year, serial comms will be much better than now.

thanks for your help jeff
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question