Thread: hex to ascii

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    12

    hex to ascii

    hello, im new to programing and im having problems. they are giving me a file with characters like this:
    53
    57
    6a
    and to start working i need to transform them into characters in ascii (like v in the first case), we cant use scanf we need to use fread. i tried
    fread(tablecod, sizeof(unsigned char), 1, fptdc );
    changing the type on sizof and tablecod but the best i can do is read the first chr and save it (in the first case 5 for example)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,926
    Is this a text file, e.g., is there "53" as in two bytes in a row in the file (possibly followed by a new line), or is this a binary file, e.g., a single byte 0x53 in the file (and presumably no new line, but rather the different lines were just for ease of illustration of the different possible bytes)?

    The phrase "in the first case 5 for example" makes me think it is the former, but it is important to be sure because you apparently are supposed to use fread, which is more commonly used with binary files.
    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

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Are you trying to read a text file with fread? That's what it sounds like and it's not exactly the most practical thing. For one thing, if fread succeeds, you would still need to turn '5' (the character 5) into a normal number before it can be used like an ascii character.

    You can do that, just subtract '0' from the character.

    I just think it's important you know your class isn't being that practical.

  4. #4
    Registered User
    Join Date
    May 2020
    Posts
    12
    Its a text file with 53, i can use fgetc and fgetchar too (and fputc fputchar fwrite), its a codification problem that involves dealing with a binary file later so maybe im supose to use readf at that point

  5. #5
    Registered User
    Join Date
    May 2020
    Posts
    12
    i can use fgetc and fgetchar too, maybe thats my mistake, the problem involves dealing with a binary file later so maybe im supose to use readf at that point

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Quote Originally Posted by stanley1234 View Post
    i can use fgetc and fgetchar too, maybe thats my mistake, the problem involves dealing with a binary file later so maybe im supose to use readf at that point
    That sounds more correct. What matters with files is how you open them. There are different functions that make reading text files easier, like fgetc, that you know, and there are different functions that make reading binary files easier, like fread, that you know.

    Use the right functions for the right file type.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Quote Originally Posted by stanley1234 View Post
    Its a text file with 53
    The program will need to convert the text "53" and other bytes written out like "6a" into numbers. When you have the numbers in number format, then you can use them as characters.

    It's a simple formula. For example 6a in hex is 6 * 16 + 10 = 96 in decimal.

    You just have to figure out how to make 'a' into 10, 'b' into 11, ... 'f' into 15.

  8. #8
    Registered User
    Join Date
    May 2020
    Posts
    12
    Thanks a lot, i will try do that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASCII character with ASCII value 0 and 32
    By hitesh_best in forum C Programming
    Replies: 4
    Last Post: 07-24-2007, 09:45 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Ascii to hex
    By bikr692002 in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2006, 03:53 PM
  4. ascii
    By tetraflare in forum C++ Programming
    Replies: 1
    Last Post: 12-08-2002, 05:00 AM
  5. ascii to hex
    By ripper079 in forum C++ Programming
    Replies: 4
    Last Post: 04-26-2002, 04:26 PM

Tags for this Thread