繁体   English   中英

将字符数组转换为字节数组

[英]Convert char array to byte array

我有一个这样声明的字符串。

CHAR bkp[40] = "dc74699a8381da395f10b"; <- this value comes from querying a registry value

在 memory (使用 VS memory 窗口)我看到..

0x00000071432FF918 64 63 37 34 36 39 39 61 38 33 38 31 64 61 33 39 35 66 31 30 62 00 .. .. .. ..

我正在尝试将字符串转换为 memory 以便当我检查 memory 地址时我看到..

0x00000071432FF918 dc 74 69 9a 83 81 da 39 5f 10 0b .. .. .. ..

我的项目在 C++ 但 function 要求它返回到char * 因此,如果需要将 char 数组转换为 C++ 字符串,它可以。

只需遍历字符串,对于每个 2 字符对,您都可以进行一些非常简单的计算和位移来提取字节值。 例如:

BYTE decodeHex(char c)
{
    if (c >= '0' && c <= '9')
        return c - '0';
    else if (c >= 'A' && c <= 'F')
        return (c - 'A') + 10;
    else if (c >= 'a' && c <= 'f')
        return (c - 'a') + 10;
    else
        // illegal! throw something...
}

CHAR bkp[] = "dc74699a8381da395f100b";
int slen = strlen(bkp);

BYTE *bytes = new BYTE[slen / 2];
int blen = 0;

for(int i = 0; i < slen; i += 2)
{
    bytes[blen++] = (decodeHex(bkp[i]) << 4) | decodeHex(bkp[i+1]);
}

// use bytes up to blen as needed...

delete[] bytes;

数组是一个字符串,因此您必须将字符转换为十六进制。 让我们使用老式的方法:

const unsigned int length = sizeof(bkp);
const std::string hex_digits = "0123456789abcdef";
std::vector<uint8_t> destination;
for (unsigned int index = 0U; index < length; index += 2U)
{
    uint8_t byte_value = 0;
    std::string::size_type  position = hex_digits.find(bkp[index]);
    if (position == std::string::npos)
    {
       std::cerr << "invalid hex value at position " << index << "\n";
       break;
    }
    byte_value = position;
    ++index;
    position = hex_digits.find(bkp[index]);
    if (position == std::string::npos)
    {
       std::cerr << "invalid hex value at position " << index << "\n";
       break;
    }
    byte_value = (byte_value * 256) + position;
    destination.push_back(byte_value);
}

注意:上述代码使用 C++ 功能,因为原始帖子被标记为 C++。

您需要将字符数组转换为二进制文件。 您的输入数组是一个十六进制字符串,所以这是相当简单的。

unsigned char toBinary(char c)
{
    if (c >= '0' && c <= '9')
        return c - '0';

    return (c - 'a') + 10;
}

CHAR bkp[40] = "dc74699a8381da395f10b"
unsigned char b[20];

int bi = 0;
for(int i = 0; i < 40; i += 2)
{
    char c = bkp[i];
    unsigned char v = toBinary(bkp[i]) << 4;
    v += toBinary(bkp[i+1])

    b[bi++] = v;
}

只是为了好玩,您可以使用非条件操作执行转换。

一般来说:

  • 'A' = 64, 'a' = 96,两者都设置了第 6 位(十进制值 64)
  • '0' = 48,因此没有设置第 6 位。

您可以取输入,取低 4 位给我们 0->9、A->F 或 a->f,然后取第 6 位并将其用作乘数,以在需要时加上 +10。

#include <conio.h>
#include <stdio.h>
#include <string.h>

void HexStrToRaw(char* in, unsigned char* out)
{
    for (int loop = 0, o_loop = 0; loop < strlen(in); loop += 2, o_loop++)
    {
        out[o_loop] = (((in[loop] & 15) + ((in[loop] >> 6) * 9)) << 4) | ((in[loop + 1] & 15) + ((in[loop + 1] >> 6) * 9));
    }
}

int main(int argc, char** argv)
{
    char          in[40] = "dc74699a8381da395f10b";
    unsigned char out[20];

    HexStrToRaw(in, out);
    for (int loop = 0; loop < sizeof(out); loop++)
    {
        printf("%d -> 0x%02x\n", loop, out[loop]);
    }
    return 0;
}

output 变为:

0 -> 0xdc
1 -> 0x74
2 -> 0x69
3 -> 0x9a
4 -> 0x83
5 -> 0x81
6 -> 0xda
7 -> 0x39
8 -> 0x5f
9 -> 0x10
10 -> 0xb0
11 -> 0xcc
12 -> 0xcc
13 -> 0xcc
14 -> 0xcc
15 -> 0xcc
16 -> 0xcc
17 -> 0xcc
18 -> 0xcc
19 -> 0xcc

暂无
暂无

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

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