How to convert an int value to a hex address?

Hello,

the code is looking for an active I2C device.
The result is an integer value. (90 i.e.).
To communicate wit the device, i have the problem that i have to convert int “90” to an integer address like “0x5A<<1”
Is there a special “thing” to converting or recalculating or … ? (that is not a string!)

9010 is 0x5a

That was not the question…
90 is 5A, but not 0x5A<<1

int a = 90 and int a = 0x5A is the same no conversion or recalculation is needed. 0x5A << 1 is 0xB4 in result it is the same as 90 << 1 or 90*2 or 180.

int addressInDec = 90;


if(address == 0x5A){ 
     Serial.println("This line will always be printed");
}

I’m not sure what the purpose of the bitshift is on your address.

0x5A << 1 == 0xB4 == 180

Does this help at all?

That’s all ok, but i need a conversion like that:
int dev = 0x5A<<1;

If dev is only in hex, this will not work: i2c_start_wait(dev+I2C_WRITE);

http://xyproblem.info/

This is the Programming Questions part of the forum.

Where is your Program?

Rubbernose:
That’s all ok, but i need a conversion like that:
int dev = 0x5A<<1;

If dev is only in hex, this will not work: i2c_start_wait(dev+I2C_WRITE);

Rubbish.

dev is a variable. It can’t be “in hex”.

Ok, here is the rubbish: http://wiki.wiring.co/wiki/Connecting_Infrared_Thermometer_MLX90614_to_Wiring#Setup_diagram_3 (last code...) That doesn't work if the address changes, or a sensor is unplugged. So i have to search for and get an int value.

Rubbernose:
That was not the question…
90 is 5A, but not 0x5A<<1

Yes, well 4 is 0x04 but not 0x04<<1.

You could say the same thing about any number.

You are right.
Back to the headline.
How can i convert the int value to 0xXX<<1 (address)?

I’m totally lost. :fearful:

1 is opposite << 1 operation. I2C needs 7 bit address so this is the reason why (address << 1 ) is passed to the function - 7 most significant bits are used. Use >> 1 to obtain address back.
Maybe more detailed description of your problem could help.

Hello and once more…
The code works well but needs a precise address (0xXX<<1).

#include <i2cmaster.h>

int device1Address = 0x50<<1;   // 0x50 is the assigned address for I²C 
                                // communication for sensor 1.
                                // Shift the address 1 bit right, the 
                                // I²Cmaster library only needs the 7 most 
                                // significant bits for the address.
int device2Address = 0x55<<1;   // 0x55 is the assigned address for I²C 
                                // communication for sensor 2.
                                // Shift the address 1 bit right, the 
                                // I²Cmaster library only needs the 7 most 
                                // significant bits for the address.

float celcius1 = 0;             // Variable to hold temperature in Celcius
                                // for sensor 1.
float fahrenheit1 = 0;          // Variable to hold temperature in Fahrenheit
                                // for sensor 1.
float celcius2 = 0;             // Variable to hold temperature in Celcius
                                // for sensor 2.
float fahrenheit2 = 0;          // Variable to hold temperature in Fahrenheit
                                // for sensor 2.

void setup()
{
  Serial.begin(9600);           // Start serial communication at 9600bps.
  
  i2c_init();                               // Initialise the i2c bus.
  PORTC = (1 << PORTC4) | (1 << PORTC5);    // Enable pullups.
}

void loop()
{
  celcius1 = temperatureCelcius(device1Address);// Read's data from MLX90614
  celcius2 = temperatureCelcius(device2Address);// with the given address,
                                                // transform's it into
                                                // temperature in Celcius and
                                                // store's it in the celcius1
                                                // or celcius2 variables.
 
  fahrenheit1 = (celcius1*1.8) + 32;     // Converts celcius into Fahrenheit 
  fahrenheit2 = (celcius2*1.8) + 32;     // and stores in Fahrenheit1 or 
                                         // Fahrenheit2 variables.

  Serial.print("Sensor 1: Celcius: ");   // Prints all readings in the Serial 
  Serial.print(celcius1);                // port.
  Serial.print("  Fahrenheit: ");
  Serial.println(fahrenheit1);
  Serial.print("Sensor 2: Celcius: ");
  Serial.print(celcius2);
  Serial.print("  Fahrenheit: ");
  Serial.println(fahrenheit2);

  delay(1000);                         // Wait a second before printing again.
}

float temperatureCelcius(int address) {
  int dev = address;
  int data_low = 0;
  int data_high = 0;
  int pec = 0;

  // Write
  i2c_start_wait(dev+I2C_WRITE);
  i2c_write(0x07);

  // Read
  i2c_rep_start(dev+I2C_READ);
  data_low = i2c_readAck();       // Read 1 byte and then send ack.
  data_high = i2c_readAck();      // Read 1 byte and then send ack.
  pec = i2c_readNak();
  i2c_stop();

  // This converts high and low bytes together and processes temperature, 
  // MSB is a error bit and is ignored for temps.
  double tempFactor = 0.02;       // 0.02 degrees per LSB (measurement 
                                  // resolution of the MLX90614).
  double tempData = 0x0000;       // Zero out the data
  int frac;                       // Data past the decimal point

  // This masks off the error bit of the high byte, then moves it left 
  // 8 bits and adds the low byte.
  tempData = (double)(((data_high & 0x007F) << 8) + data_low);
  tempData = (tempData * tempFactor)-0.01;
  float celcius = tempData - 273.15;
  
  // Returns temperature un Celcius.
  return celcius;
}

I don’t have the number or the addresses of the sensors.
So i have to search for. That works perfect but i get the address in an integer value (90 i.e.).
Now i have to convert the integer value to the integer (address) value (0x5A<<1) ?

Sigh

If you have a decimal integer value of 90, which is the same as 0x5A, then 90<<1 is the same as 0x5A<<1

90 (decimal) <<1 equals 180 (decimal)
5A (hex) <<1 equal B4 (hex)

180 (decimal) equals B4 (hex)

If you are using Windows try it for yourself in the calculator in Programmer mode.

I use Linux, but nevertheless i can convert dec to hex.
But that is not the question.
I try to write a code that converts the found decimal address into an integer address (0xXX<<1)

That works perfect but i get the address in an integer value (90 i.e.).
Now i have to convert the integer value to the integer (address) value (0x5A<<1) ?

I truly do not see a problem here, unless you’re treating the result of “(0x5A<<1)” as a “char”, in which case it will be seen as a negative number.

I’m a beginner and it is very hard to understand that “int Address = 0x5A<<1;” is an integer value and “Address” can’t be generated from the integer value 90.
I give up now after playing around for days.

All the possible addresses are reduced to 6 ones. All the 6 addresses are checked for I2C device. If something is found the results read out. That will work!

it is very hard to understand that “int Address = 0x5A<<1;” is an integer value and “Address” can’t be generated from the integer value 90.

It is impossible for me to read int Address = 0x5A<<1; and not see int Address = 90 << 1;.

Good god!
AWOL that is it: device1Address is the found device number.

Wire.beginTransmission(device1Address);
celcius1 = temperatureCelcius(device1Address<<1)

Thats so easy! Thanks!

Solved!