13

I transfer message trough a CAN protocol.

To do so, the CAN message needs data of uint8_t type. So I need to convert my char* to uint8_t. With my research on this site, I produce this code :

    char* bufferSlidePressure = ui->canDataModifiableTableWidget->item(6,3)->text().toUtf8().data();//My char*

    /* Conversion */
    uint8_t slidePressure [8];
    sscanf(bufferSlidePressure,"%c",
        &slidePressure[0]);

As you may see, my char* must fit in sliderPressure[0].

My problem is that even if I have no error during compilation, the data in slidePressure are totally incorrect. Indeed, I test it with a char* = 0 and I 've got unknow characters ... So I think the problem must come from conversion.

My datas can be Bool, Uchar, Ushort and float.

Thanks for your help.

Evans Belloeil
  • 2,413
  • 7
  • 43
  • 76

4 Answers4

13

Is your string an integer? E.g. char* bufferSlidePressure = "123";?

If so, I would simply do:

uint8_t slidePressure = (uint8_t)atoi(bufferSlidePressure);

Or, if you need to put it in an array:

slidePressure[0] = (uint8_t)atoi(bufferSlidePressure);

Edit: Following your comment, if your data could be anything, I guess you would have to copy it into the buffer of the new data type. E.g. something like:

/* in case you'd expect a float*/
float slidePressure;
memcpy(&slidePressure, bufferSlidePressure, sizeof(float));

/* in case you'd expect a bool*/
bool isSlidePressure;
memcpy(&isSlidePressure, bufferSlidePressure, sizeof(bool));

/*same thing for uint8_t, etc */

/* in case you'd expect char buffer, just a byte to byte copy */
char * slidePressure = new char[ size ]; // or a stack buffer 
memcpy(slidePressure, (const char*)bufferSlidePressure, size ); // no sizeof, since sizeof(char)=1
Carmellose
  • 4,815
  • 10
  • 38
  • 56
  • Yes it can be, but it also can be a boolean or a float, is this work the same way ? – Evans Belloeil Aug 18 '14 at 10:19
  • @EvansBelloeil If I understand correctly, I would probably do something using `memcpy()` (see my edit). – Carmellose Aug 18 '14 at 10:41
  • It seems okay, but I also need have char * now :( – Evans Belloeil Aug 18 '14 at 12:38
  • @EvansBelloeil No conversion needed then :) See edit. – Carmellose Aug 18 '14 at 13:08
  • Thanks but I need this char* in format uint8_t, the compilator doesn't accept your answer :( and told me there's an invalid conversion from char * to uint8_t – Evans Belloeil Aug 18 '14 at 13:36
  • 1
    @EvansBelloeil Then change `char` to `uint8_t` in my last example and that should work. If you want to copy to one `uint8_t`, then `size=1`. Otherwise you copy a series of `uint8_t`. See also [memcpy](http://www.cplusplus.com/reference/cstring/memcpy/) documentation. – Carmellose Aug 18 '14 at 13:42
2

uint8_t is 8 bits of memory, and can store values from 0 to 255

char is probably 8 bits of memory

char * is probably 32 or 64 bits of memory containing the address of a different place in memory in which there is a char

First, make sure you don't try to put the memory address (the char *) into the uint8 - put what it points to in:

char from;
char * pfrom = &from;
uint8_t to;
to = *pfrom;

Then work out what you are really trying to do ... because this isn't quite making sense. For example, a float is probably 32 or 64 bits of memory. If you think there is a float somewhere in your char * data you have a lot of explaining to do before we can help :/

Andy Newman
  • 1,178
  • 2
  • 8
  • 23
0

char * is a pointer, not a single character. It is possible that it points to the character you want.

uint8_t is unsigned but on most systems will be the same size as a char and you can simply cast the value.

You may need to manage the memory and lifetime of what your function returns. This could be done with vector< unsigned char> as the return type of your function rather than char *, especially if toUtf8() has to create the memory for the data.

Your question is totally ambiguous.

ui->canDataModifiableTableWidget->item(6,3)->text().toUtf8().data();

That is a lot of cascading calls. We have no idea what any of them do and whether they are yours or not. It looks dangerous.

CashCow
  • 30,981
  • 5
  • 61
  • 92
-1

More safe example in C++ way

char* bufferSlidePressure = "123";
std::string buffer(bufferSlidePressure);
std::stringstream stream;

stream << str;
int n = 0;

// convert to int
if (!(stream >> n)){
    //could not convert
}

Also, if boost is availabe

int n = boost::lexical_cast<int>( str )