简体   繁体   English

C ++将Wwn字符串转换为识别为十六进制的数据类型

[英]C++ convert wwn string to data type recognized as hex

A project I am working on already has the wwn of a iscsi device stored as a std::string less the colons. 我正在处理的项目已经将iscsi设备的wwn存储为std :: string而不是冒号。 I have to grab a subset of the string and XOR them as if they were bytes with another set of bytes for a scsi command security code. 我必须获取字符串的子集,然后对它们进行XOR运算,就好像它们是带有另一个字节集的字节一样,用于scsi命令安全代码。 Which data type should I be converting the characters too so that the XOR treats them as hex bytes. 我也应该转换哪种数据类型的字符,以便XOR将它们视为十六进制字节。 The numbers are already exactly what I need but the compiler is going to interpret them as ASCII. 这些数字已经完全符合我的需要,但是编译器将把它们解释为ASCII。 I just need a way to tell the compiler these are already hex bytes. 我只需要一种方法告诉编译器这些已经是十六进制字节。

The string is 601ad142 so i need to convert it too [0x60, 0x1a, 0xd1, 0x42] so I can xor each byte with [0x57, 0x68, 0x6f, 0x61] 字符串是601ad142,所以我也需要将其转换为[0x60、0x1a,0xd1、0x42],因此我可以将每个字节与[0x57、0x68、0x6f,0x61]异或

If I can convert the wwn string: 600601601ad14200b9265b5b274efb84 (already provided in the existingcode) to a uinit64_t I can work with that too but: 如果我可以将wwn字符串转换为: 600601601ad14200b9265b5b274efb84 (已在现有代码中提供)到uinit64_t,我也可以使用它,但:

std::string wwid(path.wwid);
wwid.erase(std::remove(wwid.begin(), wwid.end(), ':'), wwid.end());//remove colons
uint64_t wwn = wwid
std::istringstream strWwid(wwid);
strWwid >> wwn;

return 0x23cc7401 返回0x23cc7401

UPDATE : I found a working solution. 更新 :我找到了一个可行的解决方案。

std::string wwid(path.wwid);
wwid.erase(std::remove(wwid.begin(), wwid.end(), ':'), wwid.end());
char wwnBytes[8];
strncpy( wwnBytes, wwid.c_str() + 6, 8); // get chars for bytes 4 -7
std::string bytes = wwnBytes;
std::stringstream ss;
unsigned int secBytes;
ss << std::hex << bytes;
ss >> secBytes; 

Hexadecimal is a textual representation – {0x60, 0x1a, 0xd1, 0x42} is the same as {96, 26, 209, 66} . 十六进制是文本表示形式– {0x60, 0x1a, 0xd1, 0x42}{96, 26, 209, 66} {0x60, 0x1a, 0xd1, 0x42}相同。

Your string contains the (ASCII) representation of those characters ( '6' , '0' , et c) and you need to convert those to the numbers they represent. 您的字符串包含这些字符( '6''0'等)的(ASCII)表示形式,您需要将其转换为它们表示的数字。

For one character, something like 对于一个字符,类似

// Assumes that c is an ASCII-encoded hexadecimal digit.
// TODO: Add input validation.
unsigned int from_hex(char c)
{
    return (c <= '0' && c <= '9') 
          ? c - '0'
          : 10 + std::toupper(c) - 'A';
}

and to convert two digits to a byte value, you could use 并将两位数字转换为字节值,您可以使用

unsigned int from_hex(char hi_bits, char lo_bits)
{
    return from_hex(hi_bits) * 16 + from_hex(lo_bits);
}

then from_hex('6', 'f') will produce one hundred and eleven, which is written 6f in hexadecimal. 那么from_hex('6', 'f')将产生from_hex('6', 'f') ,以十六进制形式写为6f。

I was able to do this using std::stringstream: 我能够使用std :: stringstream做到这一点:

std::string wwid(path.wwid);
wwid.erase(std::remove(wwid.begin(), wwid.end(), ':'), wwid.end());
char wwnBytes[8];
strncpy( wwnBytes, wwid.c_str() + 6, 8); // get chars for bytes needed
std::string bytes = wwnBytes;
std::stringstream ss;
unsigned int secBytes;
ss << std::hex << bytes;
ss >> secBytes; // bytes 4-7 in hex

Resulting unsigned int yields the expected hex values 结果unsigned int产生预期的十六进制值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM