-2

I need to take a peek at what's inside a declared unsigned char pointer, I reduced my problem to this short example

#include<iostream>
int main (int argc, char *argv[])
{
    unsigned char * buffer = new unsigned char;
    *buffer = 8;
    std::cout << "buffer = " << (unsigned char) (*buffer) << std::endl;
}

I am expecting this output : buffer = 8

But I get buffer =

Blank, which drives me nuts, not even any value !!!

How I am actually dealing with it :

#include<iostream>
typedef unsigned char uint8_t;
int main (int argc, char *argv[])
{
    uint8_t * buffer = new uint8_t ;
    *buffer = 8;
    std::cout << "buffer = " << (int) (*buffer) << std::endl;
}

I am using this example in ns3 , buffer constructs a one byte payload packet, and I need it to be a pointer. That's why I actually tagged my question as "C" along with "C++", because the core of the issue is also concerned in C. But I found myself down voted for that ! I know "cout" and "new" are c++ literals, but it's irrelevant to the issue !!

Not having a coding problem with all that, my problem is just what's unsigned char then if it reads as a regular char with cout !!!!!

I stated I am expecting it to be buffer = 8 because unsigned char are one byte integers.

Thank you guys because you made me notice cout is dealing with it as if it is a regular char, despite it was expected for me otherwise.

mahmoud fathy
  • 361
  • 6
  • 16

3 Answers3

3

If we extend your example slightly to this:

int main(int argc, char *argv[]) {
    unsigned char * buffer = new unsigned char;
    *buffer = 8;
    std::cout << "buffer = [" << (*buffer) << "]\n";
}

the output is

buffer = ]

The ASCII char 8 means backspace, and it has nuked the opening [. Notice you don't need the cast to unsigned char - *buffer is an unsigned char.

If you want it to have the character '8' you need to set its contents to 8. Don't forget to delete what you new.

int main(int argc, char *argv[]) {
    unsigned char * buffer = new unsigned char;
    *buffer = '8'; //<---- see the single quotes?
    std::cout << "buffer = [" << (*buffer) << "]\n";
    delete buffer;
}

with output

buffer = [8]

Of course, we don't really need these pointers:

int main(int argc, char *argv[]) {
    unsigned char buffer = '8';
    std::cout << "buffer = [" << buffer << "]\n";
}

And if you insist on using raw character codes:

int main(int argc, char *argv[]) {
    unsigned char buffer = 56;
    std::cout << "buffer = [" << buffer << "]\n";
}

Edit:

If what you want to know is what numeric value is in the buffer variable and therefore want << to report an integer value rather than stream this as a character, use a cast.

int main(int argc, char *argv[]) {
    unsigned char buffer = 56;
    std::cout << "buffer = [" <<static_cast<unsigned int>(buffer) << "]\n";
}
doctorlove
  • 18,125
  • 2
  • 46
  • 59
1

'8' is the character 8. 8 is just a code of a character which is invisible.

Try writing *buffer = 48; and guess why the output is "Buffer = 0" based on this table.

riodoro1
  • 1,226
  • 7
  • 14
0

Found this just now unsigned():

#include<iostream>
int main (int argc, char *argv[])
{
    unsigned char * buffer = new unsigned char;
    *buffer = 8;
    std::cout << "buffer = " << unsigned(*buffer) << std::endl;
}

it gives the output I wanted in my question buffer = 8 which is not buffer = '8' as some might have thought I meant

mahmoud fathy
  • 361
  • 6
  • 16