Binary 在c语言中将多行转换为二进制

Binary 在c语言中将多行转换为二进制,binary,Binary,我试图读取文件中的多行,将每行转换为二进制。我认为对于每一行,我们需要每4个字符进行一次移位,以便存储下一个4个字符。但是,我不确定是否存储二进制文件(即0010)的唯一方法是作为字符串。有人能帮我吗?我不知道该怎么做。非常感谢 试试这个: char inter[5]; const char *binary = std::bitset<4>(input [i] -'0').to_string().c_str(); //to binary strcpy(inter, binary);

我试图读取文件中的多行,将每行转换为二进制。我认为对于每一行,我们需要每4个字符进行一次移位,以便存储下一个4个字符。但是,我不确定是否存储二进制文件(即0010)的唯一方法是作为字符串。有人能帮我吗?我不知道该怎么做。非常感谢

试试这个:

char inter[5];

const char *binary = std::bitset<4>(input [i] -'0').to_string().c_str(); //to binary
strcpy(inter, binary);
charinter[5];
const char*binary=std::bitset(输入[i]-'0')。到_string().c_str()//二进制
strcpy(inter,二进制);
需要包括
,并且

#包括
#包括
#包括
int main()
{
int n,k;
char*nptr=“1e2”,*endptr;//nptr包含十六进制
n=strtol(nptr,&endptr,16);//n包含十进制数
char-bin[33];//bin包含二进制文件
int指数=0;
而(n!=0)
{
如果(n&1)
bin[index++]='1';
其他的
bin[index++]='0';
n=n>>1;
}
bin[索引]='\0';
printf(“%s”,strev(bin));
返回0;
}
编辑:
如果您想要一个带前导零的32位数字,则:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int n, c, k;
    char * nptr = "1e2", *endptr;              // nptr contains hex
    n = strtol(nptr, &endptr, 16);
    char bin[33];
    int index = 0;
    for (c = 31; c >= 0; c--)
    {
        k = n >> c;
        if (k & 1)
            bin[index++] = '1';
        else
            bin[index++] = '0';
    }
    bin[index] = '\0';
    printf("%s", bin);
    return 0;
}
#包括
#包括
#包括
int main()
{
int n,c,k;
char*nptr=“1e2”,*endptr;//nptr包含十六进制
n=strtol(nptr和endptr,16);
char-bin[33];
int指数=0;
对于(c=31;c>=0;c--)
{
k=n>>c;
if(k&1)
bin[index++]='1';
其他的
bin[index++]='0';
}
bin[索引]='\0';
printf(“%s”,bin);
返回0;
}
#包括
#包括
#包括
int main(int argc,char*argv[]){
静态常量字符*表[]={
"0000","0001","0010", "0011",
"0100","0101","0110", "0111",
"1000","1001","1010", "1011",
"1100","1101","1110", "1111"
};
FILE*FILE=fopen(“data.txt”、“r”);
字符输入[5],输出[13]={0};
while(fgets(输入,5,文件)!=NULL){
char*p=输出;
对于(int i=0;i<3;++i){
if(isdigit(输入[i]))
memcpy(p,表[input[i]-'0'],4);
else//a-f
memcpy(p,表[tolower(输入[i])-'a'+10],4);
p+=4;
}
投入(产出);
}
fclose(文件);
返回0;
}

这接受三个字符的十六进制输入,并输出12个字符的二进制字符串。函数将接受参数以更改输入和输出长度。iLenOut应至少为十六进制输入*4的长度

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

int hextobin ( char *pacHexIn, char *pacBinOut, int iLenOut) {
    char acHexChars[17] = { "0123456789abcdef"};
    char *pcEachHex = NULL;
    unsigned int  uiHexIndex = 0;
    int  iEachBit = 0;
    int  iInputLen = 0;
    int  iOutputLen = iLenOut;

    if ( iLenOut % 4) {
        printf ( "as each hex character will result in 4 bits, use an even multiple of 4\n");
        return 2;
    }
    while ( iOutputLen) {
        pacBinOut[iOutputLen] = '0';
        iOutputLen--;
    }
    iOutputLen = iLenOut;
    iInputLen = strlen ( pacHexIn); // get length of input
    while ( iInputLen > 0) {
        iInputLen--;
        iOutputLen -= 4;
        pacHexIn[iInputLen] = tolower( pacHexIn[iInputLen]); // make sure input is lower case
        if ( ( pcEachHex = strchr ( acHexChars, pacHexIn[iInputLen])) != NULL) { // get index to acHexChars
            uiHexIndex = pcEachHex - acHexChars; // difference will be the value of the hex char f=15...

            for ( iEachBit = 3; iEachBit >= 0; iEachBit--) { // loop through the bits
                if ( 1 & ( uiHexIndex >> iEachBit)) { // right shift and compare to 1
                    pacBinOut[ iOutputLen + ( 3 - iEachBit)] = '1'; // set to 1
                }
                else {
                    pacBinOut[ iOutputLen + ( 3 - iEachBit)] = '0'; // set to 0
                }
            }
        }
        else {
            printf ( "Only input hex values 0-9 and a-f\n"); // invalid input
            return 1;
        }
    }
    pacBinOut[iLenOut] = '\0'; // make sure string is terminated
    return 0;
}

int main()
{
    char acInput[9] = { 0};
    char acBinary[33] = { "00000000000000000000000000000000"};
    scanf ( "%3s", acInput); // input 3 hex characters, acInput will allow for up to 8 characters
    if ( ( hextobin ( acInput, acBinary, 12)) == 0) { // return binary string of 12 characters, acBinary will allow up to 32
        printf ( "%s\n", acBinary); // print result
    }
    else {
        printf ( "conversion failed\n");
    }
    return 0;
}
#包括
#包括
#包括
#包括
int-hextobin(char*pacHexIn,char*pacBinOut,int-iLenOut){
char acHexChars[17]={“0123456789abcdef”};
char*pcEachHex=NULL;
无符号整数uiHexIndex=0;
int-iEachBit=0;
int iInputLen=0;
int iOutputLen=iLenOut;
如果(iLenOut%4){
printf(“由于每个十六进制字符将产生4位,请使用4的偶数倍数”);
返回2;
}
while(iOutputLen){
pacBinOut[iOutputLen]=“0”;
尤特普特伦--;
}
iOutputLen=iLenOut;
iInputLen=strlen(pacHexIn);//获取输入的长度
而(iInputLen>0){
输入--;
iOutputLen-=4;
pacHexIn[iInputLen]=tolower(pacHexIn[iInputLen]);//确保输入为小写
如果((pcEachHex=strchr(acHexChars,pacHexIn[iInputLen]))!=NULL){//获取acHexChars的索引
uiHexIndex=pcEachHex-acHexChars;//差值将是十六进制字符f=15的值。。。
对于(iEachBit=3;iEachBit>=0;iEachBit--){//循环通过位
如果(1&(uiHexIndex>>iEachBit)){//右移并与1比较
pacBinOut[iOutputLen+(3-iEachBit)]=“1”;//设置为1
}
否则{
pacBinOut[iOutputLen+(3-iEachBit)]=“0”;//设置为0
}
}
}
否则{
printf(“仅输入十六进制值0-9和a-f\n”);//输入无效
返回1;
}
}
pacBinOut[iLenOut]='\0';//确保字符串已终止
返回0;
}
int main()
{
字符输入[9]={0};
字符acBinary[33]={“00000000000000000000000000”};
scanf(“%3s”,acInput);//输入3个十六进制字符,acInput最多允许8个字符
如果((hextobin(acInput,acBinary,12))==0{//返回12个字符的二进制字符串,acBinary最多允许32个字符
printf(“%s\n”,acBinary);//打印结果
}
否则{
printf(“转换失败\n”);
}
返回0;
}

十六进制、十进制和二进制仅表示整数,它们可以存储为字符串。您的
strcat
想法可行,但您必须在
inter
中使用零终止,并且您应该使用ASCII码作为数字,即
'0'
'1'
在单引号中,而不是
0
1
。更好的是,在双引号中使用字符串文字:
strcat(输出,“0011”)
。忘记for循环。这确实很尴尬(你需要16个箱子标签)。对于每一行读取,使用
strtol
将十六进制字符串转换为一个数字,然后使用shift(user4056620
strtol()等)将该数字转换为二进制
将字符串转换为C长整数。该整数作为二进制数存储在内存中。但要获取这些位,您需要使用C的位操作运算符,如
>
&
和` ` ` `。@AshwaniDausodia:可能吧,但我们这里讲的是C。@MichaelWalz:好的。我想这是有意义的,因为我们正在处理bitstrings是一个固定的已知长度。不幸的是,我的方法以相反的顺序生成位。谢谢!但我不太明白这里的逻辑:n=strtol(nptr,&endptr,16)是否将nptr转换为十进制数?应该是strtol(nptr,&endptr,10)?另一件事是科学n是十进制的,即234,我们如何使用n&1来获得bi
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(int argc, char *argv[]){
    static const char *table[] = {
        "0000","0001","0010", "0011",
        "0100","0101","0110", "0111",
        "1000","1001","1010", "1011",
        "1100","1101","1110", "1111"
    };
    FILE *file = fopen("data.txt", "r");
    char input[5], output[13] = {0};

    while (fgets(input, 5, file)!=NULL){
        char *p = output;
        for ( int i = 0; i < 3; ++i){
            if(isdigit(input[i]))
                memcpy(p, table[input[i]-'0'], 4);
            else //a-f
                memcpy(p, table[tolower(input[i])-'a'+10], 4);
            p += 4;
        }
        puts(output);
    }
    fclose(file);
    return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

int hextobin ( char *pacHexIn, char *pacBinOut, int iLenOut) {
    char acHexChars[17] = { "0123456789abcdef"};
    char *pcEachHex = NULL;
    unsigned int  uiHexIndex = 0;
    int  iEachBit = 0;
    int  iInputLen = 0;
    int  iOutputLen = iLenOut;

    if ( iLenOut % 4) {
        printf ( "as each hex character will result in 4 bits, use an even multiple of 4\n");
        return 2;
    }
    while ( iOutputLen) {
        pacBinOut[iOutputLen] = '0';
        iOutputLen--;
    }
    iOutputLen = iLenOut;
    iInputLen = strlen ( pacHexIn); // get length of input
    while ( iInputLen > 0) {
        iInputLen--;
        iOutputLen -= 4;
        pacHexIn[iInputLen] = tolower( pacHexIn[iInputLen]); // make sure input is lower case
        if ( ( pcEachHex = strchr ( acHexChars, pacHexIn[iInputLen])) != NULL) { // get index to acHexChars
            uiHexIndex = pcEachHex - acHexChars; // difference will be the value of the hex char f=15...

            for ( iEachBit = 3; iEachBit >= 0; iEachBit--) { // loop through the bits
                if ( 1 & ( uiHexIndex >> iEachBit)) { // right shift and compare to 1
                    pacBinOut[ iOutputLen + ( 3 - iEachBit)] = '1'; // set to 1
                }
                else {
                    pacBinOut[ iOutputLen + ( 3 - iEachBit)] = '0'; // set to 0
                }
            }
        }
        else {
            printf ( "Only input hex values 0-9 and a-f\n"); // invalid input
            return 1;
        }
    }
    pacBinOut[iLenOut] = '\0'; // make sure string is terminated
    return 0;
}

int main()
{
    char acInput[9] = { 0};
    char acBinary[33] = { "00000000000000000000000000000000"};
    scanf ( "%3s", acInput); // input 3 hex characters, acInput will allow for up to 8 characters
    if ( ( hextobin ( acInput, acBinary, 12)) == 0) { // return binary string of 12 characters, acBinary will allow up to 32
        printf ( "%s\n", acBinary); // print result
    }
    else {
        printf ( "conversion failed\n");
    }
    return 0;
}