Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

problem with sscanf ??

Posted on 2004-10-13
6
Medium Priority
?
403 Views
Last Modified: 2010-04-01
Hello,
I made the following code:
///////
#define HPRINT2(temp) {printf("\n");\
for (int i=0;i<strlen((const char *)temp);i++)\
{printf("%02x",(temp[i]));}printf("\n");}
////////
      char * clchal="ffffff0011223344";
      char * chal="0123456789abcdef";
      char tem4[100]={0x00};
      strcat(tem4,chal);
      char * chcode=strcat(tem4,clchal);//->0123456789abcdefffffff0011223344
      printf(chcode);//until here it's correct.

      char texta[33]={0x00};//2*2*8+1 (zero-termination)
       for (i=0;i<32;++i) {sscanf(&chcode[i*2],"%02x",&texta[i]);}
      printf("\n");
      HPRINT2(texta);//->01234567ffffff89ffffffabffffffcdffffffefffffffffffffffffffffffff
////////

I really can't see what is not correct, since I tried the sscanf-part before in a simple demo program, and there it worked fine....

Any help is really appreciated.
0
Comment
Question by:plinius
  • 2
  • 2
  • 2
6 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 12301218
You seem to be getting sign-extension on all the bytes that have the high bit set (89, ab, cd, ef ).

You might try saying "unsigned char" in front  of "temp[i]" in your printf().

0
 

Author Comment

by:plinius
ID: 12301292
Thanks, it seems better now,
however, it displays
0123456789abcdefffffff0011223344//->should be
0123456789abcdefffffff//->what is printed by HPRINT2
I Think that it reads the 0x00 as a "stop" (though not sure...).... Is there any way to avoid this??

Thanks
0
 
LVL 22

Accepted Solution

by:
grg99 earned 700 total points
ID: 12301839
You can't do a strlen() on texta, it's not a string at all.  

You have to find some other way of passing the length.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Assisted Solution

by:Mistobaan
Mistobaan earned 100 total points
ID: 12302151
I did this simple prog to test your code :


#include <strings.h>
#include <stdio.h>

#define HPRINT2(temp) {printf("\n");\
for (int i=0;i<strlen((const char *)temp);i++)\
{printf("%02x",(temp[i]));}printf("\n");}
////////
int main(){
     char * clchal="ffffff0011223344";
     char * chal="0123456789abcdef";
     char tem4[100]={0x00};
     strcat(tem4,chal);
     char * chcode=strcat(tem4,clchal);//->0123456789abcdefffffff0011223344
     printf(chcode);//until here it's correct.

     char texta[33]={0x00};//2*2*8+1 (zero-termination)
      for (int i=0;i<32;++i) {sscanf(&chcode[i*2],"%02x",&texta[i]);}
     printf("\n");
     HPRINT2(texta);//->01234567ffffff89ffffffabffffffcdffffffefffffffffffffffffffffffff
     return 0;
}


It outputs
>a.out
0123456789abcdefffffff0011223344

01234567ffffff89ffffffabffffffcdffffffefffffffffffffffffffffffff

----------------end
What is wrong?

0
 

Author Comment

by:plinius
ID: 12302306
"
You have to find some other way of passing the length.
"
->that did it!! Thanks.
(I guess it was a dumb fault...)
0
 
LVL 1

Expert Comment

by:Mistobaan
ID: 12302903
tnx :D
( my first 100 points )
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question