【发布时间】:2020-11-15 04:31:57
【问题描述】:
我想读取一个文件,将其转换为十六进制字符串,然后将结果保存在一个 8 位(或一个字节)数组中。我正在逐字符读取文本文件并将其转换为十六进制并成功存储结果字符串,但现在我想将结果字符串数组放入二维数组矩阵中。
这个我试过了。
#include <stdio.h>
#include <string.h>
#include <stdint-gcc.h>
int main()
{
int i=0,j;
FILE *filePointer;
char ch;
int counter=0;
char hex[50];
filePointer = fopen("plaintext1.txt", "r");
if (filePointer == NULL)
printf("File is not available \n");
else
{
while ((ch = fgetc(filePointer)) != EOF)
{
printf("%c", ch);
hex[i] = ch;
i++;
counter++;
}
}
/*set strH with nulls*/
unsigned char strH[200];
memset(strH,0,sizeof(strH));
/*converting str character into Hex and adding into strH*/
for(i=0,j=0;i<counter;i++,j+=2)
{
sprintf((char*)strH+j,"%02X",hex[i]);
}
strH[j]='\0'; /*adding NULL in the end*/
printf("\nHexadecimal converted string is: \n");
printf("%s\n",strH);
//here i want to store the result in an array but failed here
printf("\nThe matrix of the Hex is : \n");
uint8_t text_hex[counter];
int a ,b;
for (a=0;a<strlen(strH) ;a++ ){
text_hex[a] = strH[a];
printf("%x",text_hex[a]); //this thing is annoying
}
fclose(filePointer);
return 0;
}
【问题讨论】: