473,388 Members | 1,322 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 4216
In article <11**********************@o13g2000cwo.googlegroups .com>,
<sm********@gmail.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**********************@o13g2000cwo.googlegroups .com>,
<sm********@gmail.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**********************@l41g2000cwc.googlegroups .com>,
<sm********@gmail.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********@gmail.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
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
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"; ........
3
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...
7
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...
10
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
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...
5
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...
4
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...
16
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...

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.