A Question regarding format specifiers ,

A

aki

Hi ,

i have a function reportError whose functionality i want to enhance
for case when converToAscii fails .

reportError(char *errorFormatString,
unsigned char *GT1_p,
m GT1_length,
unsigned char *GT2_p,
m GT2_length))
{
char errorBuff[128];
/* Extra bytes required as we convert all symbols. */
char asciiGT1[GT_LEN * 2 ];
char asciiGT2[GT_LEN * 2 ];


if(!GT1_p ||
convertToAscii(GT1_p,
GT1_length,
SYMBOLS,
asciiGT1) != SUCCESS)
{
/* Something went wrong - truncate the output. */
asciiGT1[0] = '\0';
}

if(!GT2_p ||
convertToAscii(GT2_p,
GT2_length,
SYMBOLS,
asciiGT2) != SUCCESS)
{
/* Something went wrong - truncate the output. */
asciiGT2[0] = '\0';
}

sprintf(errorBuff,
errorFormatString,
asciiGT1, asciiGT2);


EXCEPTION(errorBuff, FAILURE);
}

During debugging i can see
reportError (errorFormatString=0x2aab9baeb010 "Failed to extract NDC
from GT %s to prefix %s", GT1_p=0x612b5e "Dy\003\t2\020ÿÿ\a",
GT1_length=8, GT2_p=0x7fff22148470 "I\231\020", GT2_length=9)

i cannot change the formatting of errorFormatString as it presently
print value of GT1, GT2 in string formats.
I have written the sane function with some changes as below:
for the error case .


reportError(char *errorFormatString,
unsigned char *GT1_p,
m GT1_length,
unsigned char *GT2_p,
m GT2_length))
{
char errorBuff[128];
/* Extra bytes required as we convert all symbols. */
char asciiGT1[GT_LEN * 2 ];
char asciiGT2[GT_LEN * 2 ];
int flag1=0;flag2=0;

if(!GT1_p ||
convertToAscii(GT1_p,
GT1_length,
SYMBOLS,
asciiGT1) != SUCCESS)
{
/* Something went wrong - truncate the output. */
asciiGT1[0] = '\0';
flag1=1;
}

if(!GT2_p ||
convertToAscii(GT2_p,
GT2_length,
SYMBOLS,
asciiGT2) != SUCCESS)
{
/* Something went wrong - truncate the output. */
asciiGT2[0] = '\0';
flag2=1;
}

if (flag1&& !flag2) //
{
sprintf(errorBuff,
errorFormatString,
GT1_p, asciiGT2);
}

else if (!flag1 && flag2)
{
sprintf(errorBuff,
errorFormatString,
asciiGT1, GT2_p);
}
else if (flag1&& flag2)
{
sprintf(errorBuff,
errorFormatString,
GT1_p, GT2_p); // here i amt getting the values of GT1_p ,
GT2_p in string format

.
else
sprintf(errorBuff,
errorFormatString,
asciiGT1, asciiGT2);


EXCEPTION(errorBuff, FAILURE);
}

But this time i am getting the exception in string format again.
as below shown
(gdb) p errorBuff
$4 = "Failed to extract CCNDC from GT Dy\003\t2\020ÿÿ\a to prefix I
\231\020\000x\203\024\"ÿ\177\000\000\000\000\000\000\000\000\000\0
00\000\000\000\000\000^+a
\000\000\000\000\000\002\000\000\000\000\000\000\000\b\032_
\000\000\000\000\000àµÁ\002\000\000\000\000\216\0
00\000\000\0000\003n\232«*\000"

I want to know is there any way , by which i can get the vallue of
GT1, GT2 in hexdecimal form ???
so that it look nice for a user.

Hope i am good enough for raising my problem!!!

thanks in advance

Aki
 
B

Ben Bacarisse

aki said:

Hi. You have multi-posted this. This is considered a Bad Idea.

I have written the sane function with some changes as below:
for the error case .


reportError(char *errorFormatString,
unsigned char *GT1_p,
m GT1_length,
unsigned char *GT2_p,
m GT2_length))

You have a parameter called "errorFormatString" ("format" would be
enough for me in an error function) and a type called "m"? One is too
long and the other too short.
{
char errorBuff[128];
/* Extra bytes required as we convert all symbols. */
char asciiGT1[GT_LEN * 2 ];
char asciiGT2[GT_LEN * 2 ];
int flag1=0;flag2=0;

if(!GT1_p ||
convertToAscii(GT1_p,
GT1_length,
SYMBOLS,
asciiGT1) != SUCCESS)
{
/* Something went wrong - truncate the output. */
asciiGT1[0] = '\0';
flag1=1;
}

Looks all wrong to me. When GT1_p is null you set flag1.
if(!GT2_p ||
convertToAscii(GT2_p,
GT2_length,
SYMBOLS,
asciiGT2) != SUCCESS)
{
/* Something went wrong - truncate the output. */
asciiGT2[0] = '\0';
flag2=1;
}

Ditto flag2 when GT2_p is null.
if (flag1&& !flag2) //
{
sprintf(errorBuff,
errorFormatString,
GT1_p, asciiGT2);
}

else if (!flag1 && flag2)
{
sprintf(errorBuff,
errorFormatString,
asciiGT1, GT2_p);
}
else if (flag1&& flag2)
{
sprintf(errorBuff,
errorFormatString,
GT1_p, GT2_p); // here i amt getting the values of GT1_p ,
GT2_p in string format

And then you use them.
.
else
sprintf(errorBuff,
errorFormatString,
asciiGT1, asciiGT2);


EXCEPTION(errorBuff, FAILURE);
}

But this time i am getting the exception in string format again.
as below shown
(gdb) p errorBuff
$4 = "Failed to extract CCNDC from GT Dy\003\t2\020ÿÿ\a to prefix I
\231\020\000x\203\024\"ÿ\177\000\000\000\000\000\000\000\000\000\0
00\000\000\000\000\000^+a
\000\000\000\000\000\002\000\000\000\000\000\000\000\b\032_
\000\000\000\000\000àµÃ\002\000\000\000\000\216\0
00\000\000\0000\003n\232«*\000"

I want to know is there any way , by which i can get the vallue of
GT1, GT2 in hexdecimal form ???
so that it look nice for a user.

Hope i am good enough for raising my problem!!!

Possibly. I'd have liked to see more. Specifically, what does
convertToAscii do and at least the call that cause the problem.
Ideally you'd post a minimal compilable test case.

Flag variables are usually a sign that things have gone wrong. If you
must have them, don't call then flag1 and flag2 -- say what they mean.

The whole scheme (flags based on tests which then get testing in
combination) is prone to bugs. I suggest you go at it again.
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top