Hexa reading from a file

Discussion in 'C++' 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. Advertising

  2. Chandra

    mlimber Guest

    On Mar 1, 10:58 am, "Chandra" <> wrote:
    > 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.


    First, the common abbreviation for hexadecimal is hex (cf. std::hex),
    not hexa. Second, G and H are not valid hexadecimal values.

    Now, let's say have a std::string with "ABCDEF" and you want to divide
    it up into 8-bit bytes. You could do something like this (untested):

    const string str = "ABCDEF";
    const unsigned i = ConvertString<unsigned>( "0x" + str.substr( 0,
    2 ) ); // Parameter yields "0xAB"
    assert( i == 0xAB );

    where

    template<typename T>
    T ConvertString( const std::string& s )
    {
    std::istringstream iss( s );
    T t;
    if( !(iss >> std::resetiosflags( std::ios::basefield ) >> t >>
    std::ws).eof() )
    throw std::invalid_argument( "Unable to convert \"" + s +
    "\"" );
    return t;
    }

    See also http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-39.2
    and the docs on Boost's lexical_cast (http://boost.org/libs/conversion/
    lexical_cast.htm).

    Now, assuming I typed everything correctly, you just need to put it in
    a loop to extract the rest of the values. Also note that the
    conversion function throws an exception if the input is invalid, so
    you'll need to handle that.

    Cheers! --M
    mlimber, Mar 1, 2007
    #2
    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. PC
    Replies:
    2
    Views:
    3,942
    Marc Guardiani
    Nov 12, 2003
  2. miko

    convert euc-kr to hexa

    miko, Sep 21, 2003, in forum: Java
    Replies:
    1
    Views:
    440
    Roedy Green
    Sep 21, 2003
  3. Allan Bruce

    Read an Hexa file

    Allan Bruce, Jul 18, 2003, in forum: C++
    Replies:
    4
    Views:
    1,548
    Thomas Matthews
    Jul 18, 2003
  4. Golan

    Converting hexa to decimal

    Golan, Jan 8, 2004, in forum: C Programming
    Replies:
    3
    Views:
    2,161
    Kevin Goodsell
    Jan 9, 2004
  5. Sarath
    Replies:
    4
    Views:
    3,987
    Joe Greer
    Jan 30, 2008
Loading...

Share This Page