Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uint8_t iostream behavior

Abstract: I was expecting the code: cout << uint8_t(0); to print "0", but it doesn't print anything.

Long version: When I try to stream uint8_t objects to cout, I get strange characters with gcc. Is this expected behavior? Could it be that uint8_t is an alias for some char-based type? See compiler/system notes in the code example.

// compile and run with:
// g++ test-uint8.cpp -std=c++11 && ./a.out
//                    -std=c++0x (for older gcc versions)
/**
 * prints out the following with compiler:
 *     gcc (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)
 * on the system:
 *     Linux 3.7.9-101.fc17.x86_64
 * Note that the first print statement uses an unset uint8_t
 * and therefore the behaviour is undefined. (Included here for
 * completeness)

> g++ test-uint8.cpp -std=c++11 && ./a.out
>>>�<<<    >>>194<<<
>>><<<    >>>0<<<
>>><<<    >>>0<<<
>>><<<    >>>0<<<
>>><<<    >>>1<<<
>>><<<    >>>2<<<

 *
 **/

#include <cstdint>
#include <iostream>

void print(const uint8_t& n)
{
    std::cout << ">>>" << n                 << "<<<    "
              << ">>>" << (unsigned int)(n) << "<<<\n";
}

int main()
{
    uint8_t a;
    uint8_t b(0);
    uint8_t c = 0;
    uint8_t d{0};
    uint8_t e = 1;
    uint8_t f = 2;
    for (auto i : {a,b,c,d,e,f})
    {
        print(i);
    }
}
like image 348
Johann Avatar asked Mar 08 '13 14:03

Johann


People also ask

What does uint8_t * mean?

Unsigned Integers of 8 bits. A uint8 data type contains all whole numbers from 0 to 255. As with all unsigned numbers, the values must be non-negative. Uint8's are mostly used in graphics (colors are always non-negative).

What is uint8_t * in C?

In C, the unsigned 8-bit integer type is called uint8_t . It is defined in the header stdint. h . Its width is guaranteed to be exactly 8 bits; thus, its size is 1 byte.

Is uint8_t always unsigned char?

If it exists, uint8_t must always have the same width as unsigned char . However, it need not be the same type; it may be a distinct extended integer type. It also need not have the same representation as unsigned char ; for instance, the bits could be interpreted in the opposite order.

What is the difference between Uint8 and uint8_t?

The difference between Uint8 and uint8_t will depend on implementation, but usually they will both be 8 bit unsigned integers. Also uint8_t and uint16_t are defined by C (and maybe C++) standard in stdint. h header, Uint8 and Uint16 are non-standard as far as I know.


2 Answers

uint8_t is an alias for unsigned char, and the iostreams have special overloads for chars that print out the characters rather than formatting numbers.

The conversion to integer inhibits this.

like image 108
Kerrek SB Avatar answered Sep 26 '22 17:09

Kerrek SB


Could it be that uint8_t is an alias for some char-based type?

Absolutely. It's required to be a typedef for a builtin 8-bit unsigned integral type, if such a type exists. Since there are only two possible 8-bit unsigned integral types, char for a compiler that treats it as unsigned and unsigned char, it must be one of those. Except on systems where char is larger than 8 bits, in which case it won't exist.

like image 27
Pete Becker Avatar answered Sep 24 '22 17:09

Pete Becker