Solved

problem with sscanf ??

Posted on 2004-10-13
6
396 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 175 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Assisted Solution

by:Mistobaan
Mistobaan earned 25 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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