0
ファイルの内容を16進数とASCIIで印刷しようとしています。私は、ファイルを読むためにforループを間違って使用していると思います。コードこの自分のプログラムでファイルを読みにくい。ファイルを読むためにforループを正しく使用していますか?
00000000 3C 54 49 54 4C 45 3E 43 50 45 34 39 32 20 52 65 <TITLE>CPE492 Re
00000010 73 6F 75 72 63 65 73 3C 2F 54 49 54 4C 45 3E 0A sources</TITLE>.
に似たコードアウトプットは次のようになります。
#include <stdio.h>
int main() {
FILE * file;
file = fopen("BinaryFile.txt" , "r");
if (file == NULL) {
printf("Error: %m\n");
}
char textFile[1000];
for(int j=0;j<1000; j++) {
fgets(textFile, 1000, file);
char binaryNumber[1000],hexaDecimal[1000];
int temp;
long int i=0,j=0;
while(binaryNumber[i]){
binaryNumber[i] = binaryNumber[i] -48;
++i;
}
--i;
while(i-2>=0){
temp = binaryNumber[i-3] *8 + binaryNumber[i-2] *4 + binaryNumber[i-1] *2 + binaryNumber[i] ;
if(temp > 9)
hexaDecimal[j++] = temp + 55;
else
hexaDecimal[j++] = temp + 48;
i=i-4;
}
if(i ==1)
hexaDecimal[j] = binaryNumber[i-1] *2 + binaryNumber[i] + 48 ;
else if(i==0)
hexaDecimal[j] = binaryNumber[i] + 48 ;
else
--j;
printf("Equivalent hexadecimal value: ");
while(j>=0){
printf("%c",hexaDecimal[j--]);
}
return 0;
}
}
ここで出力されるものは何ですか? – ameyCU
1行に16個の値が必要な場合は、 'while(bytesread = fread(textFile、1,16、file))> 0){}'のようなループで 'fread'を使用し、 '(j = 0; j
あなたは 'printf("%02X "、textFile [j]);'を見たことがありますか?それはあなたの周りにいろいろな手間を省くでしょう。 –