Hex character to int in C++

前端 未结 4 629
猫巷女王i
猫巷女王i 2021-01-14 01:20

How can I change a hex character, not string, into a numerical value?

While typing this question, I found many answers on how to convert hex strings to values. Howe

4条回答
  •  离开以前
    2021-01-14 02:05

    You already have a solution that works with strings. Use it for chars too:

    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    int main()
    {
      char val = 'A';
      unsigned int myval;
      std::stringstream ss;
      ss << val;
      ss >> std::hex >> myval;
      cout << myval << endl;
    }
    

    Code

提交回复
热议问题