Pages: [1]   Go Down
Author Topic: i2c byte data to char  (Read 519 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello !

How do i convert Byte (Uint8_t) comming from:

Code:

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 !
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 167
Safety first.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm curious to know also.

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

char test = (char)datastring;

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18807
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18807
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
 
}
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, that worked !!
Logged

Pages: [1]   Go Up
Jump to: