Go Down

Topic: How to conversion HEX to DEC? or String to int ? (Read 4114 times) previous topic - next topic

knee266

Hello~

I got the problem for HEX to DEC conversion.
I read a HEX from I2C device (ex.0xFE), but I need conversion to DEC to do other operation.

ex.
0xFE =254 (DEC)
int a= 254 *0.0005432


May I know how to conversion HEX to DEC?


Code: [Select]

V3_HEX = 0xFE;   // simulation HEX from I2C device
String stringOne = String(V3_HEX,DEC);    //conversion HEX to DEC
int a=stringOne;    << what can't I use this command ?


thank for you help

musskopf

#1
Jun 14, 2016, 07:48 am Last Edit: Jun 14, 2016, 07:54 am by musskopf
Hello knee266,

You basically don't convert HEX to another data type. HEX is just a way to represent the data. Here a quick example:

Code: [Select]
uint8_t var1 = 65; // Small int
uint8_t var2 = 0x41 // Small int

Serial.println(var1 + var2, DEC); //This should output 130
Serial.println(var1 + var2, HEX); //This should output 82 (or 0x82)


You just need to take care when you're manipulating variable of different sizes, for example, if you try to do the operation:
uint8_t varX = (0xF1 + 0x80) // 241 + 128
The result will be 0x71 (or 113 DEC) and not 369 because the variable size is only 8 bits and it'll overflow.

Now if you change the variable size:
uint16_t varY = (0xF1 + 0x80) // 241 + 128
The result will be 0x171 (or 369 in DEC)


When reading data some sensors, you normally will ready byte by byte, so it's very common to "join" 2 uint8_t values to form a uint16_t value. For example:
Code: [Select]
uint8_t varA = 0x01; //1 Dec
uint8_t varB = 0x71; //113 Dec
uint16_t myVar = 0;

myVar = varA; //1 Dec
myVar <<= 8; //Left shit 8 bits, so from 1 Dec it's not 256 Dec. From 0x01 to 0x100;
myVar = myVar | varB; //OR operation, basically 0x100 merged with 0x71, which will result in 0x171

Serial.println(myVar,DEC);
Serial.println(myVar,HEX);

The above is just a very simple example, that can even be reduced to a few operations, but should give an ideal. At the end, the datasheet for the Sensor/Device where you're reading the data from will tell if the reading is an 8 bit, 10, 16 bit, etc... also will tell if the data is MSB or LSB.

I personally like to user uint8_t and uint16_t instead of "small unsigned int" and "unsigned int" as it's easier to see the variable size.

You might wanna have a look on this link, which will explain all bit manipulations: http://playground.arduino.cc/Code/BitMath

Cheers
http://talk2.wisen.com.au

knee266

Hello~ musskopf

thanks for your help.

The function works properly now, my new sketch as below:

Code: [Select]

V3_HEX = 0xFE;   // simulation HEX from I2C device
V3_DEC = V3_HEX *2 * 0.0078125;
Serial.println (V3_DEC,2);  // the result = 3.97 (DEC)



but I can not understand :  V3_HEX=0xFE there is HEX , (*2) and (0.0078125) are DEC.
Does Arduino can support mixed operating?

musskopf

The content of a variable is actually always binary. HEX, DEC, CHAR is only an interpretation of that value and not the value itself.

For example:
HEX 0x41 = 0b1000001
INT 65 = 0b1000001
CHAR 'A' = 0b1000001

As you can see the "content" of the variable in memory is the same... as the MCU does all operations in binary, it really doesn't matter.

Cheers
http://talk2.wisen.com.au

knee266

Hello musskopf,


Understood, thank you very much.



knee266

#5
Jun 15, 2016, 09:31 am Last Edit: Jun 16, 2016, 10:25 am by knee266
Hello musskopf

may I have your help again?
I would like use arduino to read I2C data from PSU, but my I2C sketch can not read any volume from PSU.
below is my sketch, can you help to check if I got some mistake.









musskopf

I haven't looked in detail into your code but have you tried to run the I2C scanner to confirm the device is there? 

http://playground.arduino.cc/Main/I2cScanner


Also in the getData function you're have the endTransmission after the return... so it never gets executed if the function is returning something.

Cheers 
http://talk2.wisen.com.au

knee266

Hello musskop,

 thank for your help,
I have no use I2Cscanner,  but I am sure the slave address is 0x5A.

I tried to place endTransmission before return, but arduion I2C still no any data response  from slave,  then I connect slave device to other I2C meter,  the slave device works peoperly.

Do you have any suggest? Ibelieve my sketch  still have some issue for I2C code.

knee266

Hello musskop,

My sketch were works now, the root cause form Arduino I2C issue.

The slave I2C address define is 0x5A, but when I use I2CSCANNER to scan address and found the slave I2C address become to 0x2D.

Do you know what happened?


many thanks.



I haven't looked in detail into your code but have you tried to run the I2C scanner to confirm the device is there?

http://playground.arduino.cc/Main/I2cScanner


Also in the getData function you're have the endTransmission after the return... so it never gets executed if the function is returning something.

Cheers

musskopf

Glad it's working! What part/manufacturer is that?

Do you have the datasheet details explaining the I2C address?

Some I2C devices have a single address but some you are able to change it by pulling up/down some pins on the device.

Cheers,
http://talk2.wisen.com.au

knee266

Hello musskop,


I found out what I2C address from 0x5A become to 0x2D issue, there is cause from Arduino I2C.
Arduino I2C only have 7bit?


0x5A:
0101  1010

then right shift 1 bit :

x010  1101 > 0x2D


thanks again.

musskopf

Have a look on this link about I2C address size: http://www.totalphase.com/support/articles/200349176-7-bit-8-bit-and-10-bit-I2C-Slave-Addressing

But the part datasheet should also have this information.

Cheers!
http://talk2.wisen.com.au

knee266

Hello musskop,


Slave I2C spec screenshot  as attached that define address is 0x5A.

Red block address identify by Arduino , green is IC default.

Is it mean Arduino ignore R/W bit?

thanks.



 

Go Up