English 中文(简体)
简单地将0和1条插图转化为特性? Plain C
原标题:Easy way to convert a string of 0 s and 1 s into a character? Plain C

I m 从事一个植物学项目,我用纸质从纸浆中读过,在阵列中添加最不重要的字体。 因此,一旦8个字塔被读到,我就会有8个字眼,在隐藏的电文中,这应该具有某种特性。 将零和1阵列转化为二值是否容易? 例如,阵列:char bits[] ={0,1,1,01,0,1,00}。 页: 1

感谢所有答复。 I m gonna向其中一些人开枪。

最佳回答

缩略语 休息会奏效——像样


    unsigned char ascii = 0;
    unsigned char i;

    for(i = 0; i < 8; i++)
       ascii |= (bits[7 - i] << i);

这样做可能更快,但至少是一个开端。

问题回答

我会把借方存放在一阵列中——我把借方交给他们。

因此,你开始使用0的果园:char bit = 0;

当你获得第一种轨道时,OR与你拥有的: bit> bit> bit_just_read;

在每个轨道上保持这一作用,适当转移;即,在获得下一个轨道之后,在<代码>bit>>>>(next_bit << 1)。 等等。

在读了你的8个轨道之后,bit将是适当的ASCII值,而且你可以将其印出或与你想要做的一切。

我赞同好奇帕迪,不把双管放在一阵列中,这种无足轻重。 由于你在阅读时必须掌握或以其他方式跟踪阵列指数,你也可以这样做。 也许这样一些?

bits = 0;

for ( i = 0; i < 8; ++i ) {
    lsb = get_byte_from_ppm_somehow() & 0x01;
    bits <<= 1 | lsb;
}

As long as the bit endian is correct, this should work and compile down pretty small. If the bit endian is backwards then you should be able to change the initial value of mask to 1, the mask shift to <<= , and you might need to have (0x0ff & mask) as the do{}while conditional if your compiler doesn t do what it s supposed to with byte sized variables. Don t forget to do something for the magic functions that I included where I didn t know what you wanted or how you did something

#include <stdint.h> // needed for uint8_t
...
uint8_t acc, lsb, mask;
uint8_t buf[SOME_SIZE];
size_t len = 0;

while (is_there_more_ppm_data()) {
    acc = 0;
    mask = 0x80; // This is the high bit
    do {
         if (!is_there_more() ) {
             // I don t know what you think should happen if you run out  on a non-byte boundary
             EARLY_END_OF_DATA();
             break;
         }
         lsb = 1 & get_next_ppm_byte();
         acc |= lsb ? mask : 0; // You could use an if statement
         mask >>= 1;
    } while (mask);
    buf[len] = acc; // NOTE: I didn t worry about the running off the end of the buff, but you should. 
    len++;
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签