Solved

problem with sscanf ??

Posted on 2004-10-13
6
398 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

Independent Software Vendors: 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!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

615 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