Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C to C++ statement conversion

Reply
Thread Tools

C to C++ statement conversion

 
 
CPlusPlus
Guest
Posts: n/a
 
      04-08-2010
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
 
Reply With Quote
 
 
 
 
Jonathan Lee
Guest
Posts: n/a
 
      04-08-2010
On Apr 8, 2:03*pm, CPlusPlus <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-08-2010
On 04/ 9/10 06:03 AM, CPlusPlus wrote:

> 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?

--
Ian Collins
 
Reply With Quote
 
CPlusPlus
Guest
Posts: n/a
 
      04-09-2010
On Apr 8, 5:27*pm, Ian Collins <(E-Mail Removed)> wrote:
> On 04/ 9/10 06:03 AM, CPlusPlus wrote:
>
> > 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?
>
> --
> Ian Collins


Umm w/o the bug
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-09-2010
On 04/ 9/10 01:22 PM, CPlusPlus wrote:
> On Apr 8, 5:27 pm, Ian Collins<(E-Mail Removed)> wrote:
>> On 04/ 9/10 06:03 AM, CPlusPlus wrote:
>>
>>> 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?

>
> Umm w/o the bug


In that case, the answer's your compiler! printf is legal C++...

--
Ian Collins
 
Reply With Quote
 
tonydee
Guest
Posts: n/a
 
      04-09-2010
On Apr 9, 3:03*am, CPlusPlus <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
CPlusPlus
Guest
Posts: n/a
 
      04-09-2010
On Apr 8, 11:43*pm, tonydee <(E-Mail Removed)> wrote:
> On Apr 9, 3:03*am, CPlusPlus <(E-Mail Removed)> wrote:
>
> > 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!
 
Reply With Quote
 
CPlusPlus
Guest
Posts: n/a
 
      04-09-2010
On Apr 9, 8:44*am, CPlusPlus <(E-Mail Removed)> wrote:
> On Apr 8, 11:43*pm, tonydee <(E-Mail Removed)> wrote:
>
>
>
>
>
> > On Apr 9, 3:03*am, CPlusPlus <(E-Mail Removed)> wrote:

>
> > > 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.i nquiry.devType.productId),
sizeof(devInquiry.inquiry.devType.productId));

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-09-2010
On Apr 8, 7:12 pm, Jonathan Lee <(E-Mail Removed)> wrote:
> On Apr 8, 2:03 pm, CPlusPlus <(E-Mail Removed)> wrote:


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


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


> 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.

--
James Kanze
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-09-2010
On Apr 9, 1:47 pm, CPlusPlus <(E-Mail Removed)> wrote:
> On Apr 9, 8:44 am, CPlusPlus <(E-Mail Removed)> wrote:
> > On Apr 8, 11:43 pm, tonydee <(E-Mail Removed)> wrote:


[...]
> > > > 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.i nquiry.devType.productId),
> sizeof(devInquiry.inquiry.devType.productId));


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

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com C Programming 21 08-02-2005 09:24 AM
exec "statement" VS. exec "statement in globals(), locals() Ted Python 1 07-22-2004 08:51 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM



Advertisments