Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 11 months ago.
3 Byte Hex to Decimal conversion on mbed
Does anyone have a good way to convert a 3 digit hex reading into decimal format ?
3 Answers
11 years, 11 months ago.
I don't understand what you want to convert, looking at your code from the other question you've managed to get all the data, (3 pairs of 12bit values), the code to convert them into 12 bit Integer looks right,
These Ints are not hex, they are just data, values 0 - 4095, or in hex 0 - FFF, but this is just a way to display that data (as base 10 or 16), you are selecting it in your printf statement with "%#X".
Erik Olieman mentions this in a comment on your other question.
cout and endl are part of the standard library for c++, cout replaces printf with a questionably better interface, they exist in the std namespace so can be used by using std::cout and std::endl, ( you could also use "using namespace std;" but this will pollute the global namespace so isn't advised ) after saying all that on the mbed I'd stick with printf, or better yet Serial.printf to avoid ambiguity.
Chris
Hi Chris,
You are correct, I now have things working, I re-assigned the 'int' as a 'float' and was able to apply conversion from there, i.e.
void ltc4151_ch1() { char addrch1 = 0xCE; char rRegch1[7] = {0,0,0,0,0,0,0}; char cmdch1 = 0x00; i2c.write(addrch1, &cmdch1, 1); i2c.read(addrch1, rRegch1, 7); float sensech1 = ((rRegch1[0] << 8) + rRegch1[1] >> 4); float vinch1 = ((rRegch1[2] << 8) + rRegch1[3] >> 4); float adinch1 = ((rRegch1[4] << 8) + rRegch1[5] >> 4); pc.putc(0x0D); float ift1 = ((sensech1/4096)*4096); pc.printf("CH1 IFT = %3.2f mA",ift1); float vft1 = ((vinch1/4096)*102.4); pc.printf("CH1 VFT = %3.2f V",vft1); float vfs1 = ((adinch1/4096)*4.096); pc.printf("CH1 VFS = %1.2f V",vfs1); }
posted by 22 Jan 201311 years, 11 months ago.
Not really sure what your asking, 3 bytes with hex values to decimal ? Hex is 4 bits so assuming the max value is 0x0f, try this sample:
long decValue = 0; uint8_t hexValue[4] = { 0x05, 0x0f, 0x08, 0x00 }; // assuming hexValue[0] is high byte uint8_t *hptr = hexValue; while ( *hptr ) { decValue = decValue*16 + *hptr++; // a regular hex 2 dec conversion } cout << decValue << endl;
11 years, 11 months ago.
Hi Mel,
Thanks for your response.
When trying to compile this code under mbed compiler it returns an error associated with 'cout' and 'endl' as undefined - should these be part of the #define "mbed.h" library?
Fraser
p.s. this question is in relation to a previous posted question:
http://mbed.org/questions/296/LTC4151-I2C-comms/
Essentially I have been able to readback 6 register bytes from an LTC4151 chip, and have managed to combine the upper 8-bits in the first register with the upper 4-bits in the second register forming a 3-byte hex value (i.e. from register 00h and 01h - repeated for registers 02h & 03h and registers 04h & 05h) and from these 3-byte hex values need to convert to a decimal value in order to perform the following example conversion:
float conv = ((val(dec) / 4096)*102.4));
Hope this helps to explain the issue further. Again any assistance would be much appreciated, and thanks in advance.
Fraser
do you mean printing to P.C. terminal, or display. or do you need to break the number into hundreds, tens and units. The first will be taken care of using printf. The second could be done using sprintf,
if this wasn't what you wanted please rephrase your question, possibly with examples.
Ceri
posted by ceri clatworthy 22 Jan 2013