0

I'm using C++11. Is there a function that gets the string of hex and turns it into decimal?

I've tried to find something about unicode() function that I saw somewhere in StackOverflow, but I couldn't find any information about it.

For example, we have a symbol hex: U+03C0. How to turn it into dec: 960?

Let's imagine this:

char* pi_symbol = hexToDec("U+03C0");
cout << "The pi dec is " << pi_symbol << endl;

And get this:

The pi dec is 960

younyokel
  • 327
  • 2
  • 15

1 Answers1

1

You can use std::hex:

#include <iostream>
#include <sstream>
int main()
{
    int n;
    std::istringstream("03C0") >> std::hex >> n;
    std::cout << n;
}
Oblivion
  • 7,176
  • 2
  • 14
  • 33
  • Could you please give me an example how to use `std::hex` since I don't really get it. – younyokel Sep 06 '19 at 19:01
  • @EadwineYoun there is one in the link – Oblivion Sep 06 '19 at 19:03
  • @EadwineYoun check this live link https://wandbox.org/permlink/qm8rj1sBIroRjqsT – Oblivion Sep 06 '19 at 19:14
  • Hm, why does it give us `3C0` instead of `U+03C0`? – younyokel Sep 06 '19 at 19:16
  • 2
    Because `U+03C0` is not hex. It's a representation of a Unicode value. Strictly, hex just means base 16. For example the `0x` prefix to `0x3C0` just is syntax sugar that provide so we can differentiate between base 16, 8, 10 values (C/C++ context). Glossing over details, the simple answer: The `U+` prefix is not a prefix to indicate hexadecimal-ness. It's a prefix to indicate possible Unicode-ness. Slice off the U+, then interpret the value (the hex number). – jdw Sep 06 '19 at 20:17
  • @jdw thanks for explaining. I really appreciate that :) – younyokel Sep 13 '19 at 16:00