473,769 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Nov 14 '05 #1
4 4258
In article <11************ **********@o13g 2000cwo.googleg roups.com>,
<sm********@gma il.com> wrote:
: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

That doesn't look like a string to me. A string would start with
a double-quote, and would make explicit whether there are newline
characters. e.g., "0x27 0x00\n0x30 0x00\n0x33 0x00" and so on.

: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.

strtoul() specifying a base of 16, and feeding the modified pointer
back in as the new initial pointer value. Look carefully at the error
conditions in the stroul() documentation -- different conditions get
signalled different ways.

You will have to decide what you want to do under various circumstances,
such as if there is only one value per line instead of two, or if
there is more than 2 values on one line, or if a string not interpretable
as hex shows up, or if a huge number shows up...
--
I don't know if there's destiny,
but there's a decision! -- Wim Wenders (WoD)
Nov 14 '05 #2
Hi Walter,

Thank you very much for your response. However I still am a little
doubtful. I would like to explain in details.

There is a plain text file with the following content consisting of hex
values

0xA8 0x00 0x00 0x00 0x00
0x94 0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00 0x00
0x14 0x00 0x00
0x28 0x00 0x00 0x00 0x00
0x3C

I am reading this file to a (char[]) buffer using f_op->read. I am
working in linux kernel module so standard fscanf dosent work. As you
can see that number of colums vary.

Can I do some kind of automatic parsing that will enable me to get the
hex values into an array of integer?

shake

Walter Roberson wrote:
In article <11************ **********@o13g 2000cwo.googleg roups.com>,
<sm********@gma il.com> wrote:
: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

That doesn't look like a string to me. A string would start with
a double-quote, and would make explicit whether there are newline
characters. e.g., "0x27 0x00\n0x30 0x00\n0x33 0x00" and so on.

: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.

strtoul() specifying a base of 16, and feeding the modified pointer
back in as the new initial pointer value. Look carefully at the error
conditions in the stroul() documentation -- different conditions get
signalled different ways.

You will have to decide what you want to do under various circumstances, such as if there is only one value per line instead of two, or if
there is more than 2 values on one line, or if a string not interpretable as hex shows up, or if a huge number shows up...
--
I don't know if there's destiny,
but there's a decision! -- Wim Wenders (WoD)


Nov 14 '05 #3
In article <11************ **********@l41g 2000cwc.googleg roups.com>,
<sm********@gma il.com> wrote:
:Thank you very much for your response. However I still am a little
:doubtful. I would like to explain in details.

:There is a plain text file with the following content consisting of hex
:values

:0xA8 0x00 0x00 0x00 0x00
:0x94 0x00 0x00 0x00 0x00
:0x00 0x00 0x00 0x00 0x00
:0x14 0x00 0x00
:0x28 0x00 0x00 0x00 0x00
:0x3C

:I am reading this file to a (char[]) buffer

The whole file into one array?

:using f_op->read.

That doesn't sound like a C routine -- that sounds like a device
driver.

:I am
:working in linux kernel module so standard fscanf dosent work.

Sorry, I haven't worked in the linux kernel so I don't know what
is or isn't available to the kernel.

:As you
:can see that number of colums vary.

Is the whitespace significant? e.g., in the 4th line in your example,
does the fact of there being only 3 columns signify anything like
logical end of line?

:Can I do some kind of automatic parsing that will enable me to get the
:hex values into an array of integer?

I don't know what you mean by "automatic parsing". What are you
looking for -- a magic format string for sscanf() that will parse it
all in one go? You can't do that with any standard sscanf(): sscanf
can only return one value per explicitly named destination location,
so if you want to be able to return into successive elements of an
array, you would have to name all those successive elements...

sscanf( string, "%i%i%i%i", &a[0], &a[1], &a[2], &a[3] ) and NOT
sscanf( string, "%i%i%i%i" a ) expecting a[0] thru a[3] to be filled in.
The subject line of your original posting referred to vsscanf(). That
only passes one set of destination arguments in at the call itself, but
the entire list of arguments still has to be passed in through
a va_args construct, so either you would still have to code all the
destinations as a list, or you would need a loop to construct a list
with all the destination addresses [which would require you knew how
many there were...]

Speak of knowing how many there are: are we given a maximum number
of values? Are we to assume that we've been passed a user-space
buffer that is "big enough" (buffer overflow!!) or are you expecting
the routine to do kernel-level malloc() and pass back the complete
set of values? If you are expecting the routine to allocate all the
memory, then you have to worry about the efficiency of expanding the
size of the allocated string as you encounter more and more input...
There is, by the way, no way to specify for sscanf() and kin the
equivilent of the Fortran '(' ')' format repetition specifier --
you can't write, for example, sscanf( string, "(%i)", a ) and expect
it to keep plopping values into successive positions in a, reusing
the %i format each time.
In short... considering all these constraints, you're probably best
off coding your own little loop. You may wish to use a small
state machine implimentation:

state 0: /* not in a number */
space, tab, newline, or other whitespace: stay in state 0
'0': transition to state 1
EOF: done, do whatever you need to with the values and go on with life
other: error -- what will you do now?

