简体   繁体   English

C / C ++整数到十六进制到char数组到char

[英]C/C++ integer to hex to char array to char

Well I have been trying to convert this integer into hex and have successfully done so but I need to use this hex for setting something. 好吧,我一直在尝试将此整数转换为十六进制,并且成功完成了此操作,但是我需要使用此十六进制来进行设置。 Now for this I need to use a char not a char array. 现在,为此,我需要使用一个char而不是char数组。 Nothing else has worked without manually setting it. 如果没有手动设置,没有其他任何工作。 Maybe the problem lies in the issue that I use sprintf for the conversion to hex but either way I am sure there is a way to complete this task. 也许问题出在我使用sprintf转换为十六进制的问题上,但是无论哪种方式,我都确信有一种方法可以完成此任务。 Now What I need to change is have the output be char z but I haven't found a way to get this to work. 现在,我需要更改的是将输出设置为char z,但是我还没有找到一种方法来使其正常工作。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks 谢谢

EDIT: now thi code may not make sense directly because it is incomplete and I saw no purpose inputting unrelated code. 编辑:现在,该代码可能不直接有意义,因为它不完整,我看不到输入无关代码的目的。 int x will never be over 100 and the whole point is to convert this into a hex and write it to the memory of a setting I have. int x永远不会超过100,关键是将其转换为十六进制并将其写入我所拥有的设置的内存中。 So I have been trying to figure out how to convert the integer into hex into a char. 因此,我一直在尝试找出如何将整数转换为十六进制的char。 nonstring as someone pointed out even though sprintf converts it to a string stored in a char as I just noticed. 有人指出的非字符串,即使sprintf将其转换为存储在char中的字符串,正如我刚刚注意到的那样。 But I need to take the int convert to hex and assign that to a char variable forbuse later on. 但是我需要将int转换为十六进制,然后将其分配给char变量以供稍后使用。 And that is where I am stuck. 那就是我被困住的地方。 I do not know the best way to go about completely all that in a format and way without going into a string and other things. 我不知道以某种格式和某种方式完成所有这些工作的最佳方法,而不涉及字符串和其他事物。

VOID WriteSetting(int x)
{
    char output[8];
    sprintf(output, "0x%X", x);
    char z = 0x46
    unsigned char y = z
}

Working Code: 工作代码:

VOID WriteSetting(int x)
{
    unsigned char y = (unsigned char)x;
    Settingdb.Subset.Set = y;
}

你有没有尝试过

printf("0x%X", z);

While the C++ standards do not explicitly require it, the size of a character is sometimes a byte, and an integer 4 bytes. 尽管C ++标准没有明确要求,但是字符的大小有时是一个字节,一个整数是4个字节。 I presume that this is why you are using an array of 4 characters. 我认为这就是为什么要使用4个字符的数组。

So when you have an integer type and try to cram it into a single character, you'll lose precision. 因此,当您使用整数类型并尝试将其填充为单个字符时,您将失去精度。

PS A more precise explanation of sizes of data types is here: What does the C++ standard state the size of int, long type to be? PS这里是对数据类型大小的更精确的解释: C ++标准说明int,long类型的大小是什么?

If what you want is for: 如果您想要的是:

WriteSetting(70);

or 要么

WriteSetting(0x46);

to do the same thing as 做与

char z = 0x46;
unsigned char y = z;

then all you need to do is: 那么您要做的就是:

void WriteSetting(int x)
{
    unsigned char y = x;
}

Integers don't have any inherent base - they're just numbers. 整数没有任何固有的基础-它们只是数字。 There's no difference at all between unsigned char y = 0x46; unsigned char y = 0x46;之间完全没有区别unsigned char y = 0x46; and unsigned char y = 70; unsigned char y = 70; .

void WriteSetting(unsigned char *y, int x){
    if(x > 100){
        fprintf(stderr, "x value(%d) is invalid at %s\n", x, __func__);
        return ;
    }
    *y = x;//There is no need for conversion probably
}

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

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