string related question

A

aki

Hello ,

i have hell lot no of ways to do this, but all in vain.
let me tell what exactly i want to do .
I have a function int reportError(char *errorFormatString,

unsigned char *GT1_p,

unsigned int GT1_length,

unsigned char *GT2_p,

unsigned int GT2_length)
During execution , using gdb i can see its arguments values as :

reportError (errorFormatString=0x2b5d309c4140 "Failed to extract
NDC from GT %s to prefix %s",
GT1_p=0x612b5e "Dy\003\t2\020ÿÿ\a", GT1_length=8,
GT2_p=0x7fff8d26f030 "I\231\020", GT2_length=9)

Using gdb i can see
Value at GT2 address ie GT2_p

(gdb) x /3 0x7fff8d26f030
0x7fff8d26f030: 0x00109949 0x40302200 0x1f78d30f
So it shows the value is 49991000002230400f (byte swapped and lenght
is 9 ie 18 bytes or 18 characters)

Value at GT1 address ie GT1_p
(gdb) x /3 0x612b5e
0x612b5e: 0x09037944 0xffff1032 0x00000007
So it shows the value is 447903093210ffff (byte swapped and lenght
is 8 ie 16 bytes or 16 characters)


I want to store GT2 value 49991000002230400f and GT1 value
447903093210f in errorFormatString .
as below
"Failed to extract NDC from GT 447903093210 to prefix
49991000002230400f "

Can somebody suggest a way to do this .


Thanks in Advance.

Aki
 
J

Jens Thoms Toerring

aki said:
i have hell lot no of ways to do this, but all in vain.
let me tell what exactly i want to do .
I have a function
int reportError(char *errorFormatString,
unsigned char *GT1_p,
unsigned int GT1_length,
unsigned char *GT2_p,
unsigned int GT2_length)
During execution , using gdb i can see its arguments values as :
reportError (errorFormatString=0x2b5d309c4140 "Failed to extract
NDC from GT %s to prefix %s",
GT1_p=0x612b5e "Dy\003\t2\020ÿÿ\a", GT1_length=8,
GT2_p=0x7fff8d26f030 "I\231\020", GT2_length=9)
Using gdb i can see
Value at GT2 address ie GT2_p
(gdb) x /3 0x7fff8d26f030
0x7fff8d26f030: 0x00109949 0x40302200 0x1f78d30f
So it shows the value is 49991000002230400f (byte swapped and lenght
is 9 ie 18 bytes or 18 characters)

So, obviously, GT2_p is not a string - otherwise it would have
to end in a '\0' character. And how do you deduce that it's
18 characters or bytes? Unless the value of GPT2_length is
something other than what it seems to mean GPT2_p points to
an array of 9 bytes. It's just that to print out the value
of a byte in hex you need 2 chars per byte, one for the
upper nibble and one for the lower nibble (assuming that
a byte has 8 bits).
Value at GT1 address ie GT1_p
(gdb) x /3 0x612b5e
0x612b5e: 0x09037944 0xffff1032 0x00000007
So it shows the value is 447903093210ffff (byte swapped and lenght
is 8 ie 16 bytes or 16 characters)
I want to store GT2 value 49991000002230400f and GT1 value
447903093210f in errorFormatString .

I guess you meant "want to print out ... using errorFormatString".
as below
"Failed to extract NDC from GT 447903093210 to prefix
49991000002230400f "
Can somebody suggest a way to do this .

You can't do it with any of the *printf() functions
directly. Using the '%s' format specifier won'et do
it at all, it's meant for strings - but you obviously
didn't got a string. And there are no format specifiers
for printing out complete arrays (especially if you
want to shuffle their elements around before printing).
What you need to do is to convert the elements of the
array into a string that you then can print out using
'%s'.

So, if you want to use 'errorFormatString' as it is you
need to make a string out of the data pointed to by GPT1_p
and GPT2_p, using an extra function for that purpose.
Here's one way that would do the job (taking into account
that for some strange reason you seem to want to reverse
sets of 4 bytes, if not just drop all the stuff involving
'j'):

