C 从字符中提取位序列
所以我有一个字符数组,像下面的{h,e,l,l,o,o} 所以我首先需要把它转换成它的位表示,所以我要的是这个C 从字符中提取位序列,c,binary,character,ascii,bits,C,Binary,Character,Ascii,Bits,所以我有一个字符数组,像下面的{h,e,l,l,o,o} 所以我首先需要把它转换成它的位表示,所以我要的是这个 h = 01101000 e = 01100101 l = 01101100 l = 01101100 o = 01101111 o = 01101111 我需要将所有这些位分成五组,并将其保存到一个数组中 例如,所有这些字符的并集是 011010000110010101101100011011000110111101101111 现在我把它分成五组 01101 00001 100
h = 01101000
e = 01100101
l = 01101100
l = 01101100
o = 01101111
o = 01101111
我需要将所有这些位分成五组,并将其保存到一个数组中
例如,所有这些字符的并集是
011010000110010101101100011011000110111101101111
现在我把它分成五组
01101 00001 10010 10110 11000 11011 00011 01111 01101 111
最后一个序列应该用零来完成,所以它应该是00111。注:每组5位将用一个标头完成,以便有8位
所以我还没有意识到如何做到这一点,因为我可以提取每个字符的5位,并得到每个字符的二进制表示,如下所示
for (int i = 7; i >= 0; --i)
{
printf("%c", (c & (1 << i)) ? '1' : '0');
}
用于(int i=7;i>=0;--i)
{
printf(“%c”),(c&(1假设一个字节由8位组成(注意:c标准不能保证这一点),您必须在字符串上循环并播放位操作才能完成:
>n
右移以除去n个最低位
>(8-j))| last)&0x1f;//设置最后一个高位,后跟移位到低位的j位;只保留5位
printf(“%02x”,(无符号)输出);
j+=5;//走下一个街区
last=(*p这里有一些代码可以解决您的问题:
#include <stdio.h>
#include <string.h>
int main(void)
{
char arr[6] = {'h', 'e', 'l', 'l', 'o', 'o'};
char charcode[9];
char binarr[121] = "";
char fives[24][5] = {{0}};
int i, j, n, numchars, grouping = 0, numgroups = 0;
/* Build binary string */
printf("\nCharacter encodings:\n");
for (j = 0; j < 6; j++) {
for (i = 0, n = 7; i < 8; i++, n--)
charcode[i] = (arr[j] & (01 << n)) ? '1' : '0';
charcode[8] = '\0';
printf("%c = %s\n", arr[j], charcode);
strcat(binarr, charcode);
}
/* Break binary string into groups of 5 characters */
numchars = strlen(binarr);
j = 0;
while (j < numchars) {
i = 0;
if ((numchars - j) < 5) { // add '0' padding
for (i = 0; i < (5 - (numchars - j)); i++)
fives[grouping][i] = '0';
}
while (i < 5) { // write binary digits
fives[grouping][i] = binarr[j];
++i;
++j;
}
++grouping;
++numgroups;
}
printf("\nConcatenated binary string:\n");
printf("%s\n", binarr);
printf("\nGroupings of five, with padded final grouping:\n");
for (grouping = 0; grouping <= numgroups; grouping++) {
for (i = 0; i < 5; i++)
printf("%c", fives[grouping][i]);
putchar(' ');
}
putchar('\n');
return 0;
}
#包括
#包括
#定义组大小为5
静态int nextBit(void);
静态int nextGroup(char*dest);
静态字符str[]=“helloo”;
内部主(空){
字符位[组大小+1];
int第一次,nBits;
第一次=1;
而((nBits=nextGroup(位))==组大小){
如果(!第一次){
(无效)putchar(“”);
}
第一次=0;
(无效)printf(“%s”,位);
}
如果(nBits>0){
如果(!第一次){
(无效)putchar(“”);
}
while(nBits++<组大小){
(无效)putchar('0');
}
(无效)printf(“%s”,位);
}
(void)putchar('\n');
返回0;
}
静态int nextBit(void){
静态int-bitI=0,charI=-1;
如果(--bitI<0){
bitI=字符位-1;
如果(str[++charI]='\0'){
返回-1;
}
}
返回(str[charI]&(1)您想要实现的东西没有意义。这几乎与base64相同。base64每个字符存储6位。与您想要的非常相似,除了base64从每3个字节收集额外的2位,并使用前3个字节中的6个额外位生成每4个字节。@doug65536对于每一组5位,我将添加一个头来完成对于8位,问题是我不知道如何获得所有这五个组。你不只是通过连接'h'=“011010000”的结果来构建一个字符串吗..
然后将字符分成五组,直到剩下的字符少于5个,填充最后一组。一个循环,一些用于跟踪位偏移的变量,一些移位和屏蔽,以及一个输出缓冲区。
#include <stdio.h>
#include <string.h>
int main(void)
{
char arr[6] = {'h', 'e', 'l', 'l', 'o', 'o'};
char charcode[9];
char binarr[121] = "";
char fives[24][5] = {{0}};
int i, j, n, numchars, grouping = 0, numgroups = 0;
/* Build binary string */
printf("\nCharacter encodings:\n");
for (j = 0; j < 6; j++) {
for (i = 0, n = 7; i < 8; i++, n--)
charcode[i] = (arr[j] & (01 << n)) ? '1' : '0';
charcode[8] = '\0';
printf("%c = %s\n", arr[j], charcode);
strcat(binarr, charcode);
}
/* Break binary string into groups of 5 characters */
numchars = strlen(binarr);
j = 0;
while (j < numchars) {
i = 0;
if ((numchars - j) < 5) { // add '0' padding
for (i = 0; i < (5 - (numchars - j)); i++)
fives[grouping][i] = '0';
}
while (i < 5) { // write binary digits
fives[grouping][i] = binarr[j];
++i;
++j;
}
++grouping;
++numgroups;
}
printf("\nConcatenated binary string:\n");
printf("%s\n", binarr);
printf("\nGroupings of five, with padded final grouping:\n");
for (grouping = 0; grouping <= numgroups; grouping++) {
for (i = 0; i < 5; i++)
printf("%c", fives[grouping][i]);
putchar(' ');
}
putchar('\n');
return 0;
}
Character encodings:
h = 01101000
e = 01100101
l = 01101100
l = 01101100
o = 01101111
o = 01101111
Concatenated binary string:
011010000110010101101100011011000110111101101111
Groupings of five, with padded final grouping:
01101 00001 10010 10110 11000 11011 00011 01111 01101 00111
#include <limits.h>
#include <stdio.h>
#define GROUP_SIZE 5
static int nextBit(void);
static int nextGroup(char *dest);
static char str[] = "helloo";
int main(void) {
char bits[GROUP_SIZE + 1];
int firstTime, nBits;
firstTime = 1;
while ((nBits = nextGroup(bits)) == GROUP_SIZE) {
if (!firstTime) {
(void) putchar(' ');
}
firstTime = 0;
(void) printf("%s", bits);
}
if (nBits > 0) {
if (!firstTime) {
(void) putchar(' ');
}
while (nBits++ < GROUP_SIZE) {
(void) putchar('0');
}
(void) printf("%s", bits);
}
(void) putchar('\n');
return 0;
}
static int nextBit(void) {
static int bitI = 0, charI = -1;
if (--bitI < 0) {
bitI = CHAR_BIT - 1;
if (str[++charI] == '\0') {
return -1;
}
}
return (str[charI] & (1 << bitI)) != 0 ? 1 : 0;
}
static int nextGroup(char *dest) {
int bit, i;
for (i = 0; i < GROUP_SIZE; ++i) {
bit = nextBit();
if (bit == -1) {
break;
}
dest[i] = '0' + bit;
}
dest[i] = '\0';
return i;
}