1

It took time looking as utf8 convert string to hexadecimal string, and backwards I found some examples and possible solutions, but all work well only without special characters. I have a folowing :

string in="áéíóúñü"

The result shoud be:

"c3a1c3a9c3adc3b3c3bac3b1c3bc"

I try following post, and others:

C++ convert string to hexadecimal and vice versa

How to convert a string in hexadecimal string?

http://www.cplusplus.com/forum/beginner/161703/


I will try to explain better, but I can not speak English properly. Sorry.

I have to send some data using socket. For that I have to convert names to hexadecimal using UTF-8, but in some cases have specials characters for example á, é, í... When converting normal letters get a string length of 2 per letter.

a-> "61"

e-> "65"

But special characters are encoded (on UTF-8) with length 4

á-> "c3a1" this is the correct conversion

é-> "c3a9" this is the correct conversion

I have attempted the conversion of all the ways I've found, including that suggested me down. But every time you convert a special character gives me an answer of 2 digits, that is not correct.

á-> "e1" this isnt correct

é-> "e9" this isnt correct

Community
  • 1
  • 1
Maialen
  • 31
  • 1
  • 6
  • Hi and welcome to SO! Please read [this](http://stackoverflow.com/help/how-to-ask) post for how to ask better questions, which will help people give you better answers. In particular, add the code of what you've already tried. – Steve Heim May 17 '16 at 10:39

1 Answers1

2

Loop over each "character" in the std::string object, output it's two-digit hexadecimal equivalent as an int.

For looping, I recommend you look into range-based for loops.

To set the number of digits to print, read about setting stream precision.

To print a number as hexadecimal, read about the base I/O manipulators.

To convert to an int read about static_cast.

Oh, and I recommend using an unsigned char for the single "characters".


Simple solution based on the above:

std::string stoh(std::string const& in)
{
    std::ostringstream os;

    for(unsigned char const& c : in)
    {
        os << std::hex << std::setprecision(2) << std::setw(2)
           << std::setfill('0') << static_cast<int>(c);
    }

    return os.str();
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I tryed it, but the result is the same, special characters are wrong. I use ss << std::setw(2) << static_cast(static_cast(in[i])); for each character – Maialen May 17 '16 at 10:38
  • @Maialen And how did that work? What problem did you have? What "special character" did it fail on? How did it fail? What result did you get, and what did you expect? I have updated my answer with some code using the what I mentioned before. – Some programmer dude May 17 '16 at 10:45
  • The result is the same. – Maialen May 17 '16 at 11:56
  • @Maialen Maybe your standard locale have extended ASCII encoding for the characters that you get the "wrong" output from? Create a simple text-file using those characters, and look at it in a hex-editor. Maybe it's better to use a specific library for handling UTF-8, like [utfcpp](http://utfcpp.sourceforge.net/) or [libiconv](https://www.gnu.org/software/libiconv/)? – Some programmer dude May 17 '16 at 12:04
  • @Maialen Also [it works fine for me](http://ideone.com/2TDnbP). So it does indeed seem like a problem with your encoding not being what you think it is, or your locale settings causing the problem. – Some programmer dude May 17 '16 at 12:09
  • That was the problem. But I have to use WxWidgets + Code-block and changing the encoding utf8 manually stop seeing the texts of the buttons correctly so I did not realize. THANK YOU VERY MUCH. – Maialen May 17 '16 at 12:53