To print the value inside an Unsigned character pointer
Solution 1
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.
Solution 2
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.
rvp
Interests in Distributed Systems , Artificial Intelligence , Big Data Analytics and Android FW.
Updated on June 15, 2022Comments
-
rvp almost 2 years
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)"
-
Jongware over 10 yearsAgreed. You should think about the future usage of your unsigned char ptr -- are you going to use it with regular C-strings, or with 'raw memory' bytes? Planning ahead and using the proper type ensures the compiler is able catch a class of possible errors.
-
Jess almost 7 yearsIf I know its being used as byte data, then I use %x.