CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: C/C++ question

  1. #1
    Join Date
    Aug 2001
    Posts
    140

    C/C++ question

    Hi,
    I have a string like this:

    "4f020098e567"

    I need to read that string char by char and concatinate each two char's to form a byte to get hexadecimal value. I know we can use shift operators to do this. Is there any simplest way to do.

    Thanks,
    Thiru


  2. #2
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: C/C++ question

    I don't know how you'd do it using shift parameters but..

    std::string s("4f020098e567");
    std::vector< BYTE > vectByte;
    int len = s.size();
    int pos;
    char *stop; // required for later
    if ( len & 1 ) // odd length so insert leading 0
    {
    s.insert( 0, 1, '0' );
    len++;
    }
    vectByte.resize( len / 2 );

    for ( pos=0; pos < len; pos += 2 )
    {
    std::string sub( s, spos, 2 ); // gets sub string
    vectPos[ pos / 2 ] = (BYTE)strtoul( sub.c_str(), &stop, 16 );
    }






  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: C/C++ question

    you need some more commentary?

    std::string s("4f020098e567"); // s is the string to convert
    std::vector< BYTE > vectByte; // this gives an array of variable length of the bytes
    int len = s.size(); // get the length of the string
    int pos; // required to run through the string
    char *stop; // required for second parameter of strtoul
    /*
    we are going to run through 2 characters at a time
    if our length is odd, there will be a spare one
    at the end. We could report this as an error but
    let's fix it and put in a leading zero
    */
    if ( len & 1 )
    {
    s.insert( 0, 1, '0' ); // puts an extra '0' character at the front of string
    len++; // which increases its length by 1
    }
    vectByte.resize( len / 2 ); // set the size of our byte array - half the length

    for ( pos=0; pos < len; pos += 2 )
    {
    std::string sub( s, spos, 2 ); // gets sub string of 2 characters
    /*
    now the bit we have been leading up to - use the
    strtoul function with base 16. This will convert our string (substring of 2 characters) to
    an unsigned long. It will be between 0 and 255.
    */
    vectPos[ pos / 2 ] = (BYTE)strtoul( sub.c_str(), &stop, 16 );
    }



    Now the array vectPos will contain 6 bytes as follows:
    [0]: 0x4f (79)
    [1]: 0x02 (2)
    [2]: 0x00 (0)
    [3]: 0x98 (152)
    [4]: 0xe5 (229)
    [5]: 0x67 (103)


    I assume that is what you wanted


  4. #4
    Join Date
    Aug 2001
    Posts
    140

    Re: C/C++ question

    I am using CString, Is it ok, or it need any modifications.
    -Thiru


  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: C/C++ question

    modifications with CString (note this is not the MFC forum but we know CString here anyway)

    1. s.Insert(0, '0');
    2. CString sub = s.Mid( pos, 2 );
    3. No need to put in c_str(), you can pass the CString sub directly to strtoul




    You an also use CArray< UCHAR, UCHAR > instead of std::vector, but it has the disadvantage of not being copyable, so you have to pass one in by reference to be modified, or stick with std::vector.

    If you use CArray, it is SetSize() not resize()



  6. #6
    Join Date
    Aug 2001
    Posts
    140

    Re: C/C++ question

    Thanks


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured