Char array to hex string C++

前端 未结 8 534
时光取名叫无心
时光取名叫无心 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:26

    I've found good example here Display-char-as-Hexadecimal-String-in-C++:

      std::vector<char> randomBytes(n);
      file.read(&randomBytes[0], n);
    
      // Displaying bytes: method 1
      // --------------------------
      for (auto& el : randomBytes)
        std::cout << std::setfill('0') << std::setw(2) << std::hex << (0xff & (unsigned int)el);
      std::cout << '\n';
    
      // Displaying bytes: method 2
      // --------------------------
      for (auto& el : randomBytes)
        printf("%02hhx", el);
      std::cout << '\n';
      return 0;
    

    Method 1 as shown above is probably the more C++ way:

    Cast to an unsigned int
    Use std::hex to represent the value as hexadecimal digits
    Use std::setw and std::setfill from <iomanip> to format
    Note that you need to mask the cast int against 0xff to display the least significant byte:
    (0xff & (unsigned int)el).

    Otherwise, if the highest bit is set the cast will result in the three most significant bytes being set to ff.

    0 讨论(0)
  • 2020-12-05 07:29

    The simplest:

    int main()
    {
        const char* str = "hello";
        for (const char* p = str; *p; ++p)
        {
            printf("%02x", *p);
        }
        printf("\n");
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-05 07:30

    You could use std::hex

    Eg.

    std::cout << std::hex << packet;
    
    0 讨论(0)
  • 2020-12-05 07:32

    Here is something:

    char const hex_chars[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    
    for( int i = data; i < data_length; ++i )
    {
        char const byte = data[i];
    
        string += hex_chars[ ( byte & 0xF0 ) >> 4 ];
        string += hex_chars[ ( byte & 0x0F ) >> 0 ];
    }
    
    0 讨论(0)
  • 2020-12-05 07:34

    Using boost:

    #include <boost/algorithm/hex.hpp>
    
    std::string s("tralalalala");
    std::string result;
    boost::algorithm::hex(s.begin(), s.end(), std::back_inserter(result));
    
    0 讨论(0)
  • 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<SOCKADDR *>(&from), &size);
    // bytes converting cycle
    for (int i=0,j=0; i<ret; i++,j+=2){ 
        char res[2]; 
        itoa((buffer[i] & 0xFF), res, 16);
            if (res[1] == 0) {
                data[j] = 0x30; data[j+1] = res[0];
            }else {
                data[j] = res[0]; data[j + 1] = res[1];
            }
    }
    // Null-Terminating the string with converted buffer
    data[(ret * 2)] = 0;
    

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

    0 讨论(0)
提交回复
热议问题