gpt4 book ai didi

c - 将 char 字符串分配给 uint32_t(仅限 C,不适用于 C++)

转载 作者:行者123 更新时间:2023-11-30 21:26:49 25 4
gpt4 key购买 nike

我想知道如何将 uint32_t 变量的值赋给“输入”变量。

请参阅下面的代码。

char *HEXlastName = "0x00D1";
uint32_t *in, input[];

char *utf8;
char word[50] = { "" };
for(in = input; *in; ++in) {
utf8 = to_utf8(*in);
strcat(word, utf8);
(void)userlog("%s:word--> %s",prc, word);
}

char *to_utf8(const uint32_t cp)
{
static char ret[5];
const int bytes = codepoint_len(cp);

int shift = utf[0]->bits_stored * (bytes - 1);
ret[0] = (cp >> shift & utf[bytes]->mask) | utf[bytes]->lead;
shift -= utf[0]->bits_stored;
int i;
for(i=1; i < bytes; ++i) {
ret[i] = (cp >> shift & utf[0]->mask) | utf[0]->lead;
shift -= utf[0]->bits_stored;
}
ret[bytes] = '\0';
return ret;
}

我想将“HEXlastName”的值分配给var.: input(uint32_t)。我该怎么做?

char *HEXlastName = "0x00D1";
input = HEXlastName ; //--> This sentente is wrong.
//How I could move the content of HEXlastName to input

也许这是一个愚蠢的问题,但我是 C 语言新手。

问候

最佳答案

感谢pmg先生的回复,给了我答案。

我有一个名为“input”(uint32_t)的变量,和另一个名为“HEXlocalName”的变量,其中包含带有unicodes (0x0000010x000002)的数组字符串。

此例程将 var“hexlocalname”的内容移动到 var“input”(unit32_t)

#include <limits.h>
#include <stdint.h>
#include <stdio.h>

int main(void) {
char v[UCHAR_MAX + 1] = {0};
v['0'] = 0;
v['1'] = 1;
v['2'] = 2;
v['3'] = 3;
v['4'] = 4;
v['5'] = 5;
v['6'] = 6;
v['7'] = 7;
v['8'] = 8;
v['9'] = 9;
v['a'] = v['A'] = 10;
v['b'] = v['B'] = 11;
v['c'] = v['C'] = 12;
v['d'] = v['D'] = 13;
v['e'] = v['E'] = 14;
v['f'] = v['F'] = 15;
char *HEXlastName = "0x00D10x00C10x00450x00C10x00D1";
char *p = HEXlastName;
uint32_t input[33];
size_t n = 0;
while (*p) {
input[n++] = (v[(unsigned)p[2]]<<12) + (v[(unsigned)p[3]]<<8) + (v[(unsigned)p[4]]<<4) + (v[(unsigned)p[5]]);
p += 6;
}
for (int k = 0; k < n; k++) {
printf("input[%d] is 0x%04X\n", k, input[k]);
}
return 0;
}

关于c - 将 char 字符串分配给 uint32_t(仅限 C,不适用于 C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58836958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com