Problem in reading a Hex file and then.....

Discussion in 'C Programming' started by rsk, Jul 20, 2007.

  1. rsk

    rsk Guest

    Hi Friends,

    My requirement is as follows;

    A file is consisting of data in hexadecimal format(i.e a 32 bit data for
    example like "0xdeadbeef").

    I have to read each of such data into my 'c' code
    and i need to assign them to a 32 bit integer array.

    Then i have to part select this array bit by bit like array[31]
    array[30]...........array[0] and do some specific operations.

    I am facing difficulty in assigning this 32 bit hex data to the 32 bit
    array.

    Can you please kindly help me on this.

    With Best Regards,
    RSK...
     
    rsk, Jul 20, 2007
    #1
    1. Advertising

  2. rsk

    santosh Guest

    rsk wrote:

    > Hi Friends,
    >
    > My requirement is as follows;
    >
    > A file is consisting of data in hexadecimal format(i.e a 32 bit
    > data for example like "0xdeadbeef").
    >
    > I have to read each of such data into my 'c' code
    > and i need to assign them to a 32 bit integer array.
    >
    > Then i have to part select this array bit by bit like array[31]
    > array[30]...........array[0] and do some specific operations.
    >
    > I am facing difficulty in assigning this 32 bit hex data to the
    > 32 bit array.
    >
    > Can you please kindly help me on this.


    If you know the size of your file you can declare the exact size
    of the array during compile-time. Otherwise you need to resize
    the array on-the-fly as you program reads more of the file.

    In any case, make the type of the array as long, and open the
    file with fopen in binary read mode. Then use fread in a loop to
    fill the array of longs. Stop when fread returns a less than
    expected read count. You can distinguish between end-of-file and
    a read error by testing with feof or ferror.

    The do whatever you want with the array. If it's dynamically
    allocated don't forget to free it when you're done. Similarly
    close the file after reading it.

    In C, the type long is guaranteed to be 32 bits. The type int may
    or may not be. If you want a type exactly of 32 bits use
    int32_t, though beware that it's improperly supported C99. I'd
    just use long.

    For any more help, post the code you've written to do what you
    want and point out the exact place where you're having
    difficulty. DO NOT RETYPE CODE. CUT & PASTE IT.
     
    santosh, Jul 20, 2007
    #2
    1. Advertising

  3. rsk

    rsk Guest

    Thank you so much santosh.So kind of you...


    With Best Regards,
    Ravi
     
    rsk, Jul 20, 2007
    #3
  4. "rsk" <> writes:
    > My requirement is as follows;
    >
    > A file is consisting of data in hexadecimal format(i.e a 32 bit data for
    > example like "0xdeadbeef").

    [snip]

    Let's be sure we really understand your data format, since I've seen a
    lot of confusion on this point.

    Does the file actually contain data represented in hexadecimal? In
    other words, for the value 3735928559 (0xdeadbeef), does the file
    actually contain the (probably ASCII) characters 'd', 'e', 'a', 'd',
    'b', 'e', 'e', 'f'? (And if so, are they preceded by the characters
    '0', 'x'?) Or are 32-bit numbers stored in the file in a 32-bit
    binary format?

    We've seen people here refer to raw binary as "hexadecimal", probably
    because that's how binary files are often *viewed*. But in fact,
    binary ahd hexadecimal are very different formats.

    If the values are stored in hex, how are the values separated? Does
    each hexadecimal literal occur on a line by itself (terminated with
    '\n')? Or are there multiple literals per line, and are they
    separated by spaces, tabs, commas, arbitrary whitespace, or something
    else?

    --
    Keith Thompson (The_Other_Keith) <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."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jul 20, 2007
    #4
  5. rsk

    santosh Guest

    Thad Smith wrote:
    > santosh wrote:
    >> rsk wrote:


    [ ... ]

    >> If you want a type exactly of 32 bits use int32_t, though beware that
    >> it's improperly supported C99.

    >
    > Why do you say that int32_t is improperly supported [in] C99?


    I should have written that better. What I meant was C99 itself was
    improperly supported by compiler vendors. Hence if the OP decides to use
    C99 types, he might encounter compilation problems when moving his code to
    an implementation that partially or fully lacks support for C99, like MS
    Visual C++ and many compilers targetting embedded platforms.
     
    santosh, Jul 21, 2007
    #5
  6. rsk

    rsk Guest

    No they don't precede by the charecters like '0', 'x',Actually the .txt
    file contains the commands in hex format as
    deadbeef
    00000001
    00000002
    00000003
    00000004
    00000005
    00000006
    00000007
    00000008
    00000009
    0000000a
    0000000b
    0000000c
    0000000d
    0000000e
    0000000f
    00000010
    00000011
    00000012
    00000013
    00000014
     
    rsk, Jul 21, 2007
    #6
  7. rsk

    Thad Smith Guest

    santosh wrote:
    > rsk wrote:


    > In C, the type long is guaranteed to be 32 bits.


    Variables of type long are guaranteed to have /at least/ 32 bits. They
    may have more.

    > The type int may
    > or may not be. If you want a type exactly of 32 bits use
    > int32_t, though beware that it's improperly supported C99.


    Why do you say that int32_t is improperly supported [in] C99?

    > For any more help, post the code you've written to do what you
    > want and point out the exact place where you're having
    > difficulty. DO NOT RETYPE CODE. CUT & PASTE IT.


    Good advice.

    --
    Thad
     
    Thad Smith, Jul 21, 2007
    #7
  8. rsk

    rsk Guest

    Santosh,i have a doubt when the file is containing data in hex for mat can
    i open the file with fopen in binary mode?
     
    rsk, Jul 21, 2007
    #8
  9. "Thad Smith" <> wrote in message
    news:46a1a10f$0$79255$...
    > santosh wrote:
    >> rsk wrote:

    >
    >> In C, the type long is guaranteed to be 32 bits.

    >
    > Variables of type long are guaranteed to have /at least/ 32 bits. They
    > may have more.
    >
    >> The type int may
    >> or may not be. If you want a type exactly of 32 bits use
    >> int32_t, though beware that it's improperly supported C99.

    >
    > Why do you say that int32_t is improperly supported [in] C99?
    >

    int32_t may exist or it may not. Your compiler may be an old compiler or it
    may be a compiler with a few C99 features. In future your code might even
    have to be run through a conforming C99 compiler.
    This causes huge problems, and the only answer is to be extremely
    conservative.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Jul 21, 2007
    #9
  10. rsk

    rsk Guest

    Hi friends,

    the command.txt contains a sequence of hex numbers
    like

    adcdef45
    12435fde
     
    rsk, Jul 21, 2007
    #10
  11. rsk

    pete Guest

    rsk wrote:
    >
    > Hi Friends,
    >
    > My requirement is as follows;
    >
    > A file is consisting of data in hexadecimal format(i.e a 32 bit data for
    > example like "0xdeadbeef").
    >
    > I have to read each of such data into my 'c' code
    > and i need to assign them to a 32 bit integer array.
    >
    > Then i have to part select this array bit by bit like array[31]
    > array[30]...........array[0] and do some specific operations.
    >
    > I am facing difficulty in assigning this 32 bit hex data to the 32 bit
    > array.
    >
    > Can you please kindly help me on this.


    /* BEGIN new.c */

    #include <stdio.h>

    #define READ_UBIT(U, N) ((U) >> (N) & 1u)
    #define BITS 32

    int main(void)
    {
    int array[BITS];
    long unsigned hex = 0xdeadbeef;
    unsigned index;

    for (index = 0; index != BITS; ++index) {
    array[index] = READ_UBIT(hex, index);
    }
    puts("/* BEGIN new.c output */\n");
    printf("hex is 0x%lx\n\n", hex);
    for (index = 0; index != BITS; ++index) {
    printf("array[%2u] is %d\n", index, array[index]);
    }
    puts("\n/* END new.c output */");
    return 0;
    }

    /* END new.c */


    --
    pete
     
    pete, Jul 21, 2007
    #11
  12. rsk

    Thad Smith Guest

    Malcolm McLean wrote:

    > int32_t may exist or it may not. Your compiler may be an old compiler or
    > it may be a compiler with a few C99 features. In future your code might
    > even have to be run through a conforming C99 compiler.
    > This causes huge problems, and the only answer is to be extremely
    > conservative.


    Even on a C99 conforming compiler int32_t isn't required to exist. For
    portable code, I can use variables that are at least the required length
    and not depend on the exact length.

    --
    Thad
     
    Thad Smith, Jul 21, 2007
    #12
  13. "rsk" <> writes:
    > No they don't precede by the charecters like '0', 'x',Actually the .txt
    > file contains the commands in hex format as
    > deadbeef
    > 00000001
    > 00000002

    [snip]
    > 00000013
    > 00000014


    *Please* provide some context when you post a followup. If you're
    using "talkaboutprogramming.com" as an interface to this newsgroup,
    and if it doesn't do this for you automatically, please find another
    way to post; even Google Groups would be better. Don't assume that
    your readers can easily see the article to which you're replying (but
    trim any quoted material that isn't relevant to your response,
    particularly signatures). See most of the posts in this newsgroup for
    examples.

    The above is a response my question about the actual format of your
    input file. So if I understand you correctly, you've got a text file
    where each line contains an 8-digit hexadecimal number. If that's
    correct (and it's something you should have told us to begin with),
    then reading it is fairly straightforward. I'm not going to write the
    code for you, but you can use fgets() to read a line, then something
    like strtoul to convert each line to a number. You should also think
    about error handling (what to do if an input line is too long, or if
    it doesn't contain a valid hex number).

    --
    Keith Thompson (The_Other_Keith) <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."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jul 22, 2007
    #13
  14. rsk

    santosh Guest

    rsk wrote:

    > Santosh,i have a doubt when the file is containing data in hex for mat can
    > i open the file with fopen in binary mode?


    Usually binary mode is used for non-text files. If your file is in text
    format, i.e., you can use EDIT.COM to read it, then the method Keith
    Thompson suggested is more straightforward. Read in each line using fgets
    and convert the line into a numeric value using strtoul or similar. If your
    data is strictly regular you may also use fscanf to read and convert is one
    stroke, but that's a slippery slope.
     
    santosh, Jul 22, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,388
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    525
    Juha Autero
    Aug 19, 2003
  3. Colin Green

    Stripping Hex and then ASCII

    Colin Green, Jul 7, 2004, in forum: C Programming
    Replies:
    6
    Views:
    405
    Emmanuel Delahaye
    Jul 7, 2004
  4. Replies:
    0
    Views:
    336
  5. Replies:
    4
    Views:
    561
    cwdjrxyz
    Jan 17, 2006
Loading...

Share This Page