C语言 将 C 字符串转换为二进制表示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5666900/
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:24:12  来源:igfitidea点击:

Converting C string to binary representation

cstringbytearray

提问by Maverickgugu

In ANSI C, how do we convert a string in to an array of binary bytes? All the googling and searching gives me answers for C++ and others and not C.

在 ANSI C 中,我们如何将字符串转换为二进制字节数组?所有的谷歌搜索和搜索都为我提供了 C++ 和其他而不是 C 的答案。

One idea I had was to convert the string into ASCII and then convert each ASCII value into its binary. (Duh!) I know it is the dumbest of ideas but I am not sure of any other option.

我的一个想法是将字符串转换为 ASCII,然后将每个 ASCII 值转换为其二进制值。(废话!)我知道这是最愚蠢的想法,但我不确定还有其他选择。

I've heard abt the encoding function in Java. I am not sure if that suits the same purpose and can be adopted to C.

我听说过 Java 中的编码功能。我不确定这是否适合相同的目的并且可以采用 C.

string = "Hello"
bytearr[] = 10100101... some byte array..

It would be great if someone can throw some light on this.

如果有人能对此有所了解,那就太好了。

Thanks!

谢谢!

回答by Athabaska Dick

Or did you mean how to convert C string to binary representation?

或者您的意思是如何将 C 字符串转换为二进制表示?

Here is one solution which can convert strings to binary representation. It can be easily altered to save the binary strings into array of strings.

这是一种可以将字符串转换为二进制表示的解决方案。可以很容易地将二进制字符串保存到字符串数组中。

#include <stdio.h>

int main(int argc, char *argv[])
{
    if(argv[1] == NULL) return 0; /* no input string */

    char *ptr = argv[1];
    int i;

    for(; *ptr != 0; ++ptr)
    {
        printf("%c => ", *ptr);

        /* perform bitwise AND for every bit of the character */
        for(i = 7; i >= 0; --i) 
            (*ptr & 1 << i) ? putchar('1') : putchar('0');

        putchar('\n');
    }

    return 0;
}

Example input & output:

示例输入和输出:

./ascii2bin hello

./ascii2bin hello

h => 01101000
e => 01100101
l => 01101100
l => 01101100
o => 01101111

回答by Jurlie

There is no any strings in C. Any string IS an array of bytes.

C 中没有任何字符串。任何字符串都是字节数组。

回答by John Bode

A string isan array of bytes.

字符串一个字节数组。

If you want to displaythe ASCII value of each character in hex form, you would simply do something like:

如果要以十六进制形式显示每个字符的 ASCII 值,只需执行以下操作:

while (*str != 0)
  printf("%02x ", (unsigned char) *str++);

回答by John Bode

On most of the systems I have worked on, the width of charis 1-byte and so a char[]or char*isa byte array.

在我使用过的大多数系统上, of 的宽度char是 1 字节,因此 a char[]orchar*一个字节数组。

In most other languages such as Java, the string datatype takes care of looking after, to a certain degree, concepts like encoding, by using an encoding like say UTF-8. In C this is not the case. If I were to read a UTF-8 string whose contents included multi-byte values, my characters would be represented by twobuckets in the array (or potentially more).

在大多数其他语言(如 Java)中,字符串数据类型通过使用像 UTF-8 这样的编码在一定程度上负责照顾编码等概念。在 C 中,情况并非如此。如果我要读取内容包含多字节值的 UTF-8 字符串,我的字符将由数组中的两个桶(或可能更多)表示。

To look at it from another point of view, consider that all types in C have a fixed width for your system (although they may vary between implementations).

从另一个角度来看,请考虑 C 中的所有类型对于您的系统都有固定的宽度(尽管它们可能因实现而异)。

So that string you're operating on isa byte array.

所以你正在操作的字符串一个字节数组。

Next question I guess then is how do you display those bytes? That's pretty straightforward:

我猜下一个问题是如何显示这些字节?这非常简单:

char* x = ???; /* some string */
unsigned int xlen = strlen(x);
int i = 0;

for ( i = 0; i < xlen; i++ )
{
    printf("%x", x[i]);
}

I can't think of a reason why you'd want to convert that output to binary, but it could be done if you were so minded.

我想不出您想将该输出转换为二进制的原因,但如果您如此介意,这是可以做到的。

回答by zwol

If you just want to iterate (or randomly access) individual bytes' numeric values, you don't have to do any conversion at all, because C strings are arrays already:

如果您只想迭代(或随机访问)单个字节的数值,则根本不必进行任何转换,因为 C 字符串已经是数组:

void dumpbytevals(const char *str)
{
    while (*str)
    {
        printf("%02x ", (unsigned char)*str);
        str++;
    }
    putchar('\n');
}

If you're not careful with this kind of code, though, you run the risk of being in a world of hurt when you need to support non-ASCII characters.

但是,如果您对这种代码不小心,那么当您需要支持非 ASCII 字符时,您将面临陷入困境的风险。

回答by Nathan Moinvaziri

Since printf is slow when converting a huge binary array. Here is another approach that does not use printf:

由于 printf 在转换巨大的二进制数组时很慢。这是另一种不使用 printf 的方法:

#define BASE16VAL               ("x0x1x2x3x4x5x6x7x8x9|||||||xAxBxCxDxExF") 
#define BASE16_ENCODELO(b)      (BASE16SYM[((uint8)(b)) >> 4])
#define BASE16_ENCODEHI(b)      (BASE16SYM[((uint8)(b)) & 0xF]) 
#define BASE16_DECODELO(b)      (BASE16VAL[Char_Upper(b) - '0'] << 4)
#define BASE16_DECODEHI(b)      (BASE16VAL[Char_Upper(b) - '0']). 

To convert a hex string to a byte array you would do the following:

要将十六进制字符串转换为字节数组,您可以执行以下操作:

while (*Source != 0)   
    {   
    Target[0]  = BASE16_DECODELO(Souce[0]);   
    Target[0] |= BASE16_DECODEHI(Souce[1]);    

    Target += 1;   
    Source += 2;   
    } 

*Target = 0;

Source is a pointer to a char array that contains a hex string. Target is a pointer to a char array that will contain the byte array.

Source 是指向包含十六进制字符串的 char 数组的指针。目标是指向包含字节数组的字符数组的指针。

To convert a byte array to a hex string you would to the following:

要将字节数组转换为十六进制字符串,您需要执行以下操作:

while (*Source != 0)   
    {   
    Target[0] = BASE16_ENCODELO(*Source);   
    Target[1] = BASE16_ENCODEHI(*Source);    

    Target += 2;   
    Source += 1;   
    }

Target is a pointer to a char array that contains a hex string. Source is a pointer to a char array that will contain the byte array.

目标是指向包含十六进制字符串的字符数组的指针。Source 是一个指向包含字节数组的字符数组的指针。

Here are a few missing macros:

以下是一些缺失的宏:

#define Char_IsLower(C)  ((uint8)(C - 'a') < 26)
#define Char_IsUpper(C)  ((uint8)(C - 'A') < 26)
#define Char_Upper(C)    (Char_IsLower(C) ? (C + ('A' - 'a')) : C)
#define Char_Lower(C)    (Char_IsUpper(C) ? (C + ('a' - 'A')) : C)