Discussion:
using printf for uchar* datatype
(too old to reply)
Alex Blekhman
18 years ago
Permalink
How to use printf for uchar* datatype. For e.g., If I have
a structure
called A with one of its member as uchar* data then how do
i print the value
of data using printf??
What is it `uchar'? Unsigned shar? Wide char? You can print
wide string with `printf' with "%ls" format:

printf("Here's wide string: %ls", L"Hello");

Alex
Giovanni Dicanio
18 years ago
Permalink
How to use printf for uchar* datatype. For e.g., If I have a structure
called A with one of its member as uchar* data then how do i print the
value
of data using printf??
Do you mean wchar_t * ? (i.e. Unicode UTF-16 strings)

If so, you might find the documentation and samples for printf and wprintf
to be interesting:

http://msdn2.microsoft.com/en-us/library/wc7014hz(VS.80).aspx

BTW: You should better use the "safe" versions (e.g. wprintf_s ...)

Giovanni
Igor Tandetnik
18 years ago
Permalink
How to use printf for uchar* datatype. For e.g., If I have a structure
called A with one of its member as uchar* data then how do i print
the value of data using printf??
What's contained in this field? If it just contains ASCII characters,
use %s as you would for char* .
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
kunal s patel
18 years ago
Permalink
Hi,

I am sorry for incomplete information. uchar is defined as unsigned char.
Now here's what i am doing

Within a function, I am receiving payload as function argument.

void process (uchar *pay)

Now I have structure called A with uchar data as its member. So in function
body i do the following

struct A a;
memcpy((char*)&a,(char*)pay,sizeof(A));

Now I want to print out the value of a.data. How do i do that?

Kunal
...
Brian Muth
18 years ago
Permalink
Post by kunal s patel
Hi,
I am sorry for incomplete information. uchar is defined as unsigned char.
Now here's what i am doing
Within a function, I am receiving payload as function argument.
void process (uchar *pay)
Now I have structure called A with uchar data as its member. So in function
body i do the following
struct A a;
memcpy((char*)&a,(char*)pay,sizeof(A));
Now I want to print out the value of a.data. How do i do that?
It depends. You haven't told us whether the payload is text or binary.

If it's text and you know the length of the payload, then try:

printf ("%.50s\n", a.data); // for example if the payload is 50 chars

If it's binary, you might want to print out hex using %x, using a for loop.

Brian
kunal s patel
18 years ago
Permalink
...
Igor Tandetnik
18 years ago
Permalink
Post by kunal s patel
I am sorry for incomplete information. uchar is defined as unsigned
char. Now here's what i am doing
Within a function, I am receiving payload as function argument.
void process (uchar *pay)
Now I have structure called A with uchar data as its member. So in
function body i do the following
struct A a;
memcpy((char*)&a,(char*)pay,sizeof(A));
Now I want to print out the value of a.data. How do i do that?
What type is a.data, and what kind of data is stored there?
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Alex Blekhman
18 years ago
Permalink
Post by kunal s patel
Hi,
I am sorry for incomplete information. uchar is defined as
unsigned char.
Now here's what i am doing
Within a function, I am receiving payload as function
argument.
void process (uchar *pay)
Now I have structure called A with uchar data as its
member. So in function
body i do the following
struct A a;
memcpy((char*)&a,(char*)pay,sizeof(A));
Now I want to print out the value of a.data. How do i do
that?
You print one single-byte character like this:

printf("%hc", a.data);

If you want to print its numerical value, then just cast it
to `unsigned int':

printf("%u", (unsigned)a.data);


Alex
Kenneth Porter
18 years ago
Permalink
Post by kunal s patel
Within a function, I am receiving payload as function argument.
void process (uchar *pay)
Now I have structure called A with uchar data as its member. So in
function body i do the following
struct A a;
memcpy((char*)&a,(char*)pay,sizeof(A));
Now I want to print out the value of a.data. How do i do that?
printf only knows about certain builtin types with unambiguous
interpretation. "unsigned char" has no implicit meaning. So you need to
assign it a meaning.

I suggest that instead of using printf, you use iostream. You then
implement an inserter (operator <<) for your struct A and render an
appropriate interpretation of the data to the stream. You can then say:

A a;
std::cout << a;
Giovanni Dicanio
18 years ago
Permalink
Post by Kenneth Porter
I suggest that instead of using printf, you use iostream. You then
implement an inserter (operator <<) for your struct A and render an
A a;
std::cout << a;
To provide some code to the OP, I think Kenneth's idea is something like
this:

<code>

class A
{
public:
...

// operator<< can access private data in A
friend std::ostream & operator<<( std::ostream & os, const A & a );
};

std::ostream & operator<<( std::ostream & os, const A & a )
{
// Print data fields into os
// ... os << a.field1 ...

return os;
}

</code>

However, it seems that for localization, it is better to use the printf-like
formatting (maybe using more modern CString.FormatMessage) than <<
inserters...
(There was a thread about that on the MFC newsgroup, IIRC).

Giovanni
Kenneth Porter
18 years ago
Permalink
Post by Giovanni Dicanio
However, it seems that for localization, it is better to use the
printf-like formatting (maybe using more modern CString.FormatMessage)
than << inserters...
(There was a thread about that on the MFC newsgroup, IIRC).
Or you could use C++ locales and facets. A quick google turned up this page
on the subject:

http://www.tacc.utexas.edu/resources/user_guides/pgi/pgC++_lib/stdlibug/sta
_9169.htm

Loading...