C++十六进制字符流转二进制

博客介绍了类似Python中bytes.fromhex()的功能,即从十六进制字符流读取二进制数据,还举例说明了tcpdump下来的数据复制后的十六进制流,并给出了相关参考链接。
部署运行你感兴趣的模型镜像

类似Python中的 bytes.fromhex(), 从十六进制字符流读取二进制数据

比如,tcpdump下来的data, 我们copy后的hex stream, 如:

b081fd20858480b081fd5f988580b1818c…

// ------------------------------------------------------------------
/*!
    Required headers
*/
#include <string>
#include <sstream>
#include <iomanip>

// ------------------------------------------------------------------
/*!
    Convert a block of data to a hex string
*/
void toHex( void* const  data,       //!< Data to convert
            const size_t dataLength, //!< Length of the data to convert
            std::string& dest        //!< Destination string
)
{
    unsigned char*    byteData = reinterpret_cast<unsigned char*>( data );
    std::stringstream hexStringStream;

    hexStringStream << std::hex << std::setfill( '0' );
    for( size_t index = 0; index < dataLength; ++index )
        hexStringStream << std::setw( 2 ) << static_cast<int>( byteData[index] );
    dest = hexStringStream.str();
}

/*!
    Convert a hex string to a block of data
*/
void fromHex( const std::string& in,   //!< Input hex string
              void* const        data, //!< Data store
              size_t&            len   //!< data length
)
{
    size_t         length = in.length();
    unsigned char* byteData = reinterpret_cast<unsigned char*>( data );

    std::stringstream hexStringStream;
    hexStringStream >> std::hex;
    size_t dataIndex = 0;
    for( size_t strIndex = 0; strIndex < length; ++dataIndex )
    {
        // Read out and convert the string two characters at a time
        const char tmpStr[3] = {in[strIndex++], in[strIndex++], 0};
        // Reset and fill the string stream
        hexStringStream.clear();
        hexStringStream.str( tmpStr );

        // Do the conversion
        int tmpValue = 0;
        hexStringStream >> tmpValue;
        byteData[dataIndex] = static_cast<unsigned char>( tmpValue );
    }
    len = dataIndex;
}

https://tweex.net/post/c-anything-tofrom-a-hex-string/

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

以下是几种将字节流转十六进制字符串C++实现方式: ### 方式一:通过位运算和字符拼接 ```cpp #include <iostream> #include <string> std::string bytestohexstring(char* bytes, int bytelength) { std::string str(""); std::string str2("0123456789abcdef"); for (int i = 0; i < bytelength; i++) { int b; b = 0x0f & (bytes[i] >> 4); str.append(1, str2.at(b)); b = 0x0f & bytes[i]; str.append(1, str2.at(b)); } return str; } ``` ### 方式二:使用 `uint8_t` 数组和预分配空间 ```cpp #include <iostream> #include <string> #include <vector> std::string bytesToHex(const uint8_t* data, size_t len, bool uppercase = true) { const char* hex_table = uppercase ? "0123456789ABCDEF" : "0123456789abcdef"; std::string hex_str; hex_str.reserve(len * 2); for (size_t i = 0; i < len; ++i) { uint8_t byte = data[i]; hex_str.push_back(hex_table[(byte >> 4) & 0x0F]); hex_str.push_back(hex_table[byte & 0x0F]); } return hex_str; } std::string bytesToHex(const std::vector<uint8_t>& data, bool uppercase = true) { return bytesToHex(data.data(), data.size(), uppercase); } ``` ### 方式三:利用流输出 ```cpp #include <iostream> #include <string> #include <sstream> #include <iomanip> #include <cstdint> std::string ToHexString(unsigned char* input, int datasize) { std::stringstream ss; ss << std::setbase(16) << std::setfill('0'); for (int i = 0; i < datasize; i++) ss << std::setw(2) << (unsigned int)input[i]; return ss.str(); } ``` ### 示例调用代码 ```cpp #include <iostream> #include <vector> int main() { // 方式一示例 char bytes[] = {0x12, 0x34, 0xAB}; std::string hexStr1 = bytestohexstring(bytes, sizeof(bytes)); std::cout << "Method 1: " << hexStr1 << std::endl; // 方式二示例 std::vector<uint8_t> byteVector = {0x12, 0x34, 0xAB}; std::string hexStr2 = bytesToHex(byteVector); std::cout << "Method 2: " << hexStr2 << std::endl; // 方式三示例 unsigned char input[] = {0x12, 0x34, 0xAB}; std::string hexStr3 = ToHexString(input, sizeof(input)); std::cout << "Method 3: " << hexStr3 << std::endl; return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值