1

I've used:

sprintf(hex, "%02x", (unsigned int) buffer[0] & 0xff);

to get hexadecimal representation of binary file. After that I save this representation to txt file.

Now I' d like to execute inverse operation. How can I create binary file from my hex data?

2
  • While Timothy's answer answers your question - I must ask, why are you doing this? If you're writing to a binary file (note: not ASCII but binary) there's no reason to be writing hex. You should be reading and writing binary which is faster and smaller, and will require less code (always remember, less code is better code). Commented Feb 19, 2012 at 23:52
  • In a first program, I create a txt file with hex representation of my binary file, for example some pdf file. And now in the second program I' d like to read data from txt file and create from it copy of my binary file. Maybe it is a little complicated, but I need something like this. Commented Feb 19, 2012 at 23:58

1 Answer 1

4

sscanf() will let you do the inverse of sprintf()

int output;  
int we_read_an_integer = sscanf(inputString, "%02x", &output);

Repeat as necessary.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.