C语言 如何将无符号字符数组转换为 C 中的十六进制字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5661101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:23:34  来源:igfitidea点击:

How to convert an Unsigned Character array into a hexadecimal string in C

cstringtypes

提问by Maverickgugu

Is it possible to represent an unsigned character array as a string?

是否可以将无符号字符数组表示为字符串?

When I searched for it, I found out that only memset() was able to do this (But character by character). Assuming that is not the correct way, is there a way to do the conversion?

当我搜索它时,我发现只有 memset() 能够做到这一点(但逐个字符)。假设这不是正确的方法,有没有办法进行转换?

Context: I am trying to store the output of a cryptographic hash function which happens to be an array of unsigned characters.
eg:

上下文:我试图存储一个加密哈希函数的输出,它恰好是一个无符号字符数组。
例如:

unsigned char data[N]; ...
for(i=0;i<N;i++) printf("%x",data[i]);

My goal is to represent the data as a String (%s) rather than access it by each element. Since I need the output of the hash as a String for further processing.

我的目标是将数据表示为字符串 (%s),而不是通过每个元素访问它。因为我需要散列的输出作为字符串进行进一步处理。

Thanks!

谢谢!

回答by forsvarir

So, based on your update, are you talking about trying to convert a unsigned char buffer into a hexadecimal interpretation, something like this:

因此,根据您的更新,您是否在谈论尝试将 unsigned char 缓冲区转换为十六进制解释,如下所示:

#define bufferSize 10
int main() {
  unsigned char buffer[bufferSize]={1,2,3,4,5,6,7,8,9,10};
  char converted[bufferSize*2 + 1];
  int i;

  for(i=0;i<bufferSize;i++) {
    sprintf(&converted[i*2], "%02X", buffer[i]);

    /* equivalent using snprintf, notice len field keeps reducing
       with each pass, to prevent overruns

    snprintf(&converted[i*2], sizeof(converted)-(i*2),"%02X", buffer[i]);
    */

  }
  printf("%s\n", converted);

  return 0;
}

Which outputs:

哪些输出:

0102030405060708090A

回答by unwind

In C, a string is an array of char, terminated with a character whose value is 0.

在 C 中,字符串是 的数组char,以一个值为 0 的字符结尾。

Whether or not charis a signed or unsigned type is not specified by the language, you have to be explicit and use unsigned charor signed charif you really care.

无论char是有符号还是无符号类型都不是由语言指定的,您必须明确并使用unsigned char或者signed char您是否真的在意。

It's not clear what you mean by "representing" an unsigned character array as string. It's easy enough to cast away the sign, if you want to do something like:

不清楚将无符号字符数组“表示”为字符串是什么意思。如果您想执行以下操作,则很容易丢弃该标志:

const unsigned char abc[] = { 65, 66,67, 0 }; // ASCII values for 'A', 'B', 'C'.

printf("The English alphabet starts out with '%s'\n", (const char *) abc);

This will work, to printf()there isn't much difference, it will see a pointer to an array of characters and interpret them as a string.

这会起作用,printf()因为没有太大区别,它会看到一个指向字符数组的指针并将它们解释为字符串。

Of course, if you're on a system that doesn't use ASCII, there might creep in cases where doing this won't work. Again, your question isn't very clear.

当然,如果您使用的是不使用 ASCII 的系统,则可能会出现无法执行此操作的情况。同样,你的问题不是很清楚。

回答by Chris

Well a string in C is nothing else than a few chars one after another. If they are unsigned or signed is not much of a problem, you can easily cast them.

好吧,C 中的字符串只不过是一个接一个的几个字符。如果它们未签名或签名不是什么大问题,您可以轻松地转换它们。

So to get a string out of a unsigned char array all you have to do is to make sure that the last byte is a terminating byte '\0' and then cast this array to char * (or copy it into a array of char)

因此,要从无符号字符数组中获取字符串,您所要做的就是确保最后一个字节是终止字节 '\0' 然后将此数组转换为 char * (或将其复制到一个字符数组中)

回答by Roman Slyepko

I successfully use this to convert unsigned char array to std:string

我成功地使用它将无符号字符数组转换为 std:string

unsigned char array[128];
std::stringstream buffer;
for (int i = 0; i < 128; i++)
    {
        buffer << std::hex << std::setfill('0');
        buffer << std::setw(2)  << static_cast<unsigned>(array[i]);
    }
std::string hexString = buffer.str();

回答by Batman

An example as you've asked:

你问的一个例子:

unsigned char arr [SIZE];

无符号字符 arr [SIZE];