长十六进制值的向量 [英] Vector from long hex value

查看:282
本文介绍了长十六进制值的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,我可以使用初始化向量

In C++ I can initialize a vector using

std::vector<uint8_t> data = {0x01, 0x02, 0x03};

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

For convenience (I have python byte strings that naturally output in a dump of hex), I would like to initialize for a non-delimited hex value of the form:

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

是否存在有效的c ++变体?

Is there a variant of this that is valid c++?

推荐答案

结合Evg,JHbonarius和1201ProgramAlarm的评论:

Combining comments from Evg, JHbonarius and 1201ProgramAlarm:

答案是,没有直接方法可以将长十六进制值分组为向量,但是,使用用户定义的文字提供了一种巧妙的符号改进.

The answer is that there is no direct way to group but a long hex value into a vector, however, using user defined literals provides a clever notation improvement.

首先,在代码中的任何地方使用RHS 0x229597354972973aabbe7都会失败,因为假定未加后缀的文字为int类型,并且将无法包含在寄存器中.在MSVC中,导致E0023整数常数太大".带有后缀的表示法可能会限制为较小的十六进制序列或浏览大型数据类型,但这会破坏任何对简单性的渴望.

First, using RHS 0x229597354972973aabbe7 anywhere in the code will fail because because unsuffixed literals are assumed to be of type int and will fail to be contained in the register. In MSVC, result in E0023 "integer constant is too large". Limiting to smaller hex sequences or exploring large data types may be possible with suffixed notation, but this would ruin any desire for simplicity.

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

Manual conversion is necessary, but user defined literals may provide a slightly more elegant notation. For example, we can enable conversion of a hex sequence to a vector with

std::vector<uint8_t> val1 = 0x229597354972973aabbe7_hexvec;
std::vector<uint8_t> val2 = "229597354972973aabbe7"_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;
}

编码人员必须决定这是否优于实现和使用更传统的convertHexToVec("0x41243124FF")函数.

The coder must decide whether this is superior to implementing and using a more traditional convertHexToVec("0x41243124FF") function.

这篇关于长十六进制值的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