简体   繁体   English

将 unsigned char * 转换为 char * 字符串 C

[英]Converting unsigned char * to char * string C

I have code to convert an int into a unsigned char * as such:我有将 int 转换为 unsigned char * 的代码,如下所示:

int i = //a number <= 15;
unsigned char * byte = (unsigned char *) &i;

I do this because I need the byte value of of that int.我这样做是因为我需要那个 int 的字节值。 Later I want to attach this byte value to a char * , so I need to convert it to a string.后来我想将此字节值附加到char * ,因此我需要将其转换为字符串。

Ex. if: int i = 15;
then: unsigned char * byte = F;

However I need to convert this to get:但是我需要将其转换为:

char * string = "F";

I have tried:我试过了:

char * string = (char *) &byte;
//and
char * string = (char *) byte;
//or just straight up treating it like a char * by going:
printf("%s", byte);

and several other combinations, but they've all resulted in seg faults.和其他几种组合,但它们都导致了段错误。 Does anyone know how I can do this?有谁知道我怎么能做到这一点? Or even if there's an easier way of converting an int to the hexidecimal representation in a char?或者即使有更简单的方法将 int 转换为 char 中的十六进制表示?

I'm very new to C and appreciate any answers thank you.我对 C 很陌生,感谢您的回答。

I actually want to append the converted char to an existing char *实际上想将转换后的字符附加到现有char *

  1. Insure the destination is big enough.确保目的地足够大。

     char existing[100]; // existing is populated somehow, now to add the `unsigned char` size_t len = strlen(existing); // 2 is the max size of a 8-bit value printed in hex. Adjust as needed. if (len + 2 >= sizeof existing) Error_BufferTooSmall();
  2. Write to the end of the existing char * .写入现有char *的末尾。

     // Only use the least significant `char` portion with `hh` sprintf(existing + len , "%hhX", (unsigned) i);

hh Specifies that a following d , i , o , u , x , or X conversion specifier applies to a signed char or unsigned char argument (the argument will have been promoted according to the integer promotions, but its value shall be converted to signed char or unsigned char before printing); hh指定后面的diouxX转换说明符适用于有符号字符或无符号字符参数(该参数将根据整数提升进行提升,但其值应转换为有signed char或打印前unsigned char ); ... C11 §7.21.6.1 7 ... C11 §7.21.6.1 7

int i = //a number <= 15;
unsigned char * byte = (unsigned char *) &i;

Is plain wrong.显然是错误的。

What you are doing is taking the address of the integer variable i and putting into a pointer to an unsigned char .您正在做的是获取整数变量i的地址并将其放入指向unsigned char的指针。

From what I read I think you want to convert an integer to a character (or string).根据我的阅读,我认为您想将整数转换为字符(或字符串)。

You can do this with itoa or even easier with sprintf .您可以使用itoa或使用sprintf更轻松地完成此操作。

For example:例如:

#include <stdio.h> // printf, snprintf

int main(void) {
    int i = 255;
    char theIntegerAsString[5] = {0};

    if (snprintf(theIntegerAsString, sizeof(theIntegerAsString), "%X", i) > 0) {
        printf("The number %d in Hexadecimal is: %s\n", i, theIntegerAsString);
    }
}

You might wonder why I used snprintf instead of sprintf .您可能想知道为什么我使用snprintf而不是sprintf

This is because sprintf does not always check the buffer size, whereas snprintf always does.这是因为sprintf并不总是检查缓冲区大小,而snprintf总是这样做。 See buffer overflow .请参阅缓冲区溢出

Please note that %X is specific to the type unsigned int for larger or smaller types you need another specifier.请注意, %X特定于unsigned int类型,对于更大或更小的类型,您需要另一个说明符。 I strongly recommend using <stdint.h> and <inttypes.h> like this:我强烈建议像这样使用<stdint.h><inttypes.h>

#include <stdio.h> // printf, snprintf
#include <stdint.h> // intX_t
#include <inttypes.h> // PRIx..

int main(void) {
    int32_t i = 255; // signed 32 bit integer
    char theIntegerAsString[5] = {0};

    if (snprintf(theIntegerAsString, sizeof(theIntegerAsString), "%" PRIX32, i) > 0) {
        printf("The number %" PRId32 " in Hexadecimal is: %s\n", i, theIntegerAsString);
    }
}

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

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