Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Reading from a file and putting it to uint8_t * in C

Status
Not open for further replies.

raghavkmr

Junior Member level 2
Joined
Nov 26, 2013
Messages
23
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
194
Hi i want to read alphanumeric characters from a text file and put them as hexadecimal characters in a variable
for e.g.
uint8_t *output;
*output = 0xFA ;
it works

but instead i want to read alphanumeric code from a file containing
FA
1B
2E
ED

but when i read values from file and put it into output variable i found that output takes ascii value of first character.
for e.g. 1B
output gets 31 (ascii for 1) instead of 1B what should i do

Code:
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
      int main() {
      uint8_t *output ;
      unsigned char x ;
      char c;
        FILE *fp1, *fp2; 
FILE *f_gets = fopen("key_bank_ssl.key", "r");
if(f_gets==NULL)
	{
		printf("Please point to a valid key file!\n");
		fclose(f_gets);
		
		return 0;
	}
	char str[2];
	
if( fgets (str, 3, f_gets)!=NULL ) {
      /* writing content to stdout */
      puts(str);
      output = (uint8_t *)(str); 
      puts(output);    
      printf("\noutput %s --- %02x --> size --> %lu",str,*output++,sizeof(*output));
   }
   fclose(f_gets);
}
 

Hi,

You need to read a single byte.
Then transform the byte into a string with 4 bytes of length (plus delimiter)

Example:
Lets say the byte value is 65 decimal
(Btw: it's the same as "A" represented as ASCII character, or "41" as hex value, or "0100 0001" as binary value, or "201" as octal value...no difference)
Now you need to generate a string with 4 ASCII characters from it" "0", "x", "4", "1"
"0" and "x" are fix, so you may treat them as a constant value
The "4" is the value of the upper 4 bits of your input byte
The "1" is the vaue of the lower 4 bits of the input byte.

To get the "4" just shift the input byte four bytes right.
"0100 0001" --> "0000 0100" (the resulting value range can be 0..15 in decimal)
Now check if your value is bigger than 9 in decimal:
If 0..9: add the value of an ASCII "0" (which is 48 decimal or 0x30 in hex) to it: "0000 0100" + "0011 0000" = "0011 0100"
If 10..15: add the value of an ASCII "A" (which is 65 decimal or 0x41 in hex) to it: "0000 xxxx" + "0100 0001" = "0011 xxxx"

Similarily with the lower 4 bits:
Mask the lower 4 bits using: AND "0000 1111". "0100 0001" AND "0000 1111" = "0000 0001"
Now check if your value is bigger than 9 in decimal:
If 0..9: add the value of an ASCII "0" (which is 48 decimal or 0x30 in hex) to it: "0000 0001" + "0011 0000" = "0011 0001"
If 10..15: add the value of an ASCII "A" (which is 65 decimal or 0x41 in hex) to it: "0000 xxxx" + "0100 0001" = "0011 xxxx"
That's it.
******
Or simply use the "binary to hex" conversion function.

Klaus
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top