Solved

problem with sscanf ??

Posted on 2004-10-13
6
390 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Assisted Solution

by:Mistobaan
Mistobaan earned 25 total points
Comment Utility
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
Comment Utility
"
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
Comment Utility
tnx :D
( my first 100 points )
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now