char *to_scrambled_hex( unsigned char *p, unsigned len )
{
char *s = malloc( 2 * len + 1 );
unsigned int i,
int j = 3;

for ( i = 0; s != NULL && i < len; i++ )
{
sprintf( s + 2 * i, "%02x", p[ i + j ] );
if ( ( j -= 2 ) < -3 )
j = 3;
}
}
return s;
}

Now, if you want to print using 'errorFormatString' use:

char s1 = to_scrambled_hex( GPT1_p, GPT1_len );
char s2 = to_scrambled_hex( GPT2_p, GPT2_len );
printf( errorFormatString, s1 ? s1 : "(can't print)",
s2 ? s2 : "(can't print)" );
free( s1 );
free( s2 );

Note that both 's1' and 's2' could be NULL if memory
allocation failed in the function for converting to
a string.
Regards, Jens
 
A

aki

aki said:
i have hell lot no of ways to do this, but all in vain.
let me tell what exactly i want to do .
I have a function
int reportError(char *errorFormatString,
unsigned char    *GT1_p,
unsigned int     GT1_length,
unsigned char    *GT2_p,
unsigned int      GT2_length)
During execution , using gdb i can see  its arguments values as :
    reportError (errorFormatString=0x2b5d309c4140 "Failed to extract
NDC from GT %s to prefix %s",
    GT1_p=0x612b5e "Dy\003\t2\020ÿÿ\a", GT1_length=8,
GT2_p=0x7fff8d26f030 "I\231\020", GT2_length=9)
Using gdb i can see
Value at  GT2 address ie GT2_p
(gdb) x /3 0x7fff8d26f030
0x7fff8d26f030: 0x00109949      0x40302200      0x1f78d30f
So it shows the  value is 49991000002230400f (byte swapped and lenght
is 9 ie 18 bytes  or 18 characters)

So, obviously, GT2_p is not a string - otherwise it would have
to end in a '\0' character. And how do you deduce that it's
18 characters or bytes? Unless the value of GPT2_length is
something other than what it seems to mean GPT2_p points to
an array of 9 bytes. It's just that to print out the value
of a byte in hex you need 2 chars per byte, one for the
upper nibble and one for the lower nibble (assuming that
a byte has 8 bits).
Value at  GT1 address ie GT1_p
(gdb) x /3 0x612b5e
0x612b5e:       0x09037944      0xffff1032      0x00000007
So it shows the  value is 447903093210ffff  (byte swapped and lenght
is 8 ie 16 bytes  or 16 characters)
I want to store  GT2 value 49991000002230400f  and GT1 value
447903093210f  in errorFormatString .

I guess you meant "want to print out ... using errorFormatString".
as below
"Failed to extract NDC from GT 447903093210 to prefix
49991000002230400f  "
Can somebody suggest a way to do this .

You can't do it with any of the *printf() functions
directly. Using the '%s' format specifier won'et do
it at all, it's meant for strings - but you obviously
didn't got a string. And there are no format specifiers
for printing out complete arrays (especially if you
want to shuffle their elements around before printing).
What you need to do is to convert the elements of the
array into a string that you then can print out using
'%s'.

So, if you want to use 'errorFormatString' as it is you
need to make a string out of the data pointed to by GPT1_p
and GPT2_p, using an extra function for that purpose.
Here's one way that would do the job (taking into account
that for some strange reason you seem to want to reverse
sets of 4 bytes, if not just drop all the stuff involving
'j'):

char *to_scrambled_hex( unsigned char *p, unsigned len )
{
    char *s = malloc( 2 * len + 1 );
    unsigned int i,
    int j = 3;

    for ( i = 0; s != NULL && i < len; i++ )
    {
        sprintf( s + 2 * i, "%02x", p[ i + j ] );
        if ( ( j -= 2 ) < -3 )
            j = 3;
        }
    }
    return s;

}

Now, if you want to print using 'errorFormatString' use:

char s1 = to_scrambled_hex( GPT1_p, GPT1_len );
char s2 = to_scrambled_hex( GPT2_p, GPT2_len );
printf( errorFormatString, s1 ? s1 : "(can't print)",
        s2 ? s2 : "(can't print)" );
free( s1 );
free( s2 );

Note that both 's1' and 's2' could be NULL if memory
allocation failed in the function for converting to
a string.
                        Regards, Jens


Thanks a lot Jens !!!

for understanding my problem and educating me .
The reason why it was not working before is now clear to me.
It working fine now

Regards
Aki
 
A

aki

aki said:
i have hell lot no of ways to do this, but all in vain.
let me tell what exactly i want to do .
I have a function
int reportError(char *errorFormatString,
unsigned char    *GT1_p,
unsigned int     GT1_length,
unsigned char    *GT2_p,
unsigned int      GT2_length)
During execution , using gdb i can see  its arguments values as :
    reportError (errorFormatString=0x2b5d309c4140 "Failed to extract
NDC from GT %s to prefix %s",
    GT1_p=0x612b5e "Dy\003\t2\020ÿÿ\a", GT1_length=8,
GT2_p=0x7fff8d26f030 "I\231\020", GT2_length=9)
Using gdb i can see
Value at  GT2 address ie GT2_p
(gdb) x /3 0x7fff8d26f030
0x7fff8d26f030: 0x00109949      0x40302200      0x1f78d30f
So it shows the  value is 49991000002230400f (byte swapped and lenght
is 9 ie 18 bytes  or 18 characters)

So, obviously, GT2_p is not a string - otherwise it would have
to end in a '\0' character. And how do you deduce that it's
18 characters or bytes? Unless the value of GPT2_length is
something other than what it seems to mean GPT2_p points to
an array of 9 bytes. It's just that to print out the value
of a byte in hex you need 2 chars per byte, one for the
upper nibble and one for the lower nibble (assuming that
a byte has 8 bits).
Value at  GT1 address ie GT1_p
(gdb) x /3 0x612b5e
0x612b5e:       0x09037944      0xffff1032      0x00000007
So it shows the  value is 447903093210ffff  (byte swapped and lenght
is 8 ie 16 bytes  or 16 characters)
I want to store  GT2 value 49991000002230400f  and GT1 value
447903093210f  in errorFormatString .

I guess you meant "want to print out ... using errorFormatString".
as below
"Failed to extract NDC from GT 447903093210 to prefix
49991000002230400f  "
Can somebody suggest a way to do this .

You can't do it with any of the *printf() functions
directly. Using the '%s' format specifier won'et do
it at all, it's meant for strings - but you obviously
didn't got a string. And there are no format specifiers
for printing out complete arrays (especially if you
want to shuffle their elements around before printing).
What you need to do is to convert the elements of the
array into a string that you then can print out using
'%s'.

So, if you want to use 'errorFormatString' as it is you
need to make a string out of the data pointed to by GPT1_p
and GPT2_p, using an extra function for that purpose.
Here's one way that would do the job (taking into account
that for some strange reason you seem to want to reverse
sets of 4 bytes, if not just drop all the stuff involving
'j'):

char *to_scrambled_hex( unsigned char *p, unsigned len )
{
    char *s = malloc( 2 * len + 1 );
    unsigned int i,
    int j = 3;

    for ( i = 0; s != NULL && i < len; i++ )
    {
        sprintf( s + 2 * i, "%02x", p[ i + j ] );
        if ( ( j -= 2 ) < -3 )
            j = 3;
        }
    }
    return s;

}

Now, if you want to print using 'errorFormatString' use:

char s1 = to_scrambled_hex( GPT1_p, GPT1_len );
char s2 = to_scrambled_hex( GPT2_p, GPT2_len );
printf( errorFormatString, s1 ? s1 : "(can't print)",
        s2 ? s2 : "(can't print)" );
free( s1 );
free( s2 );

Note that both 's1' and 's2' could be NULL if memory
allocation failed in the function for converting to
a string.
                        Regards, Jens


Thanks a lot Jens !!!

for understanding my problem and educating me .
The reason why it was not working before is now clear to me.
It working fine now

Regards
Aki
 

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

Similar Threads


Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top