C to C++ statement conversion

C

CPlusPlus

How can this C statement be converted to C++?



printf(" %.*s\n", sizeof(devInquiry.inq.dev.prodID),
devInquiry.inq.dev.prodID);

cout << ?????


In case, someone is wondering, what is prodID. It is member of
structure, e.g, as shown;


struct DEV_INQ
{
// data members
...
unsigned char prodID[16];
// some more stuff here
...
}DEVINQ;



Thanks
 
J

Jonathan Lee

How can this C statement be converted to C++?

printf("  %.*s\n", sizeof(devInquiry.inq.dev.prodID),
devInquiry.inq.dev.prodID);

Use ostream::write().

Or, if you really want the operator<< notation,
construct a string from your unsigned char array.

(You may just want to make prodID a string instead of
a fixed size array. Depends on your needs, I guess.)

--Jonathan
 
I

Ian Collins

How can this C statement be converted to C++?

printf(" %.*s\n", sizeof(devInquiry.inq.dev.prodID),
devInquiry.inq.dev.prodID);

With or without the bug?
 
T

tonydee

How can this C statement be converted to C++?

printf("  %.*s\n", sizeof(devInquiry.inq.dev.prodID),
devInquiry.inq.dev.prodID);

cout << ?????

   ...
   unsigned char prodID[16];

That's tricky. The .* in the printf format implies prodID might not
be NUL terminated, so we'd need to do a character-by-character search
for the first NUL, but not searching past 16 characters. Off the top
of my head:

void* p = devInquiry.inq.dev.prodID;
size_t size = sizeof devInquiry.inq.dev.prodID;

cout << " ";
void* p_nul = memchr(p, '\0', size);
cout.write(p, p_nul ? p_nul - p : size);
cout << '\n';

Which - pretty clearly I'd say - is worse than printf(). You could
try the boost format library, which uses a printf-inspired format
notation but adds type safety and other improvements....

Cheers,
Tony
 
C

CPlusPlus

How can this C statement be converted to C++?
printf("  %.*s\n", sizeof(devInquiry.inq.dev.prodID),
devInquiry.inq.dev.prodID);
cout << ?????
   ...
   unsigned char prodID[16];

That's tricky.  The .* in the printf format implies prodID might not
be NUL terminated, so we'd need to do a character-by-character search
for the first NUL, but not searching past 16 characters.  Off the top
of my head:

    void* p = devInquiry.inq.dev.prodID;
    size_t size = sizeof devInquiry.inq.dev.prodID;

    cout << "  ";
    void* p_nul = memchr(p, '\0', size);
    cout.write(p, p_nul ? p_nul - p : size);
    cout << '\n';

Which - pretty clearly I'd say - is worse than printf().  You could
try the boost format library, which uses a printf-inspired format
notation but adds type safety and other improvements....

Cheers,
Tony

Alright!
 
C

CPlusPlus

How can this C statement be converted to C++?
printf("  %.*s\n", sizeof(devInquiry.inq.dev.prodID),
devInquiry.inq.dev.prodID);
cout << ?????
   ...
   unsigned char prodID[16];
That's tricky.  The .* in the printf format implies prodID might not
be NUL terminated, so we'd need to do a character-by-character search
for the first NUL, but not searching past 16 characters.  Off the top
of my head:
    void* p = devInquiry.inq.dev.prodID;
    size_t size = sizeof devInquiry.inq.dev.prodID;
    cout << "  ";
    void* p_nul = memchr(p, '\0', size);
    cout.write(p, p_nul ? p_nul - p : size);
    cout << '\n';
Which - pretty clearly I'd say - is worse than printf().  You could
try the boost format library, which uses a printf-inspired format
notation but adds type safety and other improvements....
Cheers,
Tony

Alright!- Hide quoted text -

- Show quoted text -

I did this and it seems to be working on 32 and 64 bit machines:


cout.write((char*)static_cast<UCHAR*>(devInquiry.inquiry.devType.productId),
sizeof(devInquiry.inquiry.devType.productId));
 
J

James Kanze

Use ostream::write().

Which will have completely different semantics.
Or, if you really want the operator<< notation,
construct a string from your unsigned char array.

That's the easiest solution:

std::cout << std::string(devInquiry.inq.dev.prodId,
devInquiry.inq.dev.prodId +

sizeof(devInquiry.inq.dev.prodId)).c_str()
<< '\n';

But I think it's still a workaround for bad initial design
(which he may not be able to change now): if
devInquiry.inq.dev,prodId is a null terminated C style string,
it should be char, not unsigned char, and it should be null
terminated. And if it's not, then using printf and "%s" to
output it is wrong.
 
J

James Kanze

[...]
printf(" %.*s\n", sizeof(devInquiry.inq.dev.prodID),
devInquiry.inq.dev.prodID);
cout << ?????
...
unsigned char prodID[16];

[...]
I did this and it seems to be working on 32 and 64 bit machines:
cout.write((char*)static_cast<UCHAR*>(devInquiry.inquiry.devType.productId),
sizeof(devInquiry.inquiry.devType.productId));

Except that this doesn't have the same semantics as your
original code.
 
J

Jorgen Grahn

How can this C statement be converted to C++?



printf(" %.*s\n", sizeof(devInquiry.inq.dev.prodID),
devInquiry.inq.dev.prodID);

cout << ?????


In case, someone is wondering, what is prodID. It is member of
structure, e.g, as shown;


struct DEV_INQ
{
// data members
...
unsigned char prodID[16];
// some more stuff here
...
}DEVINQ;

Make prodID a class instead of an array, and provide an
ostream << ProdId. Then you can implement the formatting
*once* instead of every time you want to print it.

I sometimes think the ostream width/padding/etc formatting
manipulators are a red herring. ostream << Foo works best when it's up
to Foo to format itself, exactly as you want it.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top