c++ - Convert char* to uint8_t -


i transfer message trough can protocol.

to so, can message needs data of uint8_t type. need convert char* uint8_t. research on site, produce code :

    char* bufferslidepressure = ui->candatamodifiabletablewidget->item(6,3)->text().toutf8().data();//my char*      /* conversion */     uint8_t slidepressure [8];     sscanf(bufferslidepressure,"%c",         &slidepressure[0]); 

as may see, char* must fit in sliderpressure[0].

my problem if have no error during compilation, the data in slidepressure totally incorrect. indeed, test char* = 0 , 've got unknow characters ... think problem must come conversion.

my datas can bool, uchar, ushort , float.

thanks help.

is string number? e.g. char* bufferslidepressure = "123";?

if so, do:

uint8_t slidepressure = (uint8_t)atoi(bufferslidepressure); 

or, if need put in array:

slidepressure[0] = (uint8_t)atoi(bufferslidepressure); 

edit: following comment, if data anything, guess have copy buffer of new data type. e.g. like:

/* in case you'd expect float*/ float slidepressure; memcpy(&slidepressure, bufferslidepressure, sizeof(float));  /* in case you'd expect bool*/ bool isslidepressure; memcpy(&isslidepressure, bufferslidepressure, sizeof(bool));  /*same thing uint8_t, etc */  /* in case you'd expect char buffer, byte byte copy */ char * slidepressure = new char[ size ]; // or stack buffer  memcpy(slidepressure, (const char*)bufferslidepressure, size ); // no sizeof, since sizeof(char)=1 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -