Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To print the value inside an Unsigned character pointer

Tags:

c++

c

pointers

What is the use of unsigned char stream.

If I am having an unsigned character stream as below , how can i print the values inside that stream.

For an ordinary character stream or string i can print it using %s, but what about this.

Thanks in advance.

unsigned char *ptr ="(some value)"
like image 763
rvp Avatar asked Aug 22 '13 06:08

rvp


2 Answers

When it comes to string handling, there is no notable difference between unsigned char and plain char. The latter can be either signed or unsigned, it is implementation-defined. However, all symbol tables always have positive index. So when you are dealing with strings, you can always safely cast between unsigned char and char. (Pointer casts between the two types are formally poorly defined behavior by the standard, but in reality, it will always work on any system ever made.)

Thus printf("%s", ptr); will work fine.

like image 105
Lundin Avatar answered Oct 27 '22 09:10

Lundin


An unsigned char array usually represents raw byte data and if you want to output that as a string by using %s, you'll probably get gibberish characters, that'll be of no meaning to you. If you really wish to see the values, then i recommend conversion of a single byte to short and then output it with a %i.

Just my opinion.

like image 33
DaMachk Avatar answered Oct 27 '22 08:10

DaMachk