i'm doing a project in C++
the function hx_drv_uart_getchar() only receives 1 uint8_t dtype at a time. how can i convert all received characters from this function into a float array?
the string i am sending over here will be like "-1.2341, 0.9871, 4.5121~" where ~ is to let receiver know its the end of the string
so the characters i am receiving will be something like '-' -> '1' ->'.' ->'2' ->'3' ->'4'-> '1'->','-> so on and so forth
how can i concatenate them and convert it into a float array: arr = {-1.2341, 0.9871, 4.5121}?
the code below is my implementation but acceldata is "45494650515249" after converting. i am suspecting its in ascii form.
so my question is how can i convert received uint8_t data to string and then convert the whole string to a float array? or are there any straightforwards ways? i've been stuck here for a few days already... please advise thank you :)
std::string acceldata;
uint8_t getchar = 0;
hx_drv_uart_getchar(&get_ch);
acceldata = get_ch;
while(get_ch != '~'){
hx_drv_uart_getchar(&get_ch);
acceldata += std::to_string(get_ch);
}
Related
I'm trying to convert a byte array into a string using the following code.
String b = String((char*)buffer2);
but when I output the string I get a very strange result that contains white spaces and special characters that normally shouldn't be in the string.
buffer2 is of the type byte and its length is 18.
this is how its declared:
byte buffer2[18];
When I use the following code to print the byte array I get the results I expect.
for (uint8_t i = 0; i < 16; i++) {
Serial.write(buffer2[i] );
}
I'm wondering how I can convert a byte array to a string the proper way.
I'm trying to create a binary representation of a string of characters but could only print them using serial.print(arr,BIN);
Is there a way to save the binary values of each char of the string to an array or even a long number?
* Working on Arduino sketch (C++) if it makes any difference.
I used this code trying to create an array but couldn't make it work:
void loop() {
String Message = "Hello World";
int l = Message.length();
int BinMessage[l];
for (int j=0; Message[j] != NULL; j++){
BinMessage[j] = String(Message[j], BIN);
Serial.println(BinMessage);
}
One option might be to use bitRead function to read the bits of each character one by one, then store those values as characters in a new string.
I'm havig some trouble figuring out how to conver my uint8 vector to stringstream.
On input im getting vector of uint8, first 9 bytes are flags which i dont need in my string stream, next 2 bytes are some data i need as string, let's call them "name1", next 2 bytes are another name, lest call it "name2", then comes 4 bytes which are some uint32 number(but writen as 4 uint8 bytes), lets call it just "number". Now i need to pass these data to stringstream but:
name1 and number2 need to be written as bytes (byte 0x52 -> char[2]={"5","2"})
number needs to be casted to uint32
all variables need to be seperated by semicolons in final stringstream
so if im getting a vector like this one:
---some 9 bytes---, 0x05, 0x00, 0x01,0x00,0x00,0x00,0x08,0x0E, ---some other data---
i need stringstream to be like this:
"0500;0100;2062;"
i have managed to figure out how to cast number to uint32:
uint8_t tab[4];
for(int i=4; i!=0; --i)
{
tab[4-i]=data[i+14];
}
uint32_t* var = (uint32_t*)tab;
is there some better way to do this?
EDIT:
How can i pass uint8 values to string as characters?
example:
byte ouput: 0x05
string output: 05
can i put string to stringstream using "<<" operator or is it not recommended?
if you have a string, just loop on the string and output the characters to your stringstream
std::string name = "name1";
std::stringstream ss;
for(auto c : name)
ss << std::static_cast<int>(c);
ss << ";";
As a side note:
uint32_t* var = (uint32_t*)tab;
is totally useless, you don't need a pointer.
i'm not sure this will solve your problem, but i would take a different approach.
i would use a struct to describe the underlying protocol and then continue with that. example:
struct dx{
uint8 _junk0[9];
char name1[2];
char name2[2];
uint32 num;
} __attribute__((packed));
uint8 *input;
dx* struct=(dx*)input;
printf("%d",dx->num);
I will briefly explain what I want to do and help appreciated.
I have a hex number which is formatted as 16 byte number like this:
1: std::string myhex = "00000000000000000000000000000FFD";
Then I want to convert it to int. Which I think I successfully do using this:
// convert hex to int
unsigned int x = strtoul(myhex.c_str(), NULL, 16);
printf("x = %d\n", x); // prints 4093 as needed
Now, I want to convert this integer back to hex. Which I think I also successfully do using this:
// Convert int back to hex
char buff[50];
string hexval;
sprintf(buff,"%x",x);
hexval = buff;
cout << hexval.c_str(); // prints "ffd".
But my problem is that now, I want to convert the "ffd" string as above back to the format it was before, e.g., 16 byte number padded with zeros like this:
00000000000000000000000000000FFD
I want to convert the string not only print it.
Any help how to do this?
Also any corrections if anything I was achieving above is wrong or not OK are welcome.
Preferably I would like this to compile on Linux also.
Use the 0 flag (prefix) for zero-padding and field width specification in a printf:
printf("%032X", x);
Use snprintf to store it in your string:
snprintf(buff, sizeof(buff), "%032X", x);
Or use asprintf to store it in a newly-allocated string, to be certain that the memory available for the string is sufficient (since it's allocated by asprintf):
char *as_string = NULL;
asprintf(&as_string, "%032X", x);
I currently have the following code
char my_stream[800];
std::string my_string;
iResult = recv(clntSocket,my_stream,sizeof(my_stream),0);
my_string = std::string(my_stream);
Now when I attempt to convert the char array to string I get the present of weird characters in the string any suggestions on what I might be doing wrong
You're getting weird characters because your strings length is not equal to the number of bytes received.
You should initialize the string like so:
char* buffer = new char[512];
ssize_t bytesRead = recv(clntSocket,buffer,512,0);
std::string msgStr = std::string(buffer,bytesRead);
delete buffer;
The most common solution is to zero every byte of the buffer before reading anything.
char buffer[512];
buffer = { 0 };
If you're reading in a zero-terminated string from your socket, there's no need for a conversion, it's already a char string. If it's not zero-terminated already, you'll need some other kind of terminator because sockets are streams (assuming this is TCP). In other words, you don't need my_string = std::string(my_stream);
have you tried to print my_stream directly without converting to string.
According to me it may be the case of mismatch in format of data sent and received.
data on other side may be in other format like Unicode and you may be trying to print it as single byte array
if only part of string is in weird characters than it is definitely error related to null terminator at the end of my_stream missing tehn increase the size of array of my_stream.