长十六进制值的向量 在C++中,我可以用初始化向量 std::vector<uint8_t> data = {0x01, 0x02, 0x03}; std::vector data={0x01、0x02、0x03};

长十六进制值的向量 在C++中,我可以用初始化向量 std::vector<uint8_t> data = {0x01, 0x02, 0x03}; std::vector data={0x01、0x02、0x03};,c++,hex,std,C++,Hex,Std,为方便起见(我有自然输出为十六进制转储的python字节字符串),我想初始化为以下形式的非分隔十六进制值: std::vector<uint8_t> data = 0x229597354972973aabbe7; std::vector data=0x229597354973aabbe7; 是否有一个变体C++是有效的?< /P> < P>结合EVG、JHbonarius和1201TealCalp:的注释 答案是,除了将一个长的十六进制值分组到一个向量中之外,没有直接的方法,但是

为方便起见(我有自然输出为十六进制转储的python字节字符串),我想初始化为以下形式的非分隔十六进制值:

std::vector<uint8_t> data = 0x229597354972973aabbe7;
std::vector data=0x229597354973aabbe7;

是否有一个变体C++是有效的?< /P> < P>结合EVG、JHbonarius和1201TealCalp:

的注释 答案是,除了将一个长的十六进制值分组到一个向量中之外,没有直接的方法,但是,使用提供了一种巧妙的符号改进


首先,在代码中的任何位置使用RHS
0x22959734972973aabbe7
都将失败,因为假定不固定文字为int类型,并且无法包含在寄存器中。在MSVC中,导致E0023“整数常量太大”。使用后缀表示法可以限制较小的十六进制序列或探索较大的数据类型,但这会破坏对简单性的任何渴望

手动转换是必要的,但用户定义的文字可能会提供稍微优雅的表示法。例如,我们可以使用

std::vector<uint8_t> val1 = 0x229597354972973aabbe7_hexvec;
std::vector<uint8_t> val2 = "229597354972973aabbe7"_hexvec;
std::vector val1=0x22959734972973aabbe7_hexvec;
std::vector val2=“22959734972973aabbe7”\u hexvec;
使用以下代码:

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>


// Quick Utlity function to view results:
std::ostream & operator << (std::ostream & os, std::vector<uint8_t> & v)
{
    for (const auto & t : v)
        os << std::hex << (int)t << " ";

    return os;
}

std::vector<uint8_t> convertHexToVec(const char * str, size_t len)
{
    // conversion takes strings of form "FFAA54" or "0x11234" or "0X000" and converts to a vector of bytes.

    // Get the first two characters and skip them if the string starts with 0x or 0X for hex specification:
    std::string start(str, 2);
    int offset = (start == "0x" || start == "0X") ? 2 : 0;

    // Round up the number of groupings to allow for ff_hexvec  fff_hexvec and remove the offset to properly count 0xfff_hexvec
    std::vector<uint8_t> result((len + 1 - offset) / 2);

    size_t ind = result.size() - 1;

    // Loop from right to left in in pairs of two but watch out for a lone character on the left without a pair because 0xfff_hexvec is valid:
    for (const char* it = str + len - 1; it >= str + offset; it -= 2) {
        int  val = (str + offset) > (it - 1); // check if taking 2 values will run off the start and use this value to reduce by 1 if we will
        std::string s(std::max(it - 1, str + offset), 2 - val);
        result[ind--] = (uint8_t)stol(s, nullptr, 16);
    }
        
    return result;
}

std::vector<uint8_t> operator"" _hexvec(const char*str, std::size_t len)
{
    // Handles the conversion form "0xFFAABB"_hexvec or "12441AA"_hexvec
    return convertHexToVec(str, len);
}

std::vector<uint8_t> operator"" _hexvec(const char*str)
{
    // Handles the form 0xFFaaBB_hexvec and 0Xf_hexvec
    size_t len = strlen(str);
    return convertHexToVec(str, len);   
}

int main()
{
    std::vector<uint8_t> v;

    std::vector<uint8_t> val1 = 0x229597354972973aabbe7_hexvec;
    std::vector<uint8_t> val2 = "229597354972973aabbe7"_hexvec;

    std::cout << val1 << "\n";
    std::cout << val2 << "\n";

    return 0;
}
#包括
#包括
#包括
#包括
//查看结果的快速功能:

std::ostream&operator结合Evg、JHbonarius和1201项目的意见:

答案是,除了将一个长的十六进制值分组到一个向量中之外,没有直接的方法,但是,使用提供了一种巧妙的符号改进


首先,在代码中的任何位置使用RHS
0x22959734972973aabbe7
都将失败,因为假定不固定文字为int类型,并且无法包含在寄存器中。在MSVC中,导致E0023“整数常量太大”。使用后缀表示法可以限制较小的十六进制序列或探索较大的数据类型,但这会破坏对简单性的任何渴望

手动转换是必要的,但用户定义的文字可能会提供稍微优雅的表示法。例如,我们可以使用

std::vector<uint8_t> val1 = 0x229597354972973aabbe7_hexvec;
std::vector<uint8_t> val2 = "229597354972973aabbe7"_hexvec;
std::vector val1=0x22959734972973aabbe7_hexvec;
std::vector val2=“22959734972973aabbe7”\u hexvec;
使用以下代码:

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>


