Char array to hex string C++

前端 未结 8 650
时光取名叫无心
时光取名叫无心 2020-12-05 06:42

I searched char* to hex string before but implementation I found adds some non-existent garbage at the end of hex string. I receive pa

8条回答
  •  没有蜡笔的小新
    2020-12-05 07:39

    You can try this code for converting bytes from packet to a null-terminated string and store to "string" variable for processing.

    const int buffer_size = 2048;
    // variable for storing buffer as printable HEX string
    char data[buffer_size*2];
    // receive message from socket
    int ret = recvfrom(sock, buffer, sizeofbuffer, 0, reinterpret_cast(&from), &size);
    // bytes converting cycle
    for (int i=0,j=0; i

    When we send message with hex bytes 0x01020E0F, variable "data" had char array with string "01020e0f".

提交回复
热议问题