如何从文本文件中读取十六进制值数据并复制到数组中? [英] How to read hex value data from text file and copy into the array?

查看:99
本文介绍了如何从文本文件中读取十六进制值数据并复制到数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hello..i有包含十六进制值(0x00,0xAA,0x56,...等)的文本文件总共498字节数据....我已经打开并正确读取此文件但我被保存用于存储此498字节数组中的数据声明???



文本文件数据是==>

 0x04,0x2C ,0x64,0x00,0xB6,0x03,0xFA,0x84,0x87,0x6A,0x74,0x18,0x01,0x64,0x79,0x13,0x09,0x6A,0xCB,0xED,0x3D之间,为0x9c,0x79,0x88,0x49,0x4A,0x2E之间,0xE8,0x89,0x24,0xF7,0xDD,0x04,0xA9,0x09,0xE7,0xE5,0x14,0xF7,0x5B,0xC5,0x49,0x38,0x10,0xFA,0x5C,0xFA回应,0x99,0xC4,0x0D,0x74,0xE0 ,0x3A,0x45,0xE8,0x64,0xCC,为0xBB,0x72,0x57,0x31,0x63,0xF9,0x7F,0x46,0x48,0xF4,0xE8,0x91,0x5B,0xF8,0xED,0x07,0x9B,0x09,0x07,0x0D ,0x64,0xF7,0x64,0xC7,0x5A,0xB8,0x17,0xCD,0xBC,的0x6A,0xA9,0xC3,0x9D,0xEF,0x96,0x9D,0x62,0xF5,0xDD,0x83,0xD9,0x45,0x0F,0x99,0x4B ,0xF6,0x63,0x41,0x29,0x38,0xE0,0x4A,0XCD,0xF9,0x94,0xC3,0x1D,0xAE,0xB8,0x96,0x3D,0x69,0x82,0x8C,0xFF时,0x29,0xF9,0x2A,0xF6,0x85 ,0x50,0x45,0x1A,0x7E的,0xF8,0x26,0x66,0x86,0x43,0x48,0x4B,0x3E的,0xF9,0x96,0xD4,0x6A,0X 33,0xC3,0x6B,0x75,0xFD,0x29,0x95,0xAA,0x27,0xC4,0xBD,0xF7,0x0F,0x99,0x1D,将0xEB,为0x9c,0x84,0xFB,0x7B,0xF8,0x4D,0x3A,0xFA回应,0×12, 0x43,0xC9,0x43,0x0F,0x66,0x52,0xF7,0x6F,0x87,0x4A,0x36,0xE9,0xD5,0x84,0x8B,0x9A执行,0x8B,0xFF时,0xC1,0x0F,0x2D,0x25,0x8B,0x99,0xCE, 0xFE的,0xC3,0xFF,为0x0E,0xC9,0xF8,0x88,0x83,0xD9,0x83,0xD8,0x5D,0x91,0xF5,0x76,0xC7,0x3A,0xF8,0xE0,0x86,0x71,0xF7,0x7C,0x04,0x3A, 0xFA回应,0xD8,0x95,0x69,0x4B,值为0x8F,0xC5,0xFA,0xFB的才能,0xEF,0xBD,0x19,0xF6,0x05,0xC6,0xB8,0x7B,0x16,0xD5,0x71,0xF6,0x7D,0x46,0xD7,0xB7, 0xC0,0xE5,0x11,0xF7,0x75,0x04,0x4A,0x3A,0xE1,0x72,0xD9,0x4B,0x96,0x89,0xFF,0xFF时,0xDF,0x82,0xF2,0xF4,0x65,0x44,0x3B,0xB8,0xF0, 0xF9,0xF9,0x43,0x50,0xC4,0x9D,0xFE的,0x37,0x62,0xB8,0xA7,0x88,0x43,0xE9,0xC1,0xEF,0xB5,0x50,0xF7,0x87,0x85,0x17,0x40,0xE8,0x01, 0x98,0x4A,0x86,0xC7,0xF7,0xFF,为0x0F,0x31,0x38,0x4A,0x0B中,0x44,0x38,0x7E,0xF8,0x59,0xE8,0x49,0x8B,0xC3,0xD9,0xFF,0xEF,0x72,0xE0, 0x4A,0x0C,0xC7,0x29,0x3E,0xF8,0x45,0x33,0x7C,0x1B,0x02,0xD,0x3F的,0xF8,0xB1,0xF8,0x45,0x80,0x44,0x19,0xBE,0xE0,0xD6,0xC8,0x45,为0x0 4,0xC3,0xF9,0x05,0x1F,0xA1,0x6B,0x7C,0x94,0xC3,0xF7,0xFB,为0x0F,0xA3,0x2F,0x3F的,0x11,0x27,0x42,0xF7,0x44,0x1F,0x65,0x62,0x52, 0x42,0x4A,0x34,0x22,0x13,0x21,0x54,0x18,0x22,0x85,0xFF,0x43,0x32,0xB8,0xA5,0xF7,0xFF,0x12,0xF2,0x1F,0xC1,0x68,0x48,0x86,0x2F, 0x22,0xF8,0x26,0x23,0xFF,为0x0F,0xF6,0xFF,0x3F的,0x77,0x45,0x1F,0x1F的,0x17,0xF4,0x74,0x34,0x36,0x73,0x3F,0x26,0xF2,0x1F,0x6F,0x92, 0x41,0xF7,0xFF,0x77,0xA6,0xF4,0x8F,0x4F,0xF4,0xF9,0x33,0x75,0x32,0xF4,0xF1,0xF8,0xF1,0x1F,值为0x8F,0x46,0x36,0x06,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xA2,0xE4 




