1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

Hex format reading from a file

Discussion in 'Embedded' started by Chandra, Mar 1, 2007.

  1. Chandra

    Chandra Guest

    Hi,

    I have a problem related to File Reading. I have a file which contains
    data which is in Hexa format. I have to read that data and push it to
    a function which takes a parameter of type char. I am able to read the
    data from the file, which i am getting as characters.

    For example: Suppose the Hexa value from the File data is ABCDEFGH. In
    hexa form 0xAB is one byte. So, while reading this data A is taken as
    a byte which is not a required format. I am supposed to get AB as a
    byte, which i am not getting.

    Hope you people understand my query.

    with regards,

    Chandra.
     
    Chandra, Mar 1, 2007
    #1
    1. Advertisements

  2. Chandra

    larwe Guest

    Hope you understand:

    a) this is the wrong newsgroup to post a Computer Science 101
    question, and

    b) without specifying anything about the file format, delimiters, or
    even the programming language you're using, you haven't in fact asked
    a question yet; you've merely stated that you have a problem.
     
    larwe, Mar 1, 2007
    #2
    1. Advertisements

  3. 0xGH? Must be the difference between hex and hexa.

    Scott
     
    Not Really Me, Mar 2, 2007
    #3
  4. Chandra

    cs_posting Guest

    This is a very common, basic embedded coding task. Assuming you do it
    by hand and not using some library function:

    Read in two 8-bit chars.
    Convert each to a 4-bit hex nibble (digit)
    (you will need to handle the numbers and the letters seperately, or
    use a lookup table)
    Multiple the first by 0x10 and add it to the second
     
    cs_posting, Mar 2, 2007
    #4
  5. Chandra

    Arlet Guest

    On systems where you can afford to waste 64kB like that, your int is
    probably 32 bits.
     
    Arlet, Mar 2, 2007
    #5
  6. Or, if you can afford a 64K LUT you could do

    int *si = (int *)str;
    char *hex = someOutputBuffer;
    while (*si)
    hex++ = bigLut[*si++];

    assuming str is properly terminated with two nulls.

    Andrew
     
    andrew queisser, Mar 2, 2007
    #6
  7. Nice catch! Ok, version 2.0:

    short *si = (short *)str;
    char *hex = someOutputBuffer;
    while (*si)
    hex++ = bigLut[*si++];
     
    andrew queisser, Mar 2, 2007
    #7
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.