Recieving from serial in hexidecimal

Posted on 2005-04-24
Last Modified: 2012-05-05
I am using a serial program from
to recieve 64-bits from the serial port. The problem is when it recieves them it puts them into a string as characters. I want the program to put them into a sring as hexidecmimal numbers cos i have the compare the input with a database containing hex. Is there any hope of being able to alter it. Any help would be appreciated.

Question by:screach
    LVL 7

    Expert Comment

    You mean the program return "00111001..." as a string of bit value? If so, use a loop, extract 4 sequence numbers and then use this to calculate its hex value:
    0000->0 (hex)

    Hope this help
    LVL 3

    Expert Comment

    first of all - what languadge are you using ?? (C++, VIsual Basic ... )

    i have no idea why you using third party app's to read from com port, because it's quite easy to do it directly
    and once you do it yourself you be able to get binary data the way you want.
    wherei s also Microsoft MSComm activeX control that will be able to read write and do events on COM ports, quite simple to use.

    anyway in order to convert string to bytes array you can use this in visual basic

             For i = 0 To Len(StringData) - 1
                BinData(i) = Asc(Mid(StringData, i + 1, 1))
                BinData(i) = AscB(MidB(StringData, i + 1, 1))

    in order to directly read from COM port in VB you can use something like this

     Declare Function CreateFile& Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName$, ByVal dwDesiredAccess&, ByVal dwShareMode&, ByVal lpSecurityAttributes&, ByVal dwCreationDisposition&, ByVal dwFlagsAndAttributes&, ByVal hTemplateFile&)
     Declare Function CloseHandle& Lib "kernel32" (ByVal hObject&)
     Declare Function FlushFileBuffers& Lib "kernel32" (ByVal hFile&)
     Declare Function GetCommState Lib "kernel32.dll" (ByVal nCid&, ByRef lpDCB As Any) As Boolean
     Declare Function SetCommState Lib "kernel32.dll" (ByVal nCid&, ByRef lpDCB As Any) As Boolean
     Declare Function GetCommTimeouts Lib "kernel32.dll" (ByVal hFile&, ByRef lpCommTimeouts As Any) As Boolean
     Declare Function SetCommTimeouts Lib "kernel32.dll" (ByVal hFile&, ByRef lpCommTimeouts As Any) As Boolean
     Declare Function WriteFile& Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite&, lpNumberOfBytesWritten&, ByVal lpOverlapped&)
     Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, Optional ByVal lpOverlapped As Long = 0&) As Long

    sub some_example()
    Dim bModemCommand(256) As Byte
             CommPort= "COM1"
             OpenPort = CreateFile("COM1", &HC0000000, 0, 0, 3, 0, 0)
             If OpenPort = -1 Then
                Msg = "Unable to open communication port " & CommPort
                exit sub
             End If
             'Read Byte array.
             RetVal = ReadFile(OpenPort, BinaryData(0), 200, RetBytes, 0)
             RetVal = CloseHandle(OpenPort)
    end sub

    In VC++ it's even easier

    I can send you more source code, but im not sure if you want it.



    P.S. if you want to modify third party code to provide you array of bytes instead of strings it would be quite hard...


    Author Comment

    Sorry i wasn't really clear. I am using C in visual The function is returning characters in a string as the input from the serial port. I want the string to hold the hexidecimal value of each character, obviously the new string would have to be double the size. Just wondering is there an easy way to convert it.


    Author Comment

    I can only manipulate the character i cannot get to the input string before it is converted from 8 bits into a character.
    LVL 3

    Expert Comment

    strings same way as bytes are only in 8 bit per item
    If it's a unicode string then it's going to be two 8-bit items per char

    if you have pointer to a string let's say str_ptr - you can convert it to the pointer to unsigned char
    like this :

    unsigned char  * new_pointer;

    new_pointer = &(UCHAR)*str_ptr;

    and each new_pointer[i] will be a byte

    unless stripping of high bits of the input - this will give you access to all bits you want.

    now if actually strip hig bits that means some of the information from serial port is gone forether - so in this case you need to reverse engeneering and modify or use another software/method to read from serial port.
    LVL 14

    Expert Comment

    by:Shiju Sasidharan
    Private Sub Command1_Click()
        MsgBox ConvertToHex("", "-")
        MsgBox ConvertToHex("This is Input")
        MsgBox ConvertToHex("This is Input", "-")
    End Sub
    Private Function ConvertToHex(InputDataString, Optional ByVal Seperator As String) As String
    Dim lCount As Long
        For lCount = 1 To Len(InputDataString)
            If ConvertToHex = "" Then
                ConvertToHex = Hex(Asc(Mid(InputDataString, lCount, 1)))
                ConvertToHex = ConvertToHex & Seperator & Hex(Asc(Mid(InputDataString, lCount, 1)))
            End If
        Next lCount
    End Function

    Author Comment

    Its ok lads, found a really simple way of doing it

                unsigned int c;
                TCHAR test[20];
                int i;

                      c = buffer[0];


    Test now holds the hex value.

    Thanks for the help

    Accepted Solution

    PAQed with points refunded (250)

    Community Support Moderator

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    zeroMAx challenge 20 66
    for loop with Set 4 38
    java  and programming certification ? 4 31
    python sqlite question 11 24
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now