state 1: /* leading 0 seen */
'x' or 'X': transition to state 2
EOF: Maybe record the value 0, if 0 by itself can stand in for 0x00.
If so, after recording the 0 you are done, so go on with life
If not, error -- what will you do now?
space, tab, newline, or other whitespace: maybe record the value 0,
If 0 by itself can stand in for 0x00.
If so, after recording the 0, transition to state 0.
If not, error -- what will you do now?
other: error -- what will you do now?

state 2: /* leading 0x seen */
EOF: Maybe record the value 0, if 0x by itself can stand in for 0x00.
If so, after recording the 0 you are done, so go on with life
If not, error -- what will you do now?
space, tab, newline, or other whitespace: maybe record the value 0,
If 0x by itself can stand in for 0x00.
If so, after recording the 0, transition to state 0.
If not, error -- what will you do now?
'0' - '9': save input character minus '0' into d1, then transition to
state 3
'A' - 'F': save ((input character minus 'A') plus 10) into d1, then
transition to state 3
'a' - 'f': save ((input character minus 'a') plus 10) into d1, then
transition to state 3
other: error -- what will you do now?

state 3: /* 0x\d seen */
EOF: maybe record the value d1, if 0x followed by a single hex digit
can stand in for 0x0 followed by that digit.
If so, after recording the value d1, you are done, so go on with life
If not, error -- what will you do now?
space, tab, newline, or other whitespace: maybe record the value d1, if
0x followed by a single hex digit can stand in for 0x0 followed
by that digit.
If so, after recording the value d1, transition to state 0
If not, error -- what will you do now?
'0' - '9': set v = d1 * 16 + (input character minus '0'), then
transition to state 4
'A' - 'F': set v = d1 * 16 + ((input character minus 'A') plus 10),
then transition to state 4
'a' - 'f': set v = d1 * 16 + ((input character minus 'a') plus 10),
then transition to state 4
other: error -- what will you do now?

state 4: /* 0x\d\d seen */
EOF: record the value v, then you are done so go on with life
space, tab, newline, or other whitespace: record the value v, then
transition to state 0
'0' - '9', 'a' - 'f', 'A' - 'F': error, number too long -- what will
you do now?
other: error -- what will you do now?
--
Warning: potentially contains traces of nuts.
Nov 14 '05 #4
sm********@gmai l.com wrote:
There is a plain text file with the following content consisting of hex
values

0xA8 0x00 0x00 0x00 0x00
0x94 0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00 0x00
0x14 0x00 0x00
0x28 0x00 0x00 0x00 0x00
0x3C

I am reading this file to a (char[]) buffer using f_op->read. I am
working in linux kernel module so standard fscanf dosent work.


In that case, you probably want specialist advice, since there may be
more missing than just fscanf(). You can get this advice in a Linux
programming group.

Richard
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
7618
by: j0mbolar | last post by:
say you have char buf = "string1 string2 string3"; then you want to use sscanf to match "string3" and store it into another array. so: char buf = "string1 string2 string3"; char array;
12
8728
by: Simone Mehta | last post by:
hi All, I am parsing a CSV file. I want to read every row into a char array of reasonable size and then extract strings from it. <snippet> char foo="hello,world,bye,bye,world"; ..... sscanf(foo,"%s%*%s%*%s%*%s%*%s",s1,s2,s3,s4,s5); <snippet/> This is giving me junk .
3
4785
by: whisper | last post by:
Hello: I am trying to write code to read in a bunch of lines from stdin, each containing a (variable) number of integers and writing each integer in a separate line to stdout. I came up the code below. I could not get sscanf to work because it does not increment its position
7
27016
by: nick | last post by:
is it similar to scanf? when i use scanf it can read the words in the screen automatically one after another.i use a char array to store the string,then use sscanf to read the words,but it just only reat out the first word in the string array every time. so if i want to read the words in the string one by one, just like scanf, what should i do? thanks!
10
2986
by: broeisi | last post by:
What advantages does sscanf offer over scanf? I had the following code: #include <stdio.h> #include <string.h> int main(void) { int start, finish, values;
17
3483
by: Yogi_Bear_79 | last post by:
I have the following code: sscanf(line, "%d", n_ptr) !=1 || n_ptr <=0; It only partially works. If the user types a character other than 0-9 to start the string it fails. However as long as the first character is an integer it will allow letters in the following places for Example:
5
3506
by: Alex Mathieu | last post by:
Hi, using sscanf, I'm trying to retrieve something, but nothing seems to work. Here's the pattern: SS%*sþ0þ%6s Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ Actually, I would like to retrieve the "799829" from the data, but it always failed. I thought that the "%*sþ0þ" would work as if I was
4
2516
by: Gaijinco | last post by:
I have always used istringstream for inputting data from a string. But I heard someone talking about vsscanf() However from what I can find in the internet is not quite clear if is standard or not (some say it is, some say it isn't) Also it's very confusing how to use it. I have used the va_ combo before: va_list, va_start(), va_arg(), va_end()
16
2907
by: Chad | last post by:
Given the following #include <stdio.h> int main(void) { char line; long arg1, arg2; while(fgets(line, BUFSIZ, stdin) != NULL){
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.