简体   繁体   English

C中的二进制到字符?

[英]binary to char in C?

I have an array我有一个数组

bin =

     0     1     1     0     0     0     0     1
     0     1     1     0     0     1     0     1
     0     1     1     1     0     0     1     0
     0     1     1     1     0     0     1     1
     0     1     1     0     0     1     1     1
     0     1     1     0     1     0     1     0
     0     1     1     0     1     0     0     0
     0     1     1     0     0     0     0     1
     0     1     1     1     0     1     0     0
     0     0     1     0     0     1     1     1
     0     1     0     0     1     0     1     0
     0     1     0     0     1     0     0     0

Every line in the array is a 8 bits binary letter数组中的每一行都是一个 8 位的二进制字母

bin is equivalent to this string aersgjhat'JH bin 相当于这个字符串aersgjhat'JH

This is very simple using matlab, but I couldn't find how to convert a table full of binary 8 bit words to a string in C.使用 matlab 这很简单,但我找不到如何将一个充满二进制 8 位字的表转换为 C 中的字符串。

You really need to read in each byte of the array - I'm assuming a '0' is no bit set and '1' is bit set ...您确实需要读取数组的每个字节 - 我假设 '0' 没有设置位,而 '1' 是位设置 ...

A loop that sets each bit would probably do the job - if I'm reading your array correctly ....设置每一位的循环可能会完成这项工作 - 如果我正确读取您的数组......

unsigned char bin[] = {....}   // your array full of data
unsigned char output[ 16 ]      // you will need to set the output size...
int nIndex = 0;
int nOutIndex = 0;
....

// the inner loop ...
output[ nOutIndex ] = 0;
for( nIndex = 0; nIndex < 8; nIndex++ )
{
    if ( bin[ nIndex ] == 1 )
       output[ nOutIndex ] |= 1;

    output[ nOutIndex ] <<= 1;
}    

This example shows a simple method for setting the bit and shifting.此示例显示了一种设置位和移位的简单方法。 Please note this is only the inner loop, and there's no code for moving through the bin array (from the second char onwards.请注意,这只是内部循环,没有用于移动 bin 数组的代码(从第二个字符开始。

It is unimportant what is the type of the bin . bin的类型是什么并不重要。 It may be some integer type.它可能是某种整数类型。 For the demonstrative purpose I used type uint8_t .出于演示目的,我使用了类型uint8_t You can use for example type int .例如,您可以使用类型int

The program can look the following way该程序可以如下所示

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

int main( void )
{
    uint8_t bin[][8] =
    {    
        { 0, 1, 1, 0, 0, 0, 0, 1 },
        { 0, 1, 1, 0, 0, 1, 0, 1 },
        { 0, 1, 1, 1, 0, 0, 1, 0 },
        { 0, 1, 1, 1, 0, 0, 1, 1 },
        { 0, 1, 1, 0, 0, 1, 1, 1 },
        { 0, 1, 1, 0, 1, 0, 1, 0 },
        { 0, 1, 1, 0, 1, 0, 0, 0 },
        { 0, 1, 1, 0, 0, 0, 0, 1 },
        { 0, 1, 1, 1, 0, 1, 0, 0 },
        { 0, 0, 1, 0, 0, 1, 1, 1 },
        { 0, 1, 0, 0, 1, 0, 1, 0 },
        { 0, 1, 0, 0, 1, 0, 0, 0 },
    };

    const size_t N = sizeof( bin ) / sizeof( *bin ); 
    char s[N + 1] = { '\0' };

    for ( size_t i = 0; i < N; i++ )
    {
        for ( size_t j = 0; j < 8; j++  )
        {
            s[i] = ( ( unsigned char )s[i] << 1 ) | ( bin[i][j] & 1 );
        }
    }

    puts( s );
}    

The program output is程序输出是

aersgjhat'JH

Get a value_so_far initialized to 0获取初始化为0value_so_far

int value_so_far = 0;

Read a bit, multiply the value_so_far by 2 and add that bit读取一点,将value_so_far乘以2并添加该位

int bit;
if (scanf("%d", &bit) != 1) /* error */;
if (bit < 0 || bit > 1) /* error */;
value_so_far *= 2;
value_so_far += bit;

After 8 times, you have an 8-bit value. 8 次后,您将获得一个 8 位值。
Restart.重新开始。

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

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