0

How to assign 0xFFFF to int16_t correctly?

code: #include <stdio.h> #include <stdlib.h> #include "stdint.h" using namespace std; int main() { int16_t x=-1; printf("x=%04X\n",x); return 0; } output: x=FFFFFFFF How to get x=FFFF here?

24th Aug 2017, 2:15 PM
ŠŠøŠŗŠ¾Š»Š°Š¹ Š›Ń‘Š³ŠŗŠøŠ¹
ŠŠøŠŗŠ¾Š»Š°Š¹ Š›Ń‘Š³ŠŗŠøŠ¹ - avatar
2 Answers
+ 6
I encourage you to use C++'s cout facility instead of printf function. This code is working fine. #include <iostream> #include <stdlib.h> #include "stdint.h" using namespace std; int main() { int16_t x=-1; cout << "x = " << hex << x << endl; return 0; } output: x=ffff std::hex is a stream manipulator which sets the format flag to hexadecimal.
24th Aug 2017, 3:27 PM
Babak
Babak - avatar
+ 2
int i=0xffff; cout << i << endl; Adapt to suit :)
24th Aug 2017, 3:00 PM
Kirk Schafer
Kirk Schafer - avatar