標題很複雜
也就是有一個文字檔裡面存著16進位表示式
例如:abc.txt
0x41 0x42 0x43 0x44 0x45 0x46 0x47
用ascii來看,分別代表:A、B、C、D、E、F、G。
可是檔案讀進來時是
{ "0x41", "0x42", "0x43", "0x44", "0x45", "0x46", "0x47" }
但是實際上想要讀到的是
{ 'A', 'B', 'C', 'D', 'E', 'F', 'G' }
有鑑於此,就有了下面這個程式。
#include <stdio.h>
#include <windows.h>
using namespace std;
int main(void) {
FILE *st;
char buf[5];
int a, b;
st = fopen("abc.txt", "r");
if (st != NULL)
{
while (!feof(st))
{
fgets(buf, 5, st);
if ( buf[1] == 'x' )
{
a = (unsigned char)buf[2];
if ( a > 64 )
b = ( a - 65 + 10 ) * 16;
else
b = ( a - 48 ) * 16;
a = (unsigned char)buf[3];
if ( a > 64 )
b = b + ( a - 65 + 10 );
else
b = b + ( a - 48 );
printf("%d, %c\n", b , b);
}
}
fclose(st);
}
system("PAUSE");
return 0;
}
收工。
浙公网安备 33010602011771号