0

I found an explanation to decode hex-representations into decimal but only by using Qt: How to get decimal value of a unicode character in c++

As I am not using Qt and cout << (int)c does not work (Edit: it actually does work if you use it properly..!):

How to do the following:

I got the hex representation of two chars which were transmitted over some socket (Just figured out how to get the hex repr finally!..) and both combined yield following utf16-representation:

char c = u"\0b7f"

This shall be converted into it's utf16 decimal value of 2943! (see it at utf-table http://www.fileformat.info/info/unicode/char/0b7f/index.htm)

This should be absolut elementary stuff, but as a designated Python developer compelled to use C++ for a project I am hanging this issue for hours....

Community
  • 1
  • 1
  • 2
    On almost every system a char will not hold a full utf16 character. UTF16 is 2/4 bytes, whereas a char is one. Also, what do you mean by conversion? These numbers are all represented in the same way on a computer. – Nonanon Nov 11 '16 at 11:20
  • 1
    `char` is only 1 byte, you might want to use `wchar_t`, which is 2 bytes on Windows and 4 on Posix, or `char16_t` to be platform independet, if you are using C++11 and above. Then you can just cast the variable to `int` to get the numerical value. – Karsten Koop Nov 11 '16 at 11:23
  • Youre absolutely right, just edited my question....two chars combined give me this utf16-representation....I just want to get the decimal value "2943" out of "\0b7f" (see char table). – Dr. John James Cobra Nov 11 '16 at 11:24
  • @ Karsten Koop: Just tested your solution, when I say **char16_t c = '0b7f'; int x = c; std::cout << x;** it yields "14182". Why? Thanks a lot – Dr. John James Cobra Nov 11 '16 at 11:28

1 Answers1

1

Use a wider character type (char is only 8 bits, you need at least 16), and also the correct format for UTC literals. This works (live demo):

#include <iostream>

int main()
{
    char16_t c = u'\u0b7f';

    std::cout << (int)c << std::endl;  //output is 2943 as expected

    return 0;
}
Smeeheey
  • 9,906
  • 23
  • 39
  • Works! Forgot the "u" after the backslash! I had u"\0b7f" instead of u"\u0b7f"....Thank you a lot, I should ask more quickly on stackoverflow as I lost approx. 6 hours on this crap – Dr. John James Cobra Nov 11 '16 at 11:32