Convert char * to uint8_t

I send a message through the CAN protocol .

For this, the CAN message needs data of type uint8_t . So I need to convert my char * to uint8_t. With my research on this site, I am creating 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 can see, my char* should match sliderPressure[0] .

My problem is that even if I have no error during compilation, the data in the slide drink is absolutely incorrect . In fact, I am testing it with char* = 0 , and I have unknown characters ... Therefore, I think the problem should come from the conversion.

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

Thank you for your help.

+6
source share
4 answers

Is your string an integer? For example, char* bufferSlidePressure = "123"; ?

If so, I would just do:

 uint8_t slidePressure = (uint8_t)atoi(bufferSlidePressure); 

Or if you need to put it in an array:

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

Change: After your comment, if your data can be anything, I think you will have to copy it to the buffer of the new data type. For example, 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 
+7
source

uint8_t - 8 bits of memory and can store values ​​from 0 to 255

char is probably 8 bits of memory

char *, probably 32 or 64 bits of memory containing the address of another place in memory that has char

First, make sure that you are not trying to put the memory address (char *) in uint8 - put what it points to:

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

Then work out what you are really trying to do ... because it is not entirely clear. 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 explanations before we can help: /

+1
source

char * is a pointer, not a single character. Perhaps it indicates the character you need.

uint8_t is unsigned, but on most systems it will be the same size as char , and you can just specify a value.

You may need to manage the memory and lifetime of your function. This can be done using vector< unsigned char> as the return type of your function, not char * , especially if toUtf8 () should create memory for the data.

Your question is completely ambiguous.

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

This is a lot of cascading calls. We have no idea what they are doing and whether they are yours or not. It looks dangerous.

0
source

A safer example on C ++ paths

 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 ) 
0
source

Source: https://habr.com/ru/post/973968/


All Articles