As the title says I try to convert to byte data type a C++ String of Bytes and count bytes. The string I get from textbox will contain a series of one byte hexadecimal numbers, but I need to send it as bytes.
char packet_data[200];
HWND hTextBox2 = GetDlgItem(TabOneDlg,IDC_EDIT3);
SendMessageA(hTextBox2, WM_GETTEXT, (WPARAM)200, (LPARAM)packet_data);
That's how I get the input value (I am using win32 API - unmanaged forms)
EXAMPLE of input string (hex)
AA BB CC DD - 4 bytes !
In SHORT I want to do this : Got a string containing a textual representation of hexadecimal numbers, and I want to convert each textual representation of the hexadecimal numbers to "normal" numbers.
If you are sure that the hexadecimal numbers are separated by a space (as shown in the question) it is a simple question of extracting them. The simplest way in C++ is by using std::istringstream
and the normal input operator >>
:
std::istringstream istr(packet_data);
std::vector<uint8_t> data;
uint8_t i;
while (istr >> std::hex >> i)
data.push_back(i);
After the above code, the vector data
will have all data from the string. If you need to eg send the data over a socket (or similar) you can use std::vector::data
to get a raw pointer to the data (or use &data[0]
if the data
function doesn't exist), and the number of bytes is available from std::vector::size
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.