how to write hex formatted string into a binary file
hi everyone i need help .
i have a function that return a sequence of strings ex:-
Code:
[5]2A3D35E89C65379362FF
[0]AA549860FDEED365895634
[9]4512EE67AD9860
[2]528465362698AEFF56
[]85FFE6A3C9524632
SO you can see each string start with bracket [] in side a one digit value or empty bracket then nicely arranged string hex values . so i need this string value converted into hex and stored into a binary file and i need to read them back as it is how can i do this . i goggled it but i didn't under stand .. so i all kind of help appreciated
Re: how to write hex formatted string into a binary file
Quote:
Originally Posted by
Ramees219
.. . so i need this string value converted into hex and stored into a binary file and i need to read them back as it is how can i do this . i goggled it but i didn't under stand .. so i all kind of help appreciated
And what exactly was that you "didn't under stand"?
Re: how to write hex formatted string into a binary file
Consider
Code:
#include <cstdio>
#include <string>
using namespace std;
int main()
{
const string hex = "2A3D35E89C65379362FF";
auto dig = [](unsigned char ch) -> unsigned char {return ch - ('0' + 7 * (ch >= 'A'));};
for (size_t i = 0; i < hex.size() - 1; i += 2) {
unsigned char bin = (dig(hex[i]) << 4) + dig(hex[i + 1]);
printf("%X\n", bin);
}
}
Rather than display bin to the console, just write it to the required file. Note - no error checking and hex chars need to be in upper-case.
Re: how to write hex formatted string into a binary file
Quote:
Originally Posted by
Ramees219
hi everyone i need help .
i have a function that return a sequence of strings ex:-
Code:
[5]2A3D35E89C65379362FF
[0]AA549860FDEED365895634
[9]4512EE67AD9860
[2]528465362698AEFF56
[]85FFE6A3C9524632
SO you can see each string start with bracket [] in side a one digit value or empty bracket then nicely arranged string hex values . so i need this string value converted into hex and stored into a binary file and i need to read them back as it is how can i do this . i goggled it but i didn't under stand .. so i all kind of help appreciated
Nicely arranged, you say, while I doubt those are.
- representation: What are those strings represent? Byte chains? Two-byte integers? 32-byte integers?
- read/write order: In case those are longer than one byte values, is that some particular endianness is implied?
- reading back: Are you expected to read bytes/words/numbers back as those originally are? In case you are, you're in trouble. As you need separate the bytes per line in the set of lines of unequal length, which means you need to invent some separator sign reliably distinguished in the byte stream. The key word is reliably.