// Quick Utlity function to view results:
std::ostream & operator << (std::ostream & os, std::vector<uint8_t> & v)
{
    for (const auto & t : v)
        os << std::hex << (int)t << " ";

    return os;
}

std::vector<uint8_t> convertHexToVec(const char * str, size_t len)
{
    // conversion takes strings of form "FFAA54" or "0x11234" or "0X000" and converts to a vector of bytes.

    // Get the first two characters and skip them if the string starts with 0x or 0X for hex specification:
    std::string start(str, 2);
    int offset = (start == "0x" || start == "0X") ? 2 : 0;

    // Round up the number of groupings to allow for ff_hexvec  fff_hexvec and remove the offset to properly count 0xfff_hexvec
    std::vector<uint8_t> result((len + 1 - offset) / 2);

    size_t ind = result.size() - 1;

    // Loop from right to left in in pairs of two but watch out for a lone character on the left without a pair because 0xfff_hexvec is valid:
    for (const char* it = str + len - 1; it >= str + offset; it -= 2) {
        int  val = (str + offset) > (it - 1); // check if taking 2 values will run off the start and use this value to reduce by 1 if we will
        std::string s(std::max(it - 1, str + offset), 2 - val);
        result[ind--] = (uint8_t)stol(s, nullptr, 16);
    }
        
    return result;
}

std::vector<uint8_t> operator"" _hexvec(const char*str, std::size_t len)
{
    // Handles the conversion form "0xFFAABB"_hexvec or "12441AA"_hexvec
    return convertHexToVec(str, len);
}

std::vector<uint8_t> operator"" _hexvec(const char*str)
{
    // Handles the form 0xFFaaBB_hexvec and 0Xf_hexvec
    size_t len = strlen(str);
    return convertHexToVec(str, len);   
}

int main()
{
    std::vector<uint8_t> v;

    std::vector<uint8_t> val1 = 0x229597354972973aabbe7_hexvec;
    std::vector<uint8_t> val2 = "229597354972973aabbe7"_hexvec;

    std::cout << val1 << "\n";
    std::cout << val2 << "\n";

    return 0;
}
#包括
#包括
#包括
#包括
//查看结果的快速功能:
std::ostream和操作员

有没有一个变体,它是有效的C++?< /p> 我想不是


下面的代码是有效的C++,并且使用了一个更“传统的十六进制转换”过程。

  • 确认并删除前导的“0x”,同时确认所有字符都已删除 十六进制字符

  • modifyFor_SDFE()-“空格分隔格式提取”

此函数在两个字符字节描述符周围插入空格

请注意,此函数还将在修改后的字符串的前面和后面添加一个空格字符。这个新字符串用于创建和初始化std::stringstream(ss1)

  • 通过插入空格,正常的流“格式化提取”可以干净地工作
代码一个接一个地提取每个十六进制值,并将每个十六进制值压入向量,最后一个字节被压入时结束(stream.eof()。注意,向量会根据需要自动增长(不会发生溢出)

请注意,不需要“0x”前缀。。因为流模式设置为十六进制

请注意,溢出问题(上面表示为“0x22…be7可能会溢出”)只是一次只读取一个字节而被忽略了。在将来使用更大的十六进制字符串可能会更方便



在没有参数的情况下调用时

$ ./dumy889 

  33 05 08 46 50 83 08 4b bc cf 87 eb ba a3 79 27 95 43 79 59 22 ff 

行数不包括空行,也不包括仅为注释或大括号的行。您可以根据需要计算行数

有没有一个变体,它是有效的C++?< /p> 我想不是


下面的代码是有效的C++,并且使用了一个更“传统的十六进制转换”过程。

  • 确认并删除前导的“0x”,同时确认所有字符都已删除 十六进制字符

  • modifyFor_SDFE()-“空格分隔格式提取”

此函数在两个字符字节描述符周围插入空格

请注意,此函数还将在修改后的字符串前后添加一个空格字符。此新字符串用于创建和初始化std::stringstream(ss1)

  • 通过插入空格,正常的流“格式化提取”可以干净地工作
代码一个接一个地提取每个十六进制值,并将每个十六进制值推入向量,最后一个字节被推入时结束(stream.eof())。请注意,向量会根据需要自动增长(不会发生溢出)

请注意,不需要“0x”前缀。。因为流模式设置为十六进制

请注意,溢出问题(上面表示为“0x22…be7可能会溢出”)只是一次只读取一个字节而被忽略了。在将来使用更大的十六进制字符串可能会更方便



在没有参数的情况下调用时

$ ./dumy889 

  33 05 08 46 50 83 08 4b bc cf 87 eb ba a3 79 27 95 43 79 59 22 ff 


行计数不包括空行,也不包括仅为注释或大括号的行。您可以根据需要计数行。

0x229597354973aabbe7
可能溢出。简单回答:不。没有接受长十六进制值的
std::vector
构造函数。您必须自己编写解析器或查看对于为您提供此功能的BigInt库,您可能可以创建一个将十六进制文字转换为所需向量的函数。@1201程序很有趣,
auto v=0xaabb0305\u hexvec;
的代码使用
std::vector运算符“”\u hexvec(const char*str)
一个要转换的函数并不难(从十六进制符号到