|
-
May 21st, 2016, 01:40 PM
#1
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
-
May 22nd, 2016, 03:37 AM
#2
Re: how to write hex formatted string into a binary file
 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"?
Victor Nijegorodov
-
May 22nd, 2016, 05:05 AM
#3
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.
Last edited by 2kaud; May 22nd, 2016 at 06:03 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 25th, 2016, 07:51 AM
#4
Re: how to write hex formatted string into a binary file
 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.
Best regards,
Igor
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|