70,020
社区成员




//比如我有一个char str[]="01001000011001010110110001101100011011110";
// 和 char word[1000] // 用于储存2进制的数字
//如何分别将str的 01001000 01100101 01101100 01101100 01101111 0 分别存入 word[0], word[1], word[2], word[3], 和 word[4]. 然后printf("%s",word)的时候可以直接打出"Hello".
//
//ASCII:
//01001000 --> H
//01100101 --> e
//01101100 --> l
//01101100 --> l
//01101111 --> o
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char str[]="0100100001100101011011000110110001101111";
char word[1000];
char bs[9];
char *s;
char *e;
s=str;
for (int i=0;i<5;i++) {
sprintf(bs,"%.8s",s+8*i);
word[i]=(char)strtol(bs,&e,2);
}
word[5]=0;
printf("%s",word);//Hello
return 0;
}
const char* p = "0100100001100101011011000110110001101111";
char* dest = new char[strlen(p) / 8+1]{0};
for (int i = 0; i < strlen(p); i += 8) {
char strbyte[9]{ 0 };
memcpy(strbyte, p+i, sizeof(char)*8);
dest[i / 8] = strtol(strbyte, 0, 2);
}
char word[1000]={0};
int i,j;
for(i=0;i<strlen(str)/8;i++)
for(j=0;j<8;j++)
{
world[i]|=(str[8*i+j]-'0')<<(7-j);
}
大约就酱紫。