简体   繁体   中英

Casting hex number to signed integer

I have been trying to cast a hex number to signed integer. But its not recognizing negative number.

Here is an example. Do I need to sign extend separately instead of casting?

#include <iostream>

using namespace std;

int main()
{
    cout<<"Hello World";
    int64_t input = static_cast<int64_t>(0xf8546);
    
    cout << "Input: " << input << endl;

    return 0;
}

There are no such things as negative literals in C++. 0xf8546 is positive. Its type depends on your platform.

If you want a negative hexadecimal number then write

-0xf8546

This consists of the unary negation operator applied to the literal 0xf8546 , in the same way that -1 is the unary negation of the literal 1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM