Thread: Convert txt file which contains HEX text to binary equivalent

  1. #1
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200

    Lightbulb Convert txt file which contains HEX text to binary equivalent

    Hello,
    I have text file with strings like 0xAF. I would like to store does hexadecimal valus as binary to simply save spce.
    0xAF stored as text recuires 2 bytes, but saved as binary only one byte which is 1010 1110.

    Shall I convert every char to binary representation, then put into buffer and save to file usng fwrite? Any idea will be highly apreceated

    Thanks!

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    1010 1110 as a string is 8 (or 9) bytes. What am I missing here?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MartinR
    Shall I convert every char to binary representation, then put into buffer and save to file usng fwrite?
    It seems to me that it is not merely a matter of converting "every char to binary representation", but rather you have to parse the input for the numbers in hexadecimal notation such that you group the input into pairs of characters that are then computed to form the value of say, an unsigned char. If the input really has the "0x" prefix, you would have to remove that when parsing. You can then write each of these unsigned char values with fwrite, and it would be the binary representation that you seek.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Code:
    #include <stdlib.h>
    
    
    long int strtol(const char *nptr, char **endptr, int base);
    The strtol() function converts the initial part of the string in nptr to a long integer value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 01-21-2014, 10:44 PM
  2. convert a char to its binary equivalent?
    By dre in forum C Programming
    Replies: 10
    Last Post: 08-25-2009, 05:52 PM
  3. read txt file as binary then convert to text
    By 911help in forum C Programming
    Replies: 2
    Last Post: 01-04-2008, 06:29 AM
  4. Convert a text file to a binary file
    By Cyber Kitten in forum C Programming
    Replies: 16
    Last Post: 02-04-2002, 08:53 AM

Tags for this Thread