0

I am using the following code to convert the raw data values to a hexstring so I can find some information. But I am getting FFFFFFFF where I was supposed to get FF.

For example, the result should be "FF 01 00 00 EC 00 00 00 00 00 00 00 00 00 E9", but I am getting "FFFFFFFFF 01 00 00 FFFFFFEC 00 00 00 00 00 00 00 00 00 FFFFFFE9".

Does anyone know what is happening here?

std::vector<unsigned char> buf;
buf.resize( ANSWER_SIZE);

// Read from socket
m_pSocket->Read( &buf[0], buf.size() );
string result( buf.begin(), buf.end() );
result = ByteUtil::rawByteStringToHexString( result );

std::string ByteUtil::int_to_hex( int i )
{
    std::stringstream sstream;
    sstream << std::hex << i;
    return sstream.str();
}

std::string ByteUtil::rawByteStringToHexString(std::string str)
{
    std::string aux = "", temp = "";
    for (unsigned int i=0; i<str.size(); i++) {
        temp += int_to_hex(str[i]);

        if (temp.size() == 1) {
            aux += "0" + temp + " "; // completes with 0
        } else if(i != (str.size() -1)){
            aux += temp + " ";
        }
        temp = "";
    }

    // System.out.println(aux);
    return aux;
}

UPDATE: Debugging, I noticed that the int_to_hex is returning FFFFFFFF instead of FF. How can I fix that?

RafaelTSCS
  • 1,234
  • 2
  • 15
  • 36
  • 3
    Read about [sign extension](http://en.wikipedia.org/wiki/Sign_extension). – Some programmer dude Jun 27 '14 at 17:14
  • Also, you know that this would be very simple with a [`std::istringstream`](http://en.cppreference.com/w/cpp/io/basic_istringstream). – Some programmer dude Jun 27 '14 at 17:15
  • 1
    FFFFFF.. suggests that you are running into a problem with signedness. If the first bit of a 1-byte value is '1', then it's a negative number, and when you convert it to a 4-byte value (an int), it's going to have a ton of F's in front of it. – QuestionC Jun 27 '14 at 17:15
  • @JoachimPileborg I did what they suggested [here](http://stackoverflow.com/questions/7639656/getting-a-buffer-into-a-stringstream-in-hex-representation), but I got the same result. I am quite new to c++ and I addapted the code above from Java where it works fine. – RafaelTSCS Jun 27 '14 at 18:18

2 Answers2

3

Note the use of unsigned instead of int in int_to_hex().

#include <iostream>
#include <sstream>

std::string int_to_hex(unsigned i)
{
    std::stringstream sstream;
    sstream << std::hex << i;
    return sstream.str();
}

int main() {
    std::cout << int_to_hex(0xFF) << "\n";
}

Output

[1:58pm][wlynch@watermelon /tmp] ./foo
ff

Additionally...

We also can see in the question you are looking at, that they do a static_cast to achieve the same result.

ss << std::setw(2) << static_cast<unsigned>(buffer[i]);
Community
  • 1
  • 1
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • Hi @sharth. Thank you for your reply. I did this change, but still had no success. I am actually not trying to print it, but to use this result to match with a regular expression and so extract some info from it. What I am doing is exactly: read the socket answer into a vector; pass its content into a string by: string result( buf.begin(), buf.end() ); and then I call rawByteStringToHexString. – RafaelTSCS Jun 27 '14 at 19:11
  • @RafaelTSCS: So your edit says that you've reduced the problem to that function returning `FFFFFFFF` instead of `FF` as you wanted. Is there an additional problem beyond that? You've talked about how you want to use it, but you need to say: __I expect the output to be foo. The output my code currently produces is bar.__ – Bill Lynch Jun 27 '14 at 19:15
  • I need int_to_hex to return FF and instead of FFFFFFFF. I've edited the code into the question, so I show how I am reading the socket. – RafaelTSCS Jun 27 '14 at 19:20
0

OK Guys, sorry for the delay, I ran into the weekend and had some FIFA World Cup games to watch.

I was getting the same result no matter the change I made, so I decided to make some changes in the code before and I switched the input param from

std::string ByteUtil::rawByteStringToHexString(std::string str)

to

std::string ByteUtil::rawByteStringToHexString(vector<unsigned char> v)

and also kept the changes suggested by @sharth. Now I am getting the correct result.

Thank you all for the help!!

RafaelTSCS
  • 1,234
  • 2
  • 15
  • 36