I need to take a value out of an unsigned short and add it to a char array.
ex.:
unsigned short a = 6849;
char b[20];
any idea about how to place "6849" into b ?
Thank you !
I need to take a value out of an unsigned short and add it to a char array.
ex.:
unsigned short a = 6849;
char b[20];
any idea about how to place "6849" into b ?
Thank you !
your task doesn't really make sense. do you have a more complete explanation of what you are required to do?
xxxxxxxxxan unsigned short is 2 bytes. you can do shifts and masks to get the LSB and MSB and put them in b[0] and b[1] if thats what you want.
never mind. you probably want to turn 6849 into a printable string?
one way is
Code:count = snprintf(b,sizeof(b),"%u",a);
Last edited by dmh2000; 03-28-2011 at 11:23 AM.
I'm totally lost
I'm sending a char[] through a socket and I want to send some numeric information.
I guess you mean using those operators >> << ...
I will go read about this !
Thank you
i was thinking too much. if you want to use iostreams, then using << is the way to go. but do you want to send it as binary data or as ascii string?
Last edited by dmh2000; 03-28-2011 at 11:26 AM.
Hummmm
I don't know much about hidden functionnality behing sockets but I already send some text ("ABCDE") through a char and i can get it back on the other side ("ABCDE") .
i guess it's called ascii string ?
is count supposed to be a char[] ?
I suggest you use stringstreams to convert data to a string before sending it:
Now you can send the raw string by using the c_str() member function to get the string and size() to get the length.Code:std::stringstream stream; stream << my_integer; std::string mydata = stream.str();
To extract the data on the other end, you can do:
Code:std::stringstream stream(received_data); stream >> my_data;