1 byte data type ouputs junk

the following code outputs junk,


int _tmain(int argc, _TCHAR* argv[])
{
char j[] = { 9, 10};
cout<<j[0]<<" "<<j[1];
return 0;
}

Its almost like the 8 bit registers cant be used, because when I tryed this it also did not work, and I got more junk. I make k be the values i entered in the first program, but I got more junk, junk junk. Every time I use an 8 bit data type I get crap.
any ideas?

int _tmain(int argc, _TCHAR* argv[])
{
uint16_t k(0x090A);
char* p(reinterpret_cast<char*>(&k));
cout<<*p<<" "<<*(p+1)<<endl;
return 0;
}

In your first program, you're trying to output the ASCII values 9 and 10, which are tab and line feed. If you want to output them as integers, you need to first convert them:
std::cout <<(int)j[0]<<" "<<(int)j[1];

As for your second program, since you're using _tmain() I can assume you're on Windows, and most likely using an x86. x86 is little endian, so p[0]==10 and p[1]==9.
It's not junk.

when you give cout a char type to print - it displays the ASCII char that
represents by the value you gave.

So 9 will do a tab and 10 will do a linefeed (newline)
thanks, I was using uint8_t also and when I use cout i didnt know it would convert a numeric value to the ASCII value. I was trying to get the numeric values.

ex

uint8_t k=10;
cout<<k;


will output the newline

but

uint32_t k=10;
cout<<k;

will output the number 10

Last edited on
That's because uint8_t is equivalent to an unsigned char. uint32_t is equivalent to an unsigned int, which would output the actual number rather than its ASCII equivalent.
Topic archived. No new replies allowed.