CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2015
    Posts
    48

    Red face 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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: how to write hex formatted string into a binary file

    Quote Originally Posted by Ramees219 View Post
    .. . 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

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    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)

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: how to write hex formatted string into a binary file

    Quote Originally Posted by Ramees219 View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured