Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++十六进制字符串到字节数组_C++_Hex - Fatal编程技术网

C++十六进制字符串到字节数组

C++十六进制字符串到字节数组,c++,hex,C++,Hex,首先,在过去的几天里,我在谷歌上搜索了这个问题,但我发现的一切都不起作用。我没有收到运行时错误,但当我以程序生成的十六进制字符串的形式键入相同的密钥进行加密时,解密失败,但在整个程序中使用生成的密钥可以正常工作。我正在尝试输入十六进制字符串格式:00:00:00。。。并将其转换为32字节数组。输入来自getpass。我以前在爪哇和C做过这件事,但是我对C++很陌生,一切似乎都复杂得多。任何帮助都将不胜感激:而且我正在linux平台上编程,所以我希望避免使用仅限Windows的函数 以下是我尝试过

首先,在过去的几天里,我在谷歌上搜索了这个问题,但我发现的一切都不起作用。我没有收到运行时错误,但当我以程序生成的十六进制字符串的形式键入相同的密钥进行加密时,解密失败,但在整个程序中使用生成的密钥可以正常工作。我正在尝试输入十六进制字符串格式:00:00:00。。。并将其转换为32字节数组。输入来自getpass。我以前在爪哇和C做过这件事,但是我对C++很陌生,一切似乎都复杂得多。任何帮助都将不胜感激:而且我正在linux平台上编程,所以我希望避免使用仅限Windows的函数

以下是我尝试过的一个例子:

char *pass = getpass("Key: ");

std::stringstream converter;
std::istringstream ss( pass );
std::vector<byte> bytes;

std::string word;
while( ss >> word )
{
    byte temp;
    converter << std::hex << word;
    converter >> temp;
    bytes.push_back( temp );
}
byte* keyBytes = &bytes[0];

如果您的输入格式为:AA:BB:CC, 你可以这样写:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cstdint>

struct hex_to_byte
{
    static uint8_t low(const char& value)
    {
        if(value <= '9' && '0' <= value)
        {
            return static_cast<uint8_t>(value - '0');
        }
        else // ('A' <= value && value <= 'F')
        {
            return static_cast<uint8_t>(10 + (value - 'A'));
        }
    }

    static uint8_t high(const char& value)
    {
        return (low(value) << 4);
    }
};

template <typename InputIterator>
std::string from_hex(InputIterator first, InputIterator last)
{
    std::ostringstream oss;
    while(first != last)
    {
        char highValue = *first++;
        if(highValue == ':')
            continue;

        char lowValue = *first++;

        char ch = (hex_to_byte::high(highValue) | hex_to_byte::low(lowValue));
        oss << ch;
    }

    return oss.str();
}

int main()
{
    std::string pass = "AB:DC:EF";
    std::string bin_str = from_hex(std::begin(pass), std::end(pass));
    std::vector<std::uint8_t> v(std::begin(bin_str), std::end(bin_str)); // bytes: [171, 220, 239]
    return 0;
}
这个怎么样

把它当作一个单词来读,然后再进行操作? 您可以在convert中执行任何大小检查格式检查

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

char convert(char c)
{
    using namespace std;
    // do whatever decryption stuff you want here
    return c;
}

void test()
{
    using namespace std;

    string word;
    cin >> word;

    vector<char> password;

    for (int i = 0; i < word.length(); i++)
    {
        password.push_back(convert(word[i]));
    }

    for (int i = 0; i < password.size(); i++)
    {
        cout << password[i];
    }

    cout << "";
}

int main()
{
    using namespace std;
    char wait = ' ';

    test();

    cin >> wait;
}

这里不使用cin有什么具体原因吗?

我尝试不使用cin,只是因为它是一个正在输入的加密密钥,可以保留终端输入的日志。谢谢,这很有用,但我无法以字节[]的形式访问输出,因为解密方法输入参数非常具体。对不起,我不确定是什么问题。您能更具体一点吗?我需要将字节传递给的方法看起来像setkeycont byte*key,您可以这样调用SetKey:SetKey&v[0],v.size;或者直接使用bin_str:SetKey&bin_str[0],bin_str.size;