Converting ASCII to HEX


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2004
    Location
    UK-Midlands
    Posts
    84

    Exclamation Converting ASCII to HEX

    Hi,

    Can't think of how to do the following;

    An 8 digit number in ASCII stored in 8 bytes need to be converted to a 4 byte hex number.
    So 31724529 would become 01E413F1 stored as:
    NUM1 = 01
    NUM2 = E4
    NUM3 = 13
    NUM4 = F1

    Have tried several methods but failed miserably! Any help, pointers or parts of code will stop me ripping my hair out (not much left now!).

    Thanks,
    BobP

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default

    Not sure if I am suggesting the easiest way. Anyway, you can work it out. First convert the ASCII numbers to binary format in 4 bytes thus

    31724529 = ((3*10) + (1*10) + (7*10) + ... + 9)

    If this is what you want 01E413F1, 'you got it'

    If you want to send this as hex digits, you need to pick one number at a time and send the nibbles. For example, E4 will be sent as 'E' and '4'. To do this, you split the E4 into 0E and 04. To make any number to printable hex, you need to check; if it is in 0..9, simply add 30H and you got the printable hex. However, if the number is between 0A to 0Fh, you add 37H to get the printable Hex digit.

    Hope this sets you on the right track.

    Jerson

  3. #3
    Join Date
    Jul 2005
    Posts
    78


    Did you find this post helpful? Yes | No

    Default

    Interesting problem. You’ve just maxed out the capabilities of PIC basic, which is most happy with 16 bit numbers, but has some facilities to get out 32 bits.

    The basic algorithm is simple: Start from the most significant ASCII digit, convert to binary, add the binary to a sum. If more digits, multiply the sum by 10 and repeat for the next digit. Actually, when you implement this you zero out the sum first, then do the times 10 multiply at the top of the loop (saves you the code for checking for the last run of the loop).

    Converting ASCII to binary is simple: just subtract $30 (ASCII zero) from the ASCII and you got it!

    In the code below we use temp for the binary, and BottomSum and TopSum for the sum. We need 2 words for the sum to get 32 bits. The next trick is to recognize that PBP has 2 different multiplication methods, * and **. When you multiply two 16 bit numbers the result can be as high as 32 bits. PBP handles this by using * to return the lowest 16 bits and ** to return the highest 16 bits. We use both methods to accumulate a 32 bit sum for the times 10 part.

    The following code will compile, but has not been properly debugged. Always code defensively when using integer arithmetic where an overflow can give you buggy results.

    Code:
        ASCII var BYTE[8]           ' holds the 8 orgional ASCII digits
        HexResult var byte[4]       ' place for 8 hex digits (4 bytes)
        BottomSum var Word          ' BottomSum & TopSum used to store 16
        TopSum var word             '  bit results
        LastBottom var byte         ' result of last operation to BottomSum
                                    '  used to detect overflows
        Index var byte              ' pointer
        temp var word               ' tempory intermediate result
        
        ' fill ASCII with data  ASCII[]= 31724529
        ASCII[0]= 9
        ASCII[1]= 2
        ASCII[2]= 5
        ASCII[3]= 4
        ASCII[4]= 2
        ASCII[5]= 7
        ASCII[6]= 1
        ASCII[7]= 3
    
        BottomSum =0
        TopSum =0
        LastBottom = 0
        For Index = 7 to 0 step -1
            ' first, take total accumlated in the sums and multiply by 10
            TopSum = TopSum * 10                ' direct multiply
            BottomSum = BottomSum * 10          ' also a direct multiply
            TopSum = TopSum + BottomSum ** 10   ' top 16 bits of the multiply 
            ' convert ascii to number,add to accumulated sum
            temp = ASCII[Index] - $30   ' convert ascii digit to binary
            bottomSum = BottomSum + temp
            ' check bottom result in case of overflow, if so, bump top result
            ' ( an overflow will give a result smaller then the existing value)
            if bottomSum < Lastbottom then TopSum = TopSum + 1 
            LastBottom = bottomSum                  ' set for next iteration
        next index    
        
        ' now we have the result in Top & Bottom Sum, move to HexResult array
        HexResult[0] = bottomsum.byte0
        HexResult[1] = bottomsum.byte1
        HexResult[2] = Topsum.byte0
        HexResult[3] = Topsum.byte1

  4. #4
    Join Date
    Mar 2004
    Location
    UK-Midlands
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    Many thanks for all the replies.

    Was going down the same route but my code wasn't as neat.

    Will try the code later this week.

    Many thanks again,
    BobP

Similar Threads

  1. Hex converting
    By Andre_Pretorius in forum General
    Replies: 9
    Last Post: - 26th June 2009, 02:48
  2. Need help converting to ASCII Hex format
    By tcbcats in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 26th August 2007, 19:14
  3. hex ascii help please
    By ffr58kk90 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 29th December 2006, 21:09
  4. ascii characters to hex
    By Peter1960 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 26th January 2006, 04:06
  5. Converting Bytes from hex to Dec
    By Rob Martin in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 8th April 2005, 19:44

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts