首页 > 代码库 > 读取二进制转换为字符串

读取二进制转换为字符串

#include<stdio.h> 
#include<stdlib.h>//为了使用exit() 
int swapInt32(int value)
{
    return ((value & 0x000000FF) << 24) |
        ((value & 0x0000FF00) << 8) |
        ((value & 0x00FF0000) >> 8) |
        ((value & 0xFF000000) >> 24) ;
}

int main()
{
    int a,b;
    FILE *outFp, *inFp;
    inFp=fopen("DMIMOLog.DAT","rb+");
    if(inFp==NULL){
        printf("open in file error!\n");
        return 0;
    }
    outFp=fopen("DMIMOLog.txt","w+");
    if(outFp==NULL){
        printf("open outFp file error!\n");
        return 0;
    }

    for(int i=0;i<8192*2;i++)
    {
        fread(&a,sizeof(int),1,inFp);
        b=swapInt32(a);
        //printf("%05x\r\n",b);
        fprintf(outFp,"%05x\r\n",b);
    }
    fclose(inFp);
    fclose(outFp);
    printf("finish\r\n");
}

 

读取二进制转换为字符串