How to use uint8_t and to initialize a variable
#include<iostream>
using namespace std;
int main()
{
uint8_t a = 6;
cout << a;
return 1;
}
It is printing some symbol
C++ treats uint8_t as char - because that's pretty much what it is.
If you pass a char to cout, it'll print as a char, which, with a value of 6, is the ACK symbol (which would probably display strangely, depending on your terminal settings).
If you want it to be printed as a number, casting it to an unsigned in cout should do the trick:
cout << (unsigned)a;
You can cast the variable a in order to print that as a number and not an ascii symbol
#include<iostream>
#include <cstdint>
int main()
{
uint8_t a = 6;
std::cout << "a: " << a << std::endl;
std::cout << "a casted to char(is the same type actually): " << char(a) << std::endl;
std::cout << "a casted to int: " << int(a) << std::endl;
getchar();
return 0;
}
You can use good old type-unsafe printf.
#include <cstdint>
#include <cstdio>
int main()
{
std::uint8_t a = 6;
std::printf("%d\n", a);
}
Related
I want to convert a four-byte string to either int32 or uint32 in c++.
This answer helped me to write the following code:
#include <string>
#include <iostream>
#include <stdint.h>
int main()
{
std::string a("\xaa\x00\x00\xaa", 4);
int u = *(int *) a.c_str();
int v = *(unsigned int *) a.c_str();
int x = *(int32_t *) a.c_str();
int y = *(uint32_t *) a.c_str();
std::cout << a << std::endl;
std::cout << "int: " << u << std::endl;
std::cout << "uint: " << v << std::endl;
std::cout << "int32_t: " << x << std::endl;
std::cout << "uint32_t: " << y << std::endl;
return 0;
}
However the outputs are all the same, where the int (or int32_t) values are correct, but the unsigned ones are wrong. Why does not the unsigned conversions work?
// output
int: -1442840406
uint: -1442840406
int32_t: -1442840406
uint32_t: -1442840406
Pythons struct.unpack, gives the right conversion
In [1]: import struct
In [2]: struct.unpack("<i", b"\xaa\x00\x00\xaa")
Out[2]: (-1442840406,)
In [3]: struct.unpack("<I", b"\xaa\x00\x00\xaa")
Out[3]: (2852126890,)
I would also like a similar solution to work for int16 and uint16, but first things first, since I guess an extension would be trivial if I manage to solve this problem.
You need to store the unsigned values in an unsigned variables and it will work:
#include <string>
#include <iostream>
#include <stdint.h>
int main()
{
std::string a("\xaa\x00\x00\xaa", 4);
int u = *(int *) a.c_str();
unsigned int v = *(unsigned int *) a.c_str();
int32_t x = *(int32_t *) a.c_str();
uint32_t y = *(uint32_t *) a.c_str();
std::cout << a << std::endl;
std::cout << "int: " << u << std::endl;
std::cout << "uint: " << v << std::endl;
std::cout << "int32_t: " << x << std::endl;
std::cout << "uint32_t: " << y << std::endl;
return 0;
}
When you cast the value to unsigned and then store it in a signed variable, the compiler plays along. Later, when you print the signed variable, the compiler generates code to print a signed variable output.
I have to write a program to test an integer value to determine if it is odd or even, and make sure my output is clear and complete. In other words, I have to write the output like "the value 4 is an even integer". I was also hinted that I have to check the value using the remainder modulo.
The issue I have is with the scanf() function. I get a syntax error:
'%=' expected a ')'
How do I fix this?
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int main()
{
int number = 0;
cout << "enter an integer ";
int scanf(%=2 , &number);
if (number == 0)
cout << "the value" << number << "is even";
else
cout << "the value" << number << "is odd";
return 0;
}
You are using scanf() incorrectly (read the scanf() documentation on cppreference.com). The first parameter expects a null-terminated string containing the format to scan, but you are not passing in anything that even resembles a string. What you are passing in is not valid string syntax, per the C++ language standard. That is why you are getting a syntax error.
You need to change this line:
int scanf(%=2 , &number);
To this instead:
scanf("%d", &number);
Though, in C++ you really should be using std::cin instead for input (you are already using std::cout for output):
std::cin >> number;
Try this:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int number = 0;
cout << "enter an integer ";
if (cin >> number)
{
if ((number % 2) == 0)
cout << "the value " << number << " is even";
else
cout << "the value " << number << " is odd";
}
else
cout << "the value is invalid";
return 0;
}
I know this question is a little dated, however, if you are able to use modern C++ features. You can write a constexpr helper function such as this:
#include <cstdint>
constexpr bool isEven(uint32_t value) {
return ((value%2) == 0);
}
Then in your main function, you can traverse through a loop of N integers and output your display such as:
#include <iostream>
#include <iomanip>
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << " is "
<< (isEven(i) ? "even" : "odd") << '\n';
}
return 0;
}
It's literally that simple. Here's another nice feature of using the constexpr helper function... You can also format your output as such:
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << ": "
<< std::boolalpha << isEven(i) << '\n';
}
return true;
}
If you are looking for something that is more efficient than using the modulo operator you can bitwise & with the least significant digit... The code above would then become:
#include <cstdint>
constexpr bool isOdd(uint32_t value) {
return (value&1);
}
And using it would be very similar as above, just make sure you reverse the wording in your output to match that from the function being used...
#include <iostream>
#include <iomanip>
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << " is "
<< (isOdd(i) ? "odd" : "even") << '\n';
}
return 0;
}
Again you can use the std::boolalpha manipulator to get this kind of output:
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << ": "
<< std::boolalpha << isOdd(i) << '\n';
}
return true;
}
#include <stdio.h>
#include <iostream>
int main()
{
using namespace std;
uint64_t a = 3;
if (uint64_t(~a) == (~a))
cout << "right" << endl;//right
else
cout << "wrong" << endl;
cout << sizeof(~a) << endl;//8
uint8_t b = 3;
if (uint8_t(~b) == (~b))
cout << "right" << endl;
else
cout << "wrong" << endl;//wrong
cout << sizeof(~b) << endl;//4
getchar();
return 0;
}
~uint8_t returns int value,but ~uint64_t returns uint64_t .
Is this undefined behaviour ?
Posting from en.cppreference
The result of operator~ is the bitwise NOT (one's complement) value
of the argument (after promotion).
Integral promotion is applied to char, short int etc (types narrower than int) and the result needs to be casted to destination type if destination is not int.
This is the reason for sizeof(~b) == sizeof(int) in your case.
I am trying to convert a uint64_t to a uint8_t. I know this makes no sense normaly but since the JSON Library converts all numeric values to a uint64_t or int64_t I have to convert it back. I am always sure I do not receive values which will not fit into the uint8_t.
Now when I compile and run the following code on OSx everything works as expected. But as soon as I move to a Raspberry Pi 2 the code no longer works. The value is 0.
Can anybody explain why this is happening? And does somebody has a better solution?
#include <iostream>
#include "json.h"
using JSON = nlohmann::json;
typedef struct {
uint8_t boardId;
uint8_t commandGroupId;
uint8_t commandId;
} ExternalMessageType;
int main(int argc, const char * argv[])
{
JSON x;
ExternalMessageType y;
x["board-id"] = 1;
x["command-group-id"] = 1;
x["command-id"] = 11;
y.boardId = static_cast<uint8_t>(x["board-id"]);
y.commandGroupId = static_cast<uint8_t>(x["command-group-id"]);
y.commandId = static_cast<uint8_t>(x["command-id"]);
std::cout << "Board: " << (int)y.boardId << std::endl;
std::cout << "Group: " << (int)y.commandGroupId << std::endl;
std::cout << "Command: " << (int)y.commandId << std::endl;
if (y.commandGroupId == 1) {
std::cout << "Command Group is ok." << std::endl;
switch (y.commandId) {
case 11: {
std::cout << "Speed Message" << std::endl;
} break;
}
} else {
std::cout << "Command Group is not ok." << std::endl;
}
return 0;
}
i am a c coder, new to c++.
i try to print the following with cout with strange output. Any comment on this behaviour is appreciated.
#include<iostream>
using namespace std;
int main()
{
unsigned char x = 0xff;
cout << "Value of x " << hex<<x<<" hexadecimal"<<endl;
printf(" Value of x %x by printf", x);
}
output:
Value of x ΓΏ hexadecimal
Value of x ff by printf
<< handles char as a 'character' that you want to output, and just outputs that byte exactly. The hex only applies to integer-like types, so the following will do what you expect:
cout << "Value of x " << hex << int(x) << " hexadecimal" << endl;
Billy ONeal's suggestion of static_cast would look like this:
cout << "Value of x " << hex << static_cast<int>(x) << " hexadecimal" << endl;
You are doing the hex part correctly, but x is a character, and C++ is trying to print it as a character. You have to cast it to an integer.
#include<iostream>
using namespace std;
int main()
{
unsigned char x = 0xff;
cout << "Value of x " << hex<<static_cast<int>(x)<<" hexadecimal"<<endl;
printf(" Value of x %x by printf", x);
}
If I understand your question correctly, you should expect to know how to convert hex to dec since you have already assigned unsigned char x = 0xff;
#include <iostream>
int main()
{
unsigned char x = 0xff;
std::cout << std::dec << static_cast<int>(x) << std::endl;
}
which shall give the value 255 instead.
Further detail related to the the str stream to dec shall refer in http://www.cplusplus.com/reference/ios/dec/.
If you want to know the hexadecimal value from the decimal one, here is a simple example
#include <iostream>
#include <iomanip>
int main()
{
int x = 255;
std::cout << std::showbase << std::setw(4) << std::hex << x << std::endl;
}
which prints oxff.
The library <iomanip> is optional if you want to see 0x ahead of ff. The original reply related to hex number printing was in http://www.cplusplus.com/forum/windows/51591/.