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
Solved

problem with sscanf ??

Posted on 2004-10-13
6
395 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 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

808 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