wRITING DATA IN FILE

i have an issue regarding Writing data int binary file.
Actually i want the data to be written as Hex.
I have my data in an char buffer and i am writing it into binary file using fwrite function. but instead of writing directly it is converting it to ASCII and then converting it to hex file. That is f is converted into ASCII 70 and then its hex 46 is written in file.

/* Writing Buffer into BIN */
rewind(bbcb);
fseek(bbcb,2,SEEK_SET);
fwrite(test,sizeof(char),22,bbcb);
rewind(bbcb);

BBCB.BIN FILE IS OF 24 BYTES AND PTR IS TO WRITTEN FROM 3RD BYTE OF BBCB.

data stored in ptr is:
ptr[1]=C7
ptr[2]=07
ptr[3]=03
ptr[4]=01
ptr[5]=01
ptr[6]=12
ptr[7]=34
ptr[8]=56
ptr[9]=78
ptr[10]=AB
ptr[11]=CD
ptr[12]=EF
ptr[13]=AB
ptr[14]=FF
ptr[15]=FF
ptr[16]=FF
ptr[17]=FF
ptr[18]=FF
ptr[19]=FF
ptr[20]=FF
ptr[21]=FF
ptr[22]=FF
i want the data to be written as Hex.
Hexadecimal is just a representation. Viewing a byte sequence as hex doesn't change it in any way.

That is f is converted into ASCII 70 and then its hex 46 is written in file.
You'll find that 70 decimal is 46 hexadecimal.
Here's my code:
// CRCCAL.cpp : Defines the entry point for the console application.
#include<io.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

void main()
{
/* INITIALIZATION of VARIABLES */
unsigned char * pbuff;
unsigned short wCRC16;
long int usize;
char str[80],*ptr[22];
unsigned char test[2];
static char str2[30][10];
int var1,var2,var3,var4=1,count,value;
FILE *param,*bbcb,*pbaddr,*bootldr;
clrscr();

/* Opening Files*/
param=fopen("C:\\TC\\BIN\\PARAM.cfg", "rb+");
bbcb=fopen("C:\\TC\\BIN\\BBCB.bin","wb+");

/* Code to extract values from Param.cfg file */
for(var2=1;var2<12;var2++) // for var2 start
{
fgets(str,80,param); //reading line from file PARAM.CFG into STR
printf("%s\n",str);
var1=0,var3=0;
for(var1=0;var1<80;var1++) // Loop to Read 80 characters a line. Var1 reads whole line
{ // Our logic is to extract values from file after first '=' in the file
if(str[var1]=='=') // Extract Values after'=' in a line
{
value=var1+1; // Value pointing to next character after '='
do
{
if(str[value]==' ')// This take care of blank spaces after '=
{
value++;
continue;
}
else // Extract values here
{
if((str[value]=='0') && ((str[value+1]=='x')||(str[value+1]=='X')))//Ignore '0x' in begining as in case of SERIAL NUMBER and RANDOM NUMBER
value=value+2;
else
{
str2[var4][var3]=str[value+var3]; // Store values in STR2
var3++;
}
}
}while(str[value+var3]!='\n');
break;
}
}
var4++;
}

/* Extracted values stored in STR2 */
/* Print Extracted vales Here */
for(var1=1;var1<12;var1++)
printf("String Extracted : %s\n",str2[var1]);

/* Code for assigning extracted values into buffer(ptr)*/
for(var1=1;var1<23;var1++)
ptr[var1]=(char *)malloc(1); // Assigning memory to ptr

for(var1=1;var1<=5;var1++)
sprintf(ptr[var1],"%x",(atoi(str2[var1]))); /* Converting values to HEXADECIMAL*/

/* Code to split '12345678'& 'ABCDEFAB' into '12' '34' '56' '78'& 'AB' 'CD' 'EF' 'AB' */
var1=var4=6;
while(var1<=7)
{
var2=0,count=0;
while(var2<=8)
{
ptr[var4][count]=str2[var1][var2];
count++; // Count keeps track of values entered
if(count==2) // When two values are extracted count is reset and Var3 increases
{
ptr[var4][2]=NULL;
var4++;
count=0;
}
var2++;
}
var1++;
}
/* Appending 'FFFFFFFFFFFFFFFFFF' (9 Bytes for padding to be usede in BBCB.BIN */
for(var1=14;var1<23;var1++)
{
ptr[var1][0]='F';
ptr[var1][1]='F';
ptr[var1][2]=NULL;
}

/* Printing Whole PTR buffer */
for(var1=1;var1<23;++var1)
{
puts(ptr[var1]);
printf("\n");
}
/* Writing Buffer into BBCB.BIN */
fseek(bbcb,14,SEEK_SET);
//test[0]= 0x12;
//test[1]= 0xff;
//printf("%02x %02x\n", test[0], test[1]);
fwrite(ptr,sizeof(char),22,bbcb);
rewind(bbcb);
}





It is used to etract details from param.cfg file. and put it into bbcb at 2-24 byte(total 22).

Param file has data:-
MANUFACTURER_CODE=199
HARDWARE_CODE=7
LOADER_MAJOR=3
LOADER_MINOR=1
LOADER_TYPE=1
serial_number= 0x12345678
random_number= 0xABCDEFAB
FLASH_BSTRAP_ADDR= 0X00000
FLASH_BLOADER_ADDR= 0X60000
FLASH_BSTRAP_SIZE=0 X60000
FLASH_BLOADER_SIZE=0 XC0000

it is extracting values after the '=' sign in each line and storing it into ptr then writing into bbcb.

I want bbcb should have values:
00 00 C7 07 03 01 01 12 34 56 78 AB CD EF AB FF FF FF FF FF FF FF FF FF
TOTAL 24 bytes.
please help as i am not getting the exact data.
If i use this it is printing the exact value



fseek(bbcb,8,SEEK_SET);
test[0]= 0x32;
test[1]= 0x65;
printf("%02x %02x\n", test[0], test[1]);
fwrite(test,sizeof(char),2,bbcb);
rewind(bbcb);
Print format %02x means print the value as hex with a field width of 2. If you want to see it displayed as a character, use print format %c.
No i want to write into file as hex format
I really don't know what you want.

I've tried to explain that hex is just a way of displaying a number, and that your file is written correctly. As characters are represented as numbers, that value can be displayed as a character, a decimal number or a hexadecimal number.
Topic archived. No new replies allowed.