How to read a Hexadecimal file ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vijay

    How to read a Hexadecimal file ?

    Hi ,

    I am doing a small project in c.

    I have a Hexadecimal file and want to convert into ascii value.
    (i.e., Hexadecimal to Ascii conversion from a file).

    Could anyone help me?

    Thanks in adv.
    Vijay.

  • Richard Heathfield

    #2
    Re: How to read a Hexadecimal file ?

    Vijay said:
    Hi ,
    >
    I am doing a small project in c.
    >
    I have a Hexadecimal file and want to convert into ascii value.
    (i.e., Hexadecimal to Ascii conversion from a file).
    "Hexadecima l file" doesn't mean a lot, but I'm guessing - and it's just a
    guess - that you have a file containing arbitrary data and you would like
    to represent that file in a readable way - what we call a "hex dump" (as if
    hex were readable).

    This is simple enough, but can easily be made more complicated if you don't
    think it has enough features. To simplify the explanation, I have assumed
    that there are 8 bits in a byte. Beware! This is not necessarily the case.
    But it is probably the case on the system you are using.

    Define a counter, and set it to value 0.
    Define an array of char, and initialise it to "0123456789ABCD EF".
    Define an int, which I will call ch.

    Using ch, capture each value in turn from the file. For as long as that
    capture is successful, do the following:

    1) increment your counter
    2) display a space character
    3) display the ((ch & 0xF0) >4)th character of your array
    4) display the (ch & 0xF)th character of your array
    5) if the counter is equal to your maximum desired page-width:
    5a) display a newline character
    5b) set your counter to 0
    6) back to the top of the loop

    Once the capture fails, test (using ferror()) to see whether it actually
    failed, or whether it simply hit the end of the file. If it is the end of
    the file:
    if your counter is not equal to 0, display a newline character
    otherwise:
    display an appropriate error message.

    Finally, terminate the program.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at above domain (but drop the www, obviously)

    Comment

    • J. J. Farrell

      #3
      Re: How to read a Hexadecimal file ?


      Vijay wrote:
      >
      I am doing a small project in c.
      >
      I have a Hexadecimal file and want to convert into ascii value.
      (i.e., Hexadecimal to Ascii conversion from a file).
      "Hexadecima l file" is not well-defined. You need to tell us exactly
      what you mean by that term. We could guess, but there are several
      sensible possible meanings, so guessing is likely to cause confusion.

      You also need to tell us how you want it to be converted. What do we
      read out of the "hexadecima l file", and how do we convert it to ASCII?

      If you specify precisely what you want to do, we can help you write
      some C code to do it.

      Comment

      • Richard

        #4
        Re: How to read a Hexadecimal file ?

        "Vijay" <Vijayakumar.PC @gmail.comwrite s:
        Hi,
        >
        Sample Hexadecimal data is given below.
        Hi Vijay : go and do some reading. That is NOT hexadeciaml data
        below. It is just data represented in ascii. Not the same at all.

        I think that another poster (Heathfield I think) mentioned that you
        might be looking for a program to display any program as a hex dump. Use
        google there are loads of resources for this.

        good luck!

        r.
        >
        ‹ ÿCF0000.DAT
        ìÏ;ŽÂ0Ðà »lÚ$”2"0m DÆ’h€ÔHh*60 Ã…,`4Ã…lš}
        V1+¡¢36ŠøˆÜÓøûì ûì¤H%\g#À Fz*QA˜-׺
        ó‹Ä`Ÿ— ó#ÐÜ·{A±î (äçë:L¬þŠ’‡¸Ay}fâ⠀¡Ãж
        ö“¼0UzXÕE ‚Åì
        Œê›qá'ÿ ­K0®žt£è ·Vm{–éAy ºÝ4­Ÿ\ð U~C¦Í»nÃ¥@ ì"šèM#/ûìŽuЍù¯§:nÓ5iË †Ë†Ë†Ë†Ë†Ë†Ë†Ë† ˆˆˆˆˆˆˆà žÂ¹
        >
        Could u give some codes in c to decode.. (i.e, in ascii format).
        >
        J. J. Farrell wrote:
        >
        >Vijay wrote:
        >
        I am doing a small project in c.
        >
        I have a Hexadecimal file and want to convert into ascii value.
        (i.e., Hexadecimal to Ascii conversion from a file).
        >>
        >"Hexadecima l file" is not well-defined. You need to tell us exactly
        >what you mean by that term. We could guess, but there are several
        >sensible possible meanings, so guessing is likely to cause confusion.
        >>
        >You also need to tell us how you want it to be converted. What do we
        >read out of the "hexadecima l file", and how do we convert it to ASCII?
        >>
        >If you specify precisely what you want to do, we can help you write
        >some C code to do it.
        --
        Lint early. Lint often.

        Comment

        • Keith Thompson

          #5
          Re: How to read a Hexadecimal file ?

          Richard <rgrdev@gmail.c omwrites:
          "Vijay" <Vijayakumar.PC @gmail.comwrite s:
          >Hi,
          >>
          >Sample Hexadecimal data is given below.
          >
          Hi Vijay : go and do some reading. That is NOT hexadeciaml data
          below. It is just data represented in ascii. Not the same at all.
          No, it's not data represented in ASCII. It appears to be just a
          binary file, neither ASCII nor hexadecimal.

          To the original poster: You need to understand a few things. All
          files are "binary", in the sense that they're made of bits; on most
          systems you're likely to use, they're composed of 8-bit bytes. (The C
          standard requires a byte to be *at least* 8 bits, but you're not
          likely to run into a system with bytes larger than 8 bits unless you
          work with digital signal processors, and I don't think they have file
          systems.)

          A given file may or may not contain some specific kind of data. If
          the bytes are all ASCII characters (remember, ASCII is a 7-bit
          character set), it's (presumably) an ASCII file. There are 8-bit
          character sets that are extensions of ASCII. There are also character
          sets not based on ASCII; EBCDIC is the most common, but you're
          unlikely to run into that unless you work with IBM mainframes.

          Hexadecimal is a way of representing numbers as text, using the digits
          '0' to '9' and the letters 'a' to 'f' or 'A' to 'F'. You could have a
          "hexadecima l file" that contains just those (ASCII) characters
          (possibly with spaces and newlines added for formatting) -- but that's
          not what you showed us. Hexadecimal can be a convenient way of
          displaying binary data, but it's a *display* format; the binary file
          is not hexadecimal.

          You said you want to decode it to ASCII format. There are multitudes
          of ways you could do that, generating a sequence of ASCII characters
          that specify the content of the binary file. (The ASCII
          representation is likely to be larger than the original binary file;
          if you represent it as hexadecimal digits, it will be twice the size,
          one 8-bit character for each 4-digit hexadecimal digit.)

          I could guess what you're trying to do, but I'm not going to. You
          need to define the problem. You have a binary file as input (BTW,
          please don't post binary files here), and you want some kind of ASCII
          output, but you haven't said *how* the ASCII should correspond to the
          binary data.

          But even if you define the problem, you may still be asking in the
          wrong place. You mention that you want to do this in C, but you
          haven't shown any indication that you've tried to do it yourself.

          First you need to define the problem. Then you need to make some
          effort to solve it yourself. If you have some partially working code
          and you can't figure out why it's not working, post it here and we can
          offer advice. We won't do it for you.

          Unless, of course, this is a homework assignment; in that case, just
          give us your instructor's e-mail address so we can submit our
          solutions directly. It's up to you whether we mention your name or
          not.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • Richard

            #6
            Re: How to read a Hexadecimal file ?

            Keith Thompson <kst-u@mib.orgwrites :
            Richard <rgrdev@gmail.c omwrites:
            >"Vijay" <Vijayakumar.PC @gmail.comwrite s:
            >>Hi,
            >>>
            >>Sample Hexadecimal data is given below.
            >>
            >Hi Vijay : go and do some reading. That is NOT hexadeciaml data
            >below. It is just data represented in ascii. Not the same at all.
            >
            No, it's not data represented in ASCII. It appears to be just a
            binary file, neither ASCII nor hexadecimal.
            Converted by my newsreader. But yes, I saw he just pasted raw data
            in. For sure a homework assignment so hence the "google is that way"
            followup and the mention of him needing to hex dump.

            Comment

            • Keith Thompson

              #7
              Re: How to read a Hexadecimal file ?

              Richard <rgrdev@gmail.c omwrites:
              Keith Thompson <kst-u@mib.orgwrites :
              >Richard <rgrdev@gmail.c omwrites:
              >>"Vijay" <Vijayakumar.PC @gmail.comwrite s:
              >>>Sample Hexadecimal data is given below.
              >>>
              >>Hi Vijay : go and do some reading. That is NOT hexadeciaml data
              >>below. It is just data represented in ascii. Not the same at all.
              >>
              >No, it's not data represented in ASCII. It appears to be just a
              >binary file, neither ASCII nor hexadecimal.
              >
              Converted by my newsreader. But yes, I saw he just pasted raw data
              in. For sure a homework assignment so hence the "google is that way"
              followup and the mention of him needing to hex dump.
              Ah. My newsreader displayed it as a bunch of garbage characters
              (presumably as ISO-8859-1), which reinforces the idea that posting
              binary data is a bad idea. (There are methods for posting binary
              files as attachments, but that's inappropriate in a discussion group
              like this one.)

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • J. J. Farrell

                #8
                Re: How to read a Hexadecimal file ?

                Vijay wrote:
                >
                Top-posting corrected.
                J. J. Farrell wrote:
                >
                Vijay wrote:
                >
                I am doing a small project in c.
                >
                I have a Hexadecimal file and want to convert into ascii value.
                (i.e., Hexadecimal to Ascii conversion from a file).
                "Hexadecima l file" is not well-defined. You need to tell us exactly
                what you mean by that term. We could guess, but there are several
                sensible possible meanings, so guessing is likely to cause confusion.

                You also need to tell us how you want it to be converted. What do we
                read out of the "hexadecima l file", and how do we convert it to ASCII?

                If you specify precisely what you want to do, we can help you write
                some C code to do it.
                >
                Sample Hexadecimal data is given below.
                >
                ‹ ÿCF0000.DAT
                ìÏ;ŽÂ0ÐûlÚ$ "2"0mDƒh€ÔHh*6 0Å,`4Ålš}
                ...
                >
                Could u give some codes in c to decode.. (i.e, in ascii format).
                You appear to have a file of what is usually referred to as "binary"
                data. The file contains a number of bytes. Each byte contains a certain
                number of bits, most likely 8 (but that depends on the computer you are
                working on). If we assume 8 bits and consider these bytes as unsigned
                numbers, each byte in the file contains a number in the range 0 to 255.

                You need to read each byte in, then apply whatever conversion algorithm
                you require to turn the value in that byte into ASCII, then write out
                the result.

                For example, your algorithm might be

                if the value of the input byte is 3
                write out the ASCII character 'x'
                else if the value of the input byte is 179
                write out the ASCII character 'y'
                else
                write out the ASCII character 'z'
                fi

                Another possibility is that for each byte you read in, you would write
                out three ASCII digits giving the value of the byte in decimal. There's
                a very large number of other possible ways of converting the file to
                ASCII.

                It's not likely that either of these is the algorithm you want. You
                need to understand what conversion algorithm you want to implement, and
                tell us what it is. Then we'll be able to guide you in writing a C
                program to implement it.

                Comment

                • Joe Wright

                  #9
                  Re: How to read a Hexadecimal file ?

                  Keith Thompson wrote:
                  Richard <rgrdev@gmail.c omwrites:
                  >Keith Thompson <kst-u@mib.orgwrites :
                  >>Richard <rgrdev@gmail.c omwrites:
                  >>>"Vijay" <Vijayakumar.PC @gmail.comwrite s:
                  >>>>Sample Hexadecimal data is given below.
                  >>>Hi Vijay : go and do some reading. That is NOT hexadeciaml data
                  >>>below. It is just data represented in ascii. Not the same at all.
                  >>No, it's not data represented in ASCII. It appears to be just a
                  >>binary file, neither ASCII nor hexadecimal.
                  >Converted by my newsreader. But yes, I saw he just pasted raw data
                  >in. For sure a homework assignment so hence the "google is that way"
                  >followup and the mention of him needing to hex dump.
                  >
                  Ah. My newsreader displayed it as a bunch of garbage characters
                  (presumably as ISO-8859-1), which reinforces the idea that posting
                  binary data is a bad idea. (There are methods for posting binary
                  files as attachments, but that's inappropriate in a discussion group
                  like this one.)
                  >
                  Not to disagree about anything, this is a convenient place to jump in..

                  1. All files are binary!
                  2. All files are product of the creator of the file!
                  3. We cannot know, by inspection only, how to interpret the file!

                  There is general agreement among some of us the format of the 'ASCII
                  text file'. "Zero or more 'lines' of zero or more ASCII characters, each
                  line terminated with a newline character." This is the only 'text' file.

                  --
                  Joe Wright
                  "Everything should be made as simple as possible, but not simpler."
                  --- Albert Einstein ---

                  Comment

                  Working...