1
#include <iostream>
#include <cstdint>

int main() {
    std::uint8_t i{5}; // direct initialization
    std::cout << i;
    return 0;
}

I could not able to get the value 5 rather I am getting some other.

Why this code gives me some other ASCII value rather than giving value 5?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Does this answer your question? [uint8\_t can't be printed with cout](https://stackoverflow.com/questions/19562103/uint8-t-cant-be-printed-with-cout) – phuclv Dec 16 '19 at 04:08
  • `char` and `unsigned char` have special overloads that print the ASCII value. There are a lot of duplicates on that: [Why does int8_t and user input via cin shows strange result (duplicate)](https://stackoverflow.com/q/24617889/995714), [cout uint8_t as integers instead of chars](https://stackoverflow.com/q/18246154/995714), [Why doesn't uint8_t and int8_t work with file and console streams? (duplicate)](https://stackoverflow.com/q/17874122/995714)... – phuclv Dec 16 '19 at 04:11

1 Answers1

0

Use

std::cout << static_cast<int>( i );

The type std::uint8_t is defined as an alias for unsigned char.

Here is a demonstrative program.

#include <iostream>
#include <cstdint>

int main()
{
    std::uint8_t i { 65 };
    std::cout << i << '\n';
    std::cout << static_cast<int>( i ) << '\n';
}   

Its output is

A
65
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335