Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 7th, 2005, 6:53 PM   #1
smshahriar
Newbie
 
Join Date: Feb 2005
Posts: 1
Rep Power: 0 smshahriar is on a distinguished road
sscanf or vsscanf : want to scan a list of integers in c

Hi,

I want to scan from the following string all the hex numbers and
populate an array of integers:

0x27 0x00
0x30 0x00
0x33 0x00
0x36 0x00

...

How can I do so? Doing sscanf with a long list of "%x %x ..... %x" will
do it. But is there a better way such as a loop etc.

Can someone please help.

Thanks,

Shake
smshahriar is offline   Reply With Quote
Old Feb 8th, 2005, 3:30 PM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
Here's a nifty solution.
#include <stdio.h>

int main ( void )
{
  char *v = "0 1 2 3 4 5 6 7 8 9";
  int i, x, n;

  for ( i = 0; sscanf ( v + i, "%d%n", &x, &n ) == 1; i += n )
    printf("%d ", x);
  printf("\n");

  return 0;
}
Eggbert is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC