Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2009
    Location
    Scotland
    Posts
    44
    Thanks
    2
    Thanked 1 Time in 1 Post

    Converting BCD to Hex

    I have just got some software working where I can access and read in a serial port that the user has selected. As the information is read in I want to split it into 32byte segments, which I have got it doing, and then convert this information from BCD to Hex and put out onto the console. I have the conversion as being multiplying the top nibble of each byte by 10 and then adding this to the lower nibble but it doesn't appear to be correct.

    Code:
    void convert2Hex(byte* buffer, int byte_num)
    {
    int i, n0, n1;
    for (i = (byte_num-1); i >= 0; i--) {
    	n0 = x;                  //Need the value of lower nibble.
    	n1 = x * 10;           //Need the value of higher nibble, multiply by 10.
    	buffer[i] = n1 + n0;  //Put hex value back into array.
    }
    }
    The array will then be converted to a string once all 32 bytes have been converted and output to console (or that's the theory at least...)

    Any ideas if this is the correct approach?

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Where do you assign x a value? I don't see any code that does that.

    I think you are trying to do the wrong thing. If you want to be able to print the value of bytes to the console in hex, you need to store each hex number (as in every 4 bits of data) as one char.

    And to separate a byte out into each nibble, you use bitwise operators:
    unsigned char some_byte = 0xF3;
    unsigned char lower_nibble = some_byte & 0x0F;
    unsigned char upper_nibble = (some_byte & 0xF0) >> 4;

    Then you can convert lower_nibble and upper_nibble to ASCII and store them some place for output to the screen.
    OracleGuy

  • #3
    New Coder
    Join Date
    Jul 2009
    Location
    Scotland
    Posts
    44
    Thanks
    2
    Thanked 1 Time in 1 Post
    Was meant to be passing x across as the byte value (forgot to put it in with the other parameters). Read through what you have described and it makes sense with what I was thinking after I posted earlier and read through small pieces of information scattered through various forums.

    Changed the code to:
    Code:
    string convertHex(byte* sBuffer, int n_byte)
    {
    	char output[32];
    	for (int i = (n_byte-1); i >= 0; i--)
    	{
    		int n0 = sBuffer[i] & 0x0F;
    		int n1 = (sBuffer[i] & 0x0F) >> 4;
    		output[i] = ((n1 + n0) << 4) * 10;
    	}
    	//Convert array into string.
    	std::string str(reinterpret_cast<const char *>(output), 32);
    	//Return string to be displayed on console.
    	return str;
    }
    Idea being that as the data is read in it is stored in a 32 byte array and then passed to this method to be converted and returned to display on screen, although still not 100% yet (hopefully not too far off though).

    Thanks for the help oracleguy


  •  

    Posting Permissions

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