-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
  • 1
  • 7
  • 17

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,872
  • 2
  • 46
  • 62
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,246
  • 7
  • 14
  • Or possibly a backspace: http://www.asciitable.com/ – doctorlove Feb 16 '17 at 11:39
  • How about suggesting code that is clear to humans? Devoid of magic numbers – StoryTeller - Unslander Monica Feb 16 '17 at 11:40
  • @doctorlove. Most definitely a backspace. I don't remember my ASCII codes that well. – riodoro1 Feb 16 '17 at 11:40
  • I looked it up. – doctorlove Feb 16 '17 at 11:41
  • Though most platforms rely on the same ASCII encoding, `'0' == 48` is nevertheless platform-dependent. – barak manos Feb 16 '17 at 11:41
  • I get it, but (unsigned char) should be one byte integer numbers ?! – mahmoud fathy Feb 16 '17 at 11:42
  • Same goes for `8` being "a code of a character which is invisible". – barak manos Feb 16 '17 at 11:42
  • 1
    @mahmoudfathy Yes, it is one byte number, this is how character were (and still are) encoded on most platforms. The character in memory is just a byte and the table I linked gives you the idea of what number is interpreted as what character. The operator `<<` of `ostream` is implemented for `unsigned char` in such a way that it prints the character, not a number. If you want to see the number, cast the char to `unsigned int`. – riodoro1 Feb 16 '17 at 11:48
  • So perhaps the question is more how do I see the ASCII value of an unsigned char, since `<<` is overloaded for `unsigned char` and shows me this as a character? – doctorlove Feb 16 '17 at 11:53
  • @riodoro1 I should have noticed this comment before I edit this "How I am actually dealing with it :" section. So it's all about << operator , just deceiving me by showing something not in context of my program !. Thanks riodoro1 anyway – mahmoud fathy Feb 16 '17 at 12:21
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
  • 1
  • 7
  • 17