简体   繁体   English

C++ 如何将字符串字符转换为精确的十六进制字节

[英]C++ how to convert string characters to exact hex bytes

To start off: I have an app that takes a byte array and loads assembly from it.首先:我有一个应用程序需要一个字节数组并从中加载程序集。

My idea, to prevent (easy)piracy, was to have an encrypted string on server, download it on client, decrypt it to get for example: std::string decrypted = "0x4D, 0x5A, 0x90, 0x0, 0x3, 0x0, 0x0, 0x0, 0x4";为了防止(容易)盗版,我的想法是在服务器上有一个加密的字符串,在客户端下载它,解密它以获得例如: std::string decrypted = "0x4D, 0x5A, 0x90, 0x0, 0x3, 0x0, 0x0, 0x0, 0x4";

Then to convert from string to binary(byte array) so it would be然后从字符串转换为二进制(字节数组),这样就可以了

uint8_t binary[] = { 0x4D, 0x5A, 0x90, 0x0, 0x3, 0x0, 0x0, 0x0, 0x4 };

And then continue as it was before, but after lots of googling I couldn't find much info on such direct conversion between regular string and byte array.然后像以前一样继续,但经过大量谷歌搜索后,我找不到太多关于常规字符串和字节数组之间直接转换的信息。 Thank you for any help!感谢您的任何帮助! -Sarah -莎拉

You could use std::stoi in a loop.您可以在循环中使用std::stoi

It gives you the ending position of the number, which you can then use to check if the string is at its end, or if it's a comma.它为您提供数字的结尾 position,然后您可以使用它来检查字符串是否在其末尾,或者它是否为逗号。 If it's a comma skip it.如果是逗号,请跳过它。 Then call std::stoi again using the position as the string to parse.然后使用 position 作为要解析的字符串再次调用std::stoi

It's not the most effective, but should work fine.它不是最有效的,但应该可以正常工作。

Use std::stoul to interpret a string as an unsigned integer. The unsigned integer can then be cast to a uint8_t type.使用std::stoul将字符串解释为无符号 integer。然后可以将无符号 integer 转换为uint8_t类型。

One method of parsing the entire string is by using a stringstream.解析整个字符串的一种方法是使用字符串流。

Code example:代码示例:

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

int main()
{
    // Input string and output vector
    std::string const decrypted{"0x4D, 0x5A, 0x90, 0x0, 0x3, 0x0, 0x0, 0x0, 0x4"};
    std::vector<std::uint8_t> bytes;

    // Parse the string and fill the output vector
    std::istringstream decryptedStringStream{decrypted};
    std::string decryptedElement;
    while (getline(decryptedStringStream, decryptedElement, ','))
    {
        auto const byte = static_cast<std::uint8_t>(std::stoul(decryptedElement, nullptr, 16));
        bytes.push_back(byte);
    }

    // Print the results (in base 10)
    for (auto const &e : bytes)                                                                             
        std::cout << static_cast<int>(e) << '\n';
}

First of all, you should get rid of ", ".首先,你应该摆脱“,”。 Then you can parse char by char, doing bitwise leftshift on every second char and saving as byte然后你可以逐个解析一个字符,对每个第二个字符进行按位左移并保存为字节

char firstchar = HexCharToByte('5');
char secondchar = HexCharToByte('D');
char result = firstchar | (secondchar << 4);
printf("%%hhu", result); //93

Where HexCharToByte is (upper chars only): HexCharToByte 在哪里(仅限上部字符):

char HexCharToByte(char ch) => ch > 57 ? (ch - 55) : (ch - 48);

This is fast enough method of parsing hex chars.这是解析十六进制字符的足够快的方法。

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

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