下面的
是文件中打开和读取数据的代码==>



  #include   <   stdio.h  >  
#include < stdlib.h >
int main()
{
FILE * f;
char c;
f = fopen( 1.txt r);
while ((c = fgetc(f))!= EOF)
{
printf( %c,c);
}
fclose(f);
return 0 ;
}< /stdlib.h>< /stdio.h>





[更新(解决方案1)]

i已经尝试打开并读取文本文件...但是为了将这些数据存储到数组中我被收集了,我有这样的字符字符...



  #include    <   stdio.h  >  
int main()
{
FILE * in ;
char ch,cw;
unsigned char str [ 508 ] = { 0 };
int j,i = 0 ;
in = fopen( 1.txt r);
if in
{
while (!feof( in ))
{
ch = getc();
str [i] = ch;
i ++;
printf( %c,ch);
}
}< /stdio.h>





[更新(评论)]

 #include< stdio.h> 
#include< stdlib.h>
int main()
{
FILE * in ;
char ch,cw;
unsigned char str [ 508 ] = { 0 };
int j,i = 0 ;
in = fopen( 1.txt r);
if in
{
while (!feof( in ))
{
ch = getc();
str [i] = ch;
printf( %c,str [i]);
i ++;
}
}
for (j = 0 ; j< 498; j ++)
{
printf( %c,str [ J]);
}
printf( \ n);
fclose( in );
return 0 ;
}< /stdlib.h>< /stdio.h>

解决方案

当文件格式已知时用逗号分隔格式为'0xhh'的十六进制值,没有任何其他字符,如空格和换行符,你知道每个项目的长度为五个字符(最后一个除外)。



可能的解决方案可能会将文件内容读入 char 缓冲区。然后,可以使用 strtoul - C ++ Reference [ ^ ]或 sscanf - C ++参考 [ ^ ]为下一个项目将输入缓冲区指针递增5。



请注意,这些函数要求使用有效的非项目终止最后一项数字字符。因此输入缓冲区的大小必须比文件大小多一个,并且该字符必须设置为零才能创建 NULL 终止字符串。


< blockquote>你好,以下解决问题的每个人都是解决方案....

  #include   <   stdio.h  >  

int main( void ){
unsigned char tmparray [ 10 ] = {0xaa,0xbb,0xcc,0xdd,0xee,0x00,0x00,0x00,0x00,0x00};

int idx = 5 ;
int 数据;
FILE * fp = fopen( 1.txt r);
if (fp == NULL){
fprintf(stderr, < span class =code-string>无法读取1.txt);
return 0 ;
}
while (fscanf(fp, %* c%* c%x,,& data)== 1 ){
tmparray [idx ++] =( unsigned char )数据;
}
fclose(fp);

for (idx = 0 ; idx< sizeof (tmparray)/ sizeof (tmparray [ 0 ]); idx ++){
printf( %#x,,tmparray [idx]);
}
printf( \ n);

return 0 ;
}


这是你的作业,所以我不会给你代码! :笑:



首先要做的是查看你应该做的事情:读取基于文本的文件,并通过转换基于字符串将其转换为字节十六进制值到内部数字。



我?我写了一个函数,它接受了一个文件名,并返回了一个无符号字符数组。

在函数中我将通过文件两次 - 即我从头开始读了两遍结束为什么?因为我想确保我要读取适量的数据!



所以第一遍将计算','字符的数量。这告诉我们输出数组中需要多少字节:如果文件包含0x01,0x02,0x03,那么我们将得到2个逗号的计数,这意味着我们将需要3个字节来存储数据。

然后我用malloc来分配一块大的内存块。



第二遍创建每个字节值:它读取四个文件中的字符,并确保第一个为'0',第二个为'x',然后转换另外两个。

这里,我写了另一个转换十六进制的函数它的字符数字等效:

'0' - > 0 
'1' - > 1
...
'9' - > 9
'A' - > 10
'B' - > 11
...
'F' - > 15
'a' - > 10
'b' - > 11
...
'f' - > 15

写起来非常简单!

然后构建值很简单:

 outputByteValue =(GetHexValue (firstHexChar)<<  4 )| GetHexValue(secondHexChar); 



将它放入输出数组,继续前进或下一步。

在函数结束时,返回无符号字符数组。



这很简单,如果你把它分解,因为所有单个任务都很容易,你可以测试每个在你进入下一个之前,你是孤立的。

试一试!


hello..i have text file containing hex value (0x00,0xAA,0x56,...etc) total 498 bytes data....i have open and read this file properly but i am cunfused for storing this 498 bytes data in array declared???

text file data is ==>

0x04,0x2C,0x64,0x00,0xB6,0x03,0xFA,0x84,0x87,0x6A,0x74,0x18,0x01,0x64,0x79,0x13,0x09,0x6A,0xCB,0xED,0x3D,0x9C,0x79,0x88,0x49,0x4A,0x2E,0xE8,0x89,0x24,0xF7,0xDD,0x04,0xA9,0x09,0xE7,0xE5,0x14,0xF7,0x5B,0xC5,0x49,0x38,0x10,0xFA,0x5C,0xFA,0x99,0xC4,0x0D,0x74,0xE0,0x3A,0x45,0xE8,0x64,0xCC,0xBB,0x72,0x57,0x31,0x63,0xF9,0x7F,0x46,0x48,0xF4,0xE8,0x91,0x5B,0xF8,0xED,0x07,0x9B,0x09,0x07,0x0D,0x64,0xF7,0x64,0xC7,0x5A,0xB8,0x17,0xCD,0xBC,0x6A,0xA9,0xC3,0x9D,0xEF,0x96,0x9D,0x62,0xF5,0xDD,0x83,0xD9,0x45,0x0F,0x99,0x4B,0xF6,0x63,0x41,0x29,0x38,0xE0,0x4A,0xCD,0xF9,0x94,0xC3,0x1D,0xAE,0xB8,0x96,0x3D,0x69,0x82,0x8C,0xFF,0x29,0xF9,0x2A,0xF6,0x85,0x50,0x45,0x1A,0x7E,0xF8,0x26,0x66,0x86,0x43,0x48,0x4B,0x3E,0xF9,0x96,0xD4,0x6A,0x33,0xC3,0x6B,0x75,0xFD,0x29,0x95,0xAA,0x27,0xC4,0xBD,0xF7,0x0F,0x99,0x1D,0xEB,0x9C,0x84,0xFB,0x7B,0xF8,0x4D,0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1F,0xC1,0x68,0x48,0x86,0x2F,0x22,0xF8,0x26,0x23,0xFF,0x0F,0xF6,0xFF,0x3F,0x77,0x45,0x1F,0x1F,0x17,0xF4,0x74,0x34,0x36,0x73,0x3F,0x26,0xF2,0x1F,0x6F,0x92,0x41,0xF7,0xFF,0x77,0xA6,0xF4,0x8F,0x4F,0xF4,0xF9,0x33,0x75,0x32,0xF4,0xF1,0xF8,0xF1,0x1F,0x8F,0x46,0x36,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xA2,0xE4



below is code for open and read data in file ==>

#include <stdio.h>
#include <stdlib.h>
int main()
{
	FILE *f;
	char c;
	f=fopen("1.txt","r");
	while((c=fgetc(f))!=EOF)
	{
		printf("%c",c);
	}
	fclose(f);
	return 0;
}</stdlib.h></stdio.h>



[Update (was Solution 1)]
i have tried for open and read the text file...but for storing this data into array i am cunfused i have charactor by charactor like this...

#include<stdio.h>
int main()
{
FILE *in;
char ch,cw;
unsigned char str[508]={0};
int j,i=0;
in=fopen("1.txt","r");
if(in)
{
   while(!feof(in))
   {
	ch=getc(in);
   	str[i] = ch;
	i++;
        printf("%c",ch);
   }
}</stdio.h>



[Update (was Comment)]

# include <stdio.h>
# include <stdlib.h>
int main()
{
FILE *in;
char ch,cw;
unsigned char str[508]={0};
int j,i=0;
in=fopen("1.txt","r");
if(in)
{
   while(!feof(in))
   {
	ch=getc(in);
   	str[i] = ch;
        printf("%c",str[i]);
	i++;
   }
}
for(j=0;j<498;j++)
{
        printf("%c",str[j]);
}
printf("\n");
fclose(in);
return 0;
}</stdlib.h></stdio.h>

解决方案

When the file format is known to have comma separated hex values in the format '0xhh' without any other characters like spaces and line feeds you know the length of each item to be five characters (except the last one).

A possible solution might read the file content into a char buffer. Then each item can be converted to an unsigned value using strtoul - C++ Reference[^] or sscanf - C++ Reference[^] incrementing the input buffer pointer by five for the next item.

Note that these functions require the last item to be terminated with a valid non-digit character. So the input buffer's size must be one more than the file size and that character must be set to zero to create a NULL terminated string.


hello everyone the problem is solved below is the solution....

#include <stdio.h>

int main(void) {
    unsigned char tmparray[10] = {0xaa,0xbb,0xcc,0xdd,0xee,0x00,0x00,0x00,0x00,0x00};

    int idx = 5;
    int data;
    FILE *fp = fopen("1.txt", "r");
    if (fp == NULL) {
        fprintf(stderr, "Can't read 1.txt");
        return 0;
    }
    while (fscanf(fp, "%*c%*c%x,", &data) == 1) {
        tmparray[idx++] = (unsigned char)data;
    }
    fclose(fp);

    for (idx = 0; idx < sizeof(tmparray) / sizeof(tmparray[0]); idx++) {
        printf("%#x,", tmparray[idx]);
    }
    printf("\n");

    return 0;
}


This is your homework, so I'll not give you the code! :laugh:

The first thing to do is to look at what you are expected to do: read a text based file, and convert it to bytes by converting string based hex values to internal numbers.

Me? I'd write a function which accepted a file name, and returned an array of unsigned chars.
In the function I'd make two passes through the file - i.e. I'd read it twice from beginning to end. Why? Because I'd want to make sure that I was going to read the right amount of data!

So the first pass would count the number of ',' characters. This tells us how many bytes we are going to need in the output array: if the file contains "0x01,0x02,0x03" then we will get a count of 2 commas, and that means we will need 3 bytes to store the data.
I'd then use malloc to allocate a block of memory that large.

The second pass creates each byte value: it reads four characters from the file, and makes sure the first one is '0', the second is 'x', and then converts the other two.
Here, I'd write another function that converts a hex character to it's numeric equivilent:

'0' -> 0
'1' -> 1
...
'9' -> 9
'A' -> 10
'B' -> 11
...
'F' -> 15
'a' -> 10
'b' -> 11
...
'f' -> 15

That's pretty trivial to write!
Then it's simple to build the value:

outputByteValue = (GetHexValue(firstHexChar) << 4) | GetHexValue(secondHexChar);


Put that into the output array, and move on or the next.
At the end of the function, return the array of unsigned chars.

That's pretty simple to do, if you break it down like that because all the individual tasks are easy to do, and you can test each bit in isolation before you move on to the next.
Give it a try!


这篇关于如何从文本文件中读取十六进制值数据并复制到数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