Go Down

Topic: i2c byte data to char (Read 613 times) previous topic - next topic

Ben_der

Hello !

How do i convert Byte (Uint8_t) comming from:

Code: [Select]


uint8_t datastring;

Wire.requestFrom(0x60,67); //Lets fetch the stuff; 67Bytes of fun.
datastring |= Wire.receive();



to a char string?
Lots of commas in between, but they are cruciel to be kept.
further on - last two bytes will be
<cr><lf>k

as far as I know char is signed in Arduino -127/+128 and datastring will be 0-255

thanks for Help !

Catcher

I'm curious to know also.

My first try would be type casting.
Does this work?

char test = (char)datastring;


Nick Gammon

Something like this should work. I wouldn't worry about the different data types, the compiler isn't.

Code: [Select]
const int WANTED_NUMBER = 67;

uint8_t datastring [WANTED_NUMBER];

bool getData ()
{

  if (Wire.requestFrom (0x60, WANTED_NUMBER) < WANTED_NUMBER)
    return false;  // didn't get number we wanted

// get each byte
  for (byte i = 0; i < WANTED_NUMBER; i++)
    datastring [i] = Wire.receive ();
   
  return true;  // OK
 
}  // end of getData
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

If you are planning to treat it as a "C" string you might add a terminating 0x00 byte:

Code: [Select]
const int WANTED_NUMBER = 67;

uint8_t datastring [WANTED_NUMBER + 1];

bool getData ()
{


  if (Wire.requestFrom(0x60, WANTED_NUMBER) < WANTED_NUMBER)
    return false;  // didn't get number we wanted

// get each byte
  for (byte i = 0; i < WANTED_NUMBER; i++)
    datastring [i] = Wire.receive ();
   
  datastring [WANTED_NUMBER] = 0x00;  // terminating null
 
  return true;  // OK
 
}
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Ben_der


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy