469,903 Members | 2,316 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

sscanf in c#

AMP
Hello,
Anybody know if anything exists like sscanf in c.
I found a few things OL but most were pretty old. Maybe something has
come along since 2004?
Thanks
Mike

Sep 18 '06 #1
20 20760

"AMP" <am******@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
Hello,
Anybody know if anything exists like sscanf in c.
I found a few things OL but most were pretty old. Maybe something has
come along since 2004?
Thanks
Mike
Yup, regular expressions :) Regex library.

HTH,
Mythran
Sep 18 '06 #2

"AMP" <am******@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
| Hello,
| Anybody know if anything exists like sscanf in c.
| I found a few things OL but most were pretty old. Maybe something has
| come along since 2004?
| Thanks
| Mike
|

Search the doc's for String.Format, this is the closest you can get.

Willy.

Sep 18 '06 #3

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:en**************@TK2MSFTNGP05.phx.gbl...
>
"AMP" <am******@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
| Hello,
| Anybody know if anything exists like sscanf in c.
| I found a few things OL but most were pretty old. Maybe something has
| come along since 2004?
| Thanks
| Mike
|

Search the doc's for String.Format, this is the closest you can get.

Willy.
If I remember correctly, sscanf doesn't format output, it reads input from a
pre-formatted string. For that, you can use regular expressions.

HTH,
Mythran
Sep 18 '06 #4

"Mythran" <ki********@hotmail.comwrote in message
news:Ow**************@TK2MSFTNGP03.phx.gbl...
|
| "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
| news:en**************@TK2MSFTNGP05.phx.gbl...
| >
| "AMP" <am******@gmail.comwrote in message
| news:11**********************@k70g2000cwa.googlegr oups.com...
| | Hello,
| | Anybody know if anything exists like sscanf in c.
| | I found a few things OL but most were pretty old. Maybe something has
| | come along since 2004?
| | Thanks
| | Mike
| |
| >
| Search the doc's for String.Format, this is the closest you can get.
| >
| Willy.
| >
| >
| >
|
| If I remember correctly, sscanf doesn't format output, it reads input from
a
| pre-formatted string. For that, you can use regular expressions.
|
| HTH,
| Mythran
|
|

Sorry I misread the OP's post, was thinking of scanf, anyway you don't need
regex.

Willy.
Sep 18 '06 #5
Mythran wrote:
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:en**************@TK2MSFTNGP05.phx.gbl...

"AMP" <am******@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
| Hello,
| Anybody know if anything exists like sscanf in c.
| I found a few things OL but most were pretty old. Maybe something has
| come along since 2004?
| Thanks
| Mike
|

Search the doc's for String.Format, this is the closest you can get.

Willy.


If I remember correctly, sscanf doesn't format output, it reads input from a
pre-formatted string. For that, you can use regular expressions.
You can also use a StringReader

Sep 18 '06 #6

"AMP" <am******@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
| Hello,
| Anybody know if anything exists like sscanf in c.
| I found a few things OL but most were pretty old. Maybe something has
| come along since 2004?
| Thanks
| Mike
|

Ignore my previous post.
Using Parse and TryParse methods you can achieve the same (and more) results
as sscanf in C.
Consider following sample....

static void Main()
{
string tokenString = "12 25 56 4";
string [] split = tokenString.Split(new Char [] {' '});
int Int32Val;
char charVal;
float floatVal;
bool result = Int32.TryParse(split[0], NumberStyles.Integer, null, out
Int32Val);
if(result)
Console.WriteLine(Int32Val);
result = Char.TryParse(split[1][0].ToString(), out charVal);
if(result)
Console.WriteLine(charVal);
result = Single.TryParse(split[2], NumberStyles.Float, null, out
floatVal);
if(result)
Console.WriteLine("{0:f}", floatVal);
}
This should output:

12
2
56,00

Willy.


Sep 18 '06 #7

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:eQ**************@TK2MSFTNGP05.phx.gbl...
>
"AMP" <am******@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
| Hello,
| Anybody know if anything exists like sscanf in c.
| I found a few things OL but most were pretty old. Maybe something has
| come along since 2004?
| Thanks
| Mike
|

Ignore my previous post.
Using Parse and TryParse methods you can achieve the same (and more)
results
as sscanf in C.
Consider following sample....

static void Main()
{
string tokenString = "12 25 56 4";
string [] split = tokenString.Split(new Char [] {' '});
int Int32Val;
char charVal;
float floatVal;
bool result = Int32.TryParse(split[0], NumberStyles.Integer, null,
out
Int32Val);
if(result)
Console.WriteLine(Int32Val);
result = Char.TryParse(split[1][0].ToString(), out charVal);
if(result)
Console.WriteLine(charVal);
result = Single.TryParse(split[2], NumberStyles.Float, null, out
floatVal);
if(result)
Console.WriteLine("{0:f}", floatVal);
}
This should output:

12
2
56,00

Willy.

Well, suppose you have the following string:

"Item #1: $32.53 Item #2: $32.54 Sub-Total: $65.07"

With sscanf, I believe you can do something like:
sscanf(buff, "Item #1: $%d Item #2: $%d Sub-Total: $%d", value1, value2,
value3);
printf("Sub-Total: %d", value3);
Using regex, you can do something similar...and parsing out yourself would
be more trickier...

HTH,
Mythran
Sep 18 '06 #8

"Mythran" <ki********@hotmail.comwrote in message
news:uS**************@TK2MSFTNGP06.phx.gbl...
|
| "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
| news:eQ**************@TK2MSFTNGP05.phx.gbl...
| >
| "AMP" <am******@gmail.comwrote in message
| news:11**********************@k70g2000cwa.googlegr oups.com...
| | Hello,
| | Anybody know if anything exists like sscanf in c.
| | I found a few things OL but most were pretty old. Maybe something has
| | come along since 2004?
| | Thanks
| | Mike
| |
| >
| Ignore my previous post.
| Using Parse and TryParse methods you can achieve the same (and more)
| results
| as sscanf in C.
| Consider following sample....
| >
| static void Main()
| {
| string tokenString = "12 25 56 4";
| string [] split = tokenString.Split(new Char [] {' '});
| int Int32Val;
| char charVal;
| float floatVal;
| bool result = Int32.TryParse(split[0], NumberStyles.Integer, null,
| out
| Int32Val);
| if(result)
| Console.WriteLine(Int32Val);
| result = Char.TryParse(split[1][0].ToString(), out charVal);
| if(result)
| Console.WriteLine(charVal);
| result = Single.TryParse(split[2], NumberStyles.Float, null, out
| floatVal);
| if(result)
| Console.WriteLine("{0:f}", floatVal);
| }
| >
| >
| This should output:
| >
| 12
| 2
| 56,00
| >
| Willy.
| >
| >
| >
| >
|
| Well, suppose you have the following string:
|
| "Item #1: $32.53 Item #2: $32.54 Sub-Total: $65.07"
|
| With sscanf, I believe you can do something like:
|
|
| sscanf(buff, "Item #1: $%d Item #2: $%d Sub-Total: $%d", value1, value2,
| value3);
| printf("Sub-Total: %d", value3);
|
|
| Using regex, you can do something similar...and parsing out yourself would
| be more trickier...
|
| HTH,
| Mythran
|

I prefer using TryParse over RegEx, just a matter of taste, and quite faster
;-)

// suppose the current culture is en-US...
Decimal decVal;
string tokenString = "Item #1: $32.53 Item #2: $32.54 Sub-Total:
$65.07";
string [] split = tokenString.Split(new Char [] {' '});
bool result = Decimal.TryParse(split[7], NumberStyles.Currency, null,
out decVal);
if(result)
Console.WriteLine("Sub-total: {0:c}", decVal);

Not really tricky IMO.

Willy.
Sep 18 '06 #9

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:Ow****************@TK2MSFTNGP02.phx.gbl...
>
"Mythran" <ki********@hotmail.comwrote in message
news:uS**************@TK2MSFTNGP06.phx.gbl...
|
| "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
| news:eQ**************@TK2MSFTNGP05.phx.gbl...
| >
| "AMP" <am******@gmail.comwrote in message
| news:11**********************@k70g2000cwa.googlegr oups.com...
| | Hello,
| | Anybody know if anything exists like sscanf in c.
| | I found a few things OL but most were pretty old. Maybe something
has
| | come along since 2004?
| | Thanks
| | Mike
| |
| >
| Ignore my previous post.
| Using Parse and TryParse methods you can achieve the same (and more)
| results
| as sscanf in C.
| Consider following sample....
| >
| static void Main()
| {
| string tokenString = "12 25 56 4";
| string [] split = tokenString.Split(new Char [] {' '});
| int Int32Val;
| char charVal;
| float floatVal;
| bool result = Int32.TryParse(split[0], NumberStyles.Integer,
null,
| out
| Int32Val);
| if(result)
| Console.WriteLine(Int32Val);
| result = Char.TryParse(split[1][0].ToString(), out charVal);
| if(result)
| Console.WriteLine(charVal);
| result = Single.TryParse(split[2], NumberStyles.Float, null, out
| floatVal);
| if(result)
| Console.WriteLine("{0:f}", floatVal);
| }
| >
| >
| This should output:
| >
| 12
| 2
| 56,00
| >
| Willy.
| >
| >
| >
| >
|
| Well, suppose you have the following string:
|
| "Item #1: $32.53 Item #2: $32.54 Sub-Total: $65.07"
|
| With sscanf, I believe you can do something like:
|
|
| sscanf(buff, "Item #1: $%d Item #2: $%d Sub-Total: $%d", value1, value2,
| value3);
| printf("Sub-Total: %d", value3);
|
|
| Using regex, you can do something similar...and parsing out yourself
would
| be more trickier...
|
| HTH,
| Mythran
|

I prefer using TryParse over RegEx, just a matter of taste, and quite
faster
;-)

// suppose the current culture is en-US...
Decimal decVal;
string tokenString = "Item #1: $32.53 Item #2: $32.54 Sub-Total:
$65.07";
string [] split = tokenString.Split(new Char [] {' '});
bool result = Decimal.TryParse(split[7], NumberStyles.Currency, null,
out decVal);
if(result)
Console.WriteLine("Sub-total: {0:c}", decVal);

Not really tricky IMO.

Willy.

grr, that's not my point. The OP requested similar functionality to sscanf.
The closest he can get is by using Regex...sure, you can parse it yourself
into an array and access the individual elements, but that's not what the OP
was originally asking AFAIK (even though, it should work for the OP).
Anywho :)

Mythran
Sep 19 '06 #10

"Mythran" <ki********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
|
| "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
| news:Ow****************@TK2MSFTNGP02.phx.gbl...
| >
| "Mythran" <ki********@hotmail.comwrote in message
| news:uS**************@TK2MSFTNGP06.phx.gbl...
| |
| | "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
| | news:eQ**************@TK2MSFTNGP05.phx.gbl...
| | >
| | "AMP" <am******@gmail.comwrote in message
| | news:11**********************@k70g2000cwa.googlegr oups.com...
| | | Hello,
| | | Anybody know if anything exists like sscanf in c.
| | | I found a few things OL but most were pretty old. Maybe something
| has
| | | come along since 2004?
| | | Thanks
| | | Mike
| | |
| | >
| | Ignore my previous post.
| | Using Parse and TryParse methods you can achieve the same (and more)
| | results
| | as sscanf in C.
| | Consider following sample....
| | >
| | static void Main()
| | {
| | string tokenString = "12 25 56 4";
| | string [] split = tokenString.Split(new Char [] {' '});
| | int Int32Val;
| | char charVal;
| | float floatVal;
| | bool result = Int32.TryParse(split[0], NumberStyles.Integer,
| null,
| | out
| | Int32Val);
| | if(result)
| | Console.WriteLine(Int32Val);
| | result = Char.TryParse(split[1][0].ToString(), out charVal);
| | if(result)
| | Console.WriteLine(charVal);
| | result = Single.TryParse(split[2], NumberStyles.Float, null,
out
| | floatVal);
| | if(result)
| | Console.WriteLine("{0:f}", floatVal);
| | }
| | >
| | >
| | This should output:
| | >
| | 12
| | 2
| | 56,00
| | >
| | Willy.
| | >
| | >
| | >
| | >
| |
| | Well, suppose you have the following string:
| |
| | "Item #1: $32.53 Item #2: $32.54 Sub-Total: $65.07"
| |
| | With sscanf, I believe you can do something like:
| |
| |
| | sscanf(buff, "Item #1: $%d Item #2: $%d Sub-Total: $%d", value1,
value2,
| | value3);
| | printf("Sub-Total: %d", value3);
| |
| |
| | Using regex, you can do something similar...and parsing out yourself
| would
| | be more trickier...
| |
| | HTH,
| | Mythran
| |
| >
| I prefer using TryParse over RegEx, just a matter of taste, and quite
| faster
| ;-)
| >
| // suppose the current culture is en-US...
| Decimal decVal;
| string tokenString = "Item #1: $32.53 Item #2: $32.54 Sub-Total:
| $65.07";
| string [] split = tokenString.Split(new Char [] {' '});
| bool result = Decimal.TryParse(split[7], NumberStyles.Currency,
null,
| out decVal);
| if(result)
| Console.WriteLine("Sub-total: {0:c}", decVal);
| >
| Not really tricky IMO.
| >
| Willy.
| >
| >
|
| grr, that's not my point. The OP requested similar functionality to
sscanf.
| The closest he can get is by using Regex...sure, you can parse it yourself
| into an array and access the individual elements, but that's not what the
OP
| was originally asking AFAIK (even though, it should work for the OP).
| Anywho :)
|
| Mythran
|

Sorry, but I'm affraid we'll have to agree to disagree. I don't see how a
simple C library 'function' like sscanf compares to Regex, a 'library' and a
complex engine on it's own, it's too heavy weight compared to sscanf.

I know you can achieve the same using RegEx, but I can do exactly that using
some minimal code arround the individual type's Parse and TryParse methods.
Notice, that the OP asks about sscanf, this means he has a C background,
chances are that he know little/nothing about RegEx, maybe he will prefer to
use Parse and TryParse, the choice will be on him.
Willy.

|
Sep 19 '06 #11

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:ew**************@TK2MSFTNGP02.phx.gbl...
>
"Mythran" <ki********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
|
| "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
| news:Ow****************@TK2MSFTNGP02.phx.gbl...
| >
| "Mythran" <ki********@hotmail.comwrote in message
| news:uS**************@TK2MSFTNGP06.phx.gbl...
| |
| | "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in
message
| | news:eQ**************@TK2MSFTNGP05.phx.gbl...
| | >
| | "AMP" <am******@gmail.comwrote in message
| | news:11**********************@k70g2000cwa.googlegr oups.com...
| | | Hello,
| | | Anybody know if anything exists like sscanf in c.
| | | I found a few things OL but most were pretty old. Maybe
something
| has
| | | come along since 2004?
| | | Thanks
| | | Mike
| | |
| | >
| | Ignore my previous post.
| | Using Parse and TryParse methods you can achieve the same (and
more)
| | results
| | as sscanf in C.
| | Consider following sample....
| | >
| | static void Main()
| | {
| | string tokenString = "12 25 56 4";
| | string [] split = tokenString.Split(new Char [] {' '});
| | int Int32Val;
| | char charVal;
| | float floatVal;
| | bool result = Int32.TryParse(split[0], NumberStyles.Integer,
| null,
| | out
| | Int32Val);
| | if(result)
| | Console.WriteLine(Int32Val);
| | result = Char.TryParse(split[1][0].ToString(), out charVal);
| | if(result)
| | Console.WriteLine(charVal);
| | result = Single.TryParse(split[2], NumberStyles.Float, null,
out
| | floatVal);
| | if(result)
| | Console.WriteLine("{0:f}", floatVal);
| | }
| | >
| | >
| | This should output:
| | >
| | 12
| | 2
| | 56,00
| | >
| | Willy.
| | >
| | >
| | >
| | >
| |
| | Well, suppose you have the following string:
| |
| | "Item #1: $32.53 Item #2: $32.54 Sub-Total: $65.07"
| |
| | With sscanf, I believe you can do something like:
| |
| |
| | sscanf(buff, "Item #1: $%d Item #2: $%d Sub-Total: $%d", value1,
value2,
| | value3);
| | printf("Sub-Total: %d", value3);
| |
| |
| | Using regex, you can do something similar...and parsing out yourself
| would
| | be more trickier...
| |
| | HTH,
| | Mythran
| |
| >
| I prefer using TryParse over RegEx, just a matter of taste, and quite
| faster
| ;-)
| >
| // suppose the current culture is en-US...
| Decimal decVal;
| string tokenString = "Item #1: $32.53 Item #2: $32.54 Sub-Total:
| $65.07";
| string [] split = tokenString.Split(new Char [] {' '});
| bool result = Decimal.TryParse(split[7], NumberStyles.Currency,
null,
| out decVal);
| if(result)
| Console.WriteLine("Sub-total: {0:c}", decVal);
| >
| Not really tricky IMO.
| >
| Willy.
| >
| >
|
| grr, that's not my point. The OP requested similar functionality to
sscanf.
| The closest he can get is by using Regex...sure, you can parse it
yourself
| into an array and access the individual elements, but that's not what
the
OP
| was originally asking AFAIK (even though, it should work for the OP).
| Anywho :)
|
| Mythran
|

Sorry, but I'm affraid we'll have to agree to disagree. I don't see how a
simple C library 'function' like sscanf compares to Regex, a 'library' and
a
complex engine on it's own, it's too heavy weight compared to sscanf.

I know you can achieve the same using RegEx, but I can do exactly that
using
some minimal code arround the individual type's Parse and TryParse
methods.
Notice, that the OP asks about sscanf, this means he has a C background,
chances are that he know little/nothing about RegEx, maybe he will prefer
to
use Parse and TryParse, the choice will be on him.
Willy.

|

I do see where you are coming from. And yes, I will continue to disagree as
we have previously agreed to allow :) On second thought, maybe I will agree
with you about parsing out manually depending on the amount of data the OP
needs to parse. If it is a larger amount of data, and performance isn't an
issue, then I would then go with Regex for this.

:)

Mythran
Sep 19 '06 #12

"Mythran" <ki********@hotmail.comwrote in message
news:uw****************@TK2MSFTNGP04.phx.gbl...
|
| I do see where you are coming from. And yes, I will continue to disagree
as
| we have previously agreed to allow :) On second thought, maybe I will
agree
| with you about parsing out manually depending on the amount of data the OP
| needs to parse.

sscanf (and family) is often used for very simple parsing in C. Something
like this:

// query registry value.
if( ERROR_SUCCESS == RegQueryValueExA( hKey, "whateverString", NULL,
NULL, (LPBYTE)szResult, &dwResult ) )
{
// parse result
if(sscanf( szResult, "%x", &dwResult ) == 0)
...

is very common, and the equivalent in C# looks something like...

int dwResult ;
string installResult = rk.GetValue("whateverString");
bool result = Int32.TryParse(installResult, NumberStyles.HexNumber,
null, out dwResult );
if(result)
// hexVal contains a correct hex value.
...

This illustrates the equivalence of sscanf and Parse (or preferably:
TryParse).
I (nor you!) would never use RegEx for this.
If it is a larger amount of data, and performance isn't an
| issue, then I would then go with Regex for this.
|
| :)
|
Agreed, but that's not my point.
Willy.
Sep 19 '06 #13
AMP
Hello,
I am the OP.
I am beginning to understand but I have the following:
sscanf(strdata[1], "%lx\n", currentAddr);

currentAddr is an unsigned long in c(which is a uint in c#) 4 bytes,
correct?

I am trying to get the hang of this, but I could use an example with
something I'm using.
I have to convert about 8 of these to dfferent formats, so its
important I can understand what I;m doing.

Thanks
Mike

Mythran wrote:
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:ew**************@TK2MSFTNGP02.phx.gbl...

"Mythran" <ki********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
|
| "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
| news:Ow****************@TK2MSFTNGP02.phx.gbl...
| >
| "Mythran" <ki********@hotmail.comwrote in message
| news:uS**************@TK2MSFTNGP06.phx.gbl...
| |
| | "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in
message
| | news:eQ**************@TK2MSFTNGP05.phx.gbl...
| | >
| | "AMP" <am******@gmail.comwrote in message
| | news:11**********************@k70g2000cwa.googlegr oups.com...
| | | Hello,
| | | Anybody know if anything exists like sscanf in c.
| | | I found a few things OL but most were pretty old. Maybe
something
| has
| | | come along since 2004?
| | | Thanks
| | | Mike
| | |
| | >
| | Ignore my previous post.
| | Using Parse and TryParse methods you can achieve the same (and
more)
| | results
| | as sscanf in C.
| | Consider following sample....
| | >
| | static void Main()
| | {
| | string tokenString = "12 25 56 4";
| | string [] split = tokenString.Split(new Char [] {' '});
| | int Int32Val;
| | char charVal;
| | float floatVal;
| | bool result = Int32.TryParse(split[0], NumberStyles.Integer,
| null,
| | out
| | Int32Val);
| | if(result)
| | Console.WriteLine(Int32Val);
| | result = Char.TryParse(split[1][0].ToString(), out charVal);
| | if(result)
| | Console.WriteLine(charVal);
| | result = Single.TryParse(split[2], NumberStyles.Float, null,
out
| | floatVal);
| | if(result)
| | Console.WriteLine("{0:f}", floatVal);
| | }
| | >
| | >
| | This should output:
| | >
| | 12
| | 2
| | 56,00
| | >
| | Willy.
| | >
| | >
| | >
| | >
| |
| | Well, suppose you have the following string:
| |
| | "Item #1: $32.53 Item #2: $32.54 Sub-Total: $65.07"
| |
| | With sscanf, I believe you can do something like:
| |
| |
| | sscanf(buff, "Item #1: $%d Item #2: $%d Sub-Total: $%d", value1,
value2,
| | value3);
| | printf("Sub-Total: %d", value3);
| |
| |
| | Using regex, you can do something similar...and parsing out yourself
| would
| | be more trickier...
| |
| | HTH,
| | Mythran
| |
| >
| I prefer using TryParse over RegEx, just a matter of taste, and quite
| faster
| ;-)
| >
| // suppose the current culture is en-US...
| Decimal decVal;
| string tokenString = "Item #1: $32.53 Item #2: $32.54 Sub-Total:
| $65.07";
| string [] split = tokenString.Split(new Char [] {' '});
| bool result = Decimal.TryParse(split[7], NumberStyles.Currency,
null,
| out decVal);
| if(result)
| Console.WriteLine("Sub-total: {0:c}", decVal);
| >
| Not really tricky IMO.
| >
| Willy.
| >
| >
|
| grr, that's not my point. The OP requested similar functionality to
sscanf.
| The closest he can get is by using Regex...sure, you can parse it
yourself
| into an array and access the individual elements, but that's not what
the
OP
| was originally asking AFAIK (even though, it should work for the OP).
| Anywho :)
|
| Mythran
|

Sorry, but I'm affraid we'll have to agree to disagree. I don't see how a
simple C library 'function' like sscanf compares to Regex, a 'library' and
a
complex engine on it's own, it's too heavy weight compared to sscanf.

I know you can achieve the same using RegEx, but I can do exactly that
using
some minimal code arround the individual type's Parse and TryParse
methods.
Notice, that the OP asks about sscanf, this means he has a C background,
chances are that he know little/nothing about RegEx, maybe he will prefer
to
use Parse and TryParse, the choice will be on him.
Willy.

|

I do see where you are coming from. And yes, I will continue to disagree as
we have previously agreed to allow :) On second thought, maybe I will agree
with you about parsing out manually depending on the amount of data the OP
needs to parse. If it is a larger amount of data, and performance isn't an
issue, then I would then go with Regex for this.

:)

Mythran
Sep 19 '06 #14
AMP
Also, theres this qualifier (3), which is the max number to read, in
the following that I would have to deal with.
sscanf(strdata[linepos], "%3x", blkout[dataframelen]);

I appreciate EVERONES help with this.
AMP wrote:
Hello,
I am the OP.
I am beginning to understand but I have the following:
sscanf(strdata[1], "%lx\n", currentAddr);

currentAddr is an unsigned long in c(which is a uint in c#) 4 bytes,
correct?

I am trying to get the hang of this, but I could use an example with
something I'm using.
I have to convert about 8 of these to dfferent formats, so its
important I can understand what I;m doing.

Thanks
Mike

Mythran wrote:
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:ew**************@TK2MSFTNGP02.phx.gbl...
>
"Mythran" <ki********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
|
| "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
| news:Ow****************@TK2MSFTNGP02.phx.gbl...
| >
| "Mythran" <ki********@hotmail.comwrote in message
| news:uS**************@TK2MSFTNGP06.phx.gbl...
| |
| | "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in
message
| | news:eQ**************@TK2MSFTNGP05.phx.gbl...
| | >
| | "AMP" <am******@gmail.comwrote in message
| | news:11**********************@k70g2000cwa.googlegr oups.com...
| | | Hello,
| | | Anybody know if anything exists like sscanf in c.
| | | I found a few things OL but most were pretty old. Maybe
something
| has
| | | come along since 2004?
| | | Thanks
| | | Mike
| | |
| | >
| | Ignore my previous post.
| | Using Parse and TryParse methods you can achieve the same (and
more)
| | results
| | as sscanf in C.
| | Consider following sample....
| | >
| | static void Main()
| | {
| | string tokenString = "12 25 56 4";
| | string [] split = tokenString.Split(new Char [] {' '});
| | int Int32Val;
| | char charVal;
| | float floatVal;
| | bool result = Int32.TryParse(split[0], NumberStyles.Integer,
| null,
| | out
| | Int32Val);
| | if(result)
| | Console.WriteLine(Int32Val);
| | result = Char.TryParse(split[1][0].ToString(), out charVal);
| | if(result)
| | Console.WriteLine(charVal);
| | result = Single.TryParse(split[2], NumberStyles.Float, null,
out
| | floatVal);
| | if(result)
| | Console.WriteLine("{0:f}", floatVal);
| | }
| | >
| | >
| | This should output:
| | >
| | 12
| | 2
| | 56,00
| | >
| | Willy.
| | >
| | >
| | >
| | >
| |
| | Well, suppose you have the following string:
| |
| | "Item #1: $32.53 Item #2: $32.54 Sub-Total: $65.07"
| |
| | With sscanf, I believe you can do something like:
| |
| |
| | sscanf(buff, "Item #1: $%d Item #2: $%d Sub-Total: $%d", value1,
value2,
| | value3);
| | printf("Sub-Total: %d", value3);
| |
| |
| | Using regex, you can do something similar...and parsing out yourself
| would
| | be more trickier...
| |
| | HTH,
| | Mythran
| |
| >
| I prefer using TryParse over RegEx, just a matter of taste, and quite
| faster
| ;-)
| >
| // suppose the current culture is en-US...
| Decimal decVal;
| string tokenString = "Item #1: $32.53 Item #2: $32.54 Sub-Total:
| $65.07";
| string [] split = tokenString.Split(new Char [] {' '});
| bool result = Decimal.TryParse(split[7], NumberStyles.Currency,
null,
| out decVal);
| if(result)
| Console.WriteLine("Sub-total: {0:c}", decVal);
| >
| Not really tricky IMO.
| >
| Willy.
| >
| >
|
| grr, that's not my point. The OP requested similar functionality to
sscanf.
| The closest he can get is by using Regex...sure, you can parse it
yourself
| into an array and access the individual elements, but that's not what
the
OP
| was originally asking AFAIK (even though, it should work for the OP).
| Anywho :)
|
| Mythran
|
>
Sorry, but I'm affraid we'll have to agree to disagree. I don't see how a
simple C library 'function' like sscanf compares to Regex, a 'library' and
a
complex engine on it's own, it's too heavy weight compared to sscanf.
>
I know you can achieve the same using RegEx, but I can do exactly that
using
some minimal code arround the individual type's Parse and TryParse
methods.
Notice, that the OP asks about sscanf, this means he has a C background,
chances are that he know little/nothing about RegEx, maybe he will prefer
to
use Parse and TryParse, the choice will be on him.
>
>
Willy.
>
|
>
>
I do see where you are coming from. And yes, I will continue to disagree as
we have previously agreed to allow :) On second thought, maybe I will agree
with you about parsing out manually depending on the amount of data the OP
needs to parse. If it is a larger amount of data, and performance isn't an
issue, then I would then go with Regex for this.

:)

Mythran
Sep 19 '06 #15
AMP

For the following:
sscanf(strdata[linepos], "%3x", blkout[dataframelen]);
What it looks like to me, is we are taking (strdata[linepos],
formatting it and storing it in blkout[dataframelen]. So were not just
checking something, we are moving the values to another Array and
formatting them at the same time.Am I correct?
And no I'm not a c programmer, I'm trying to convert a c program to c#,
so I'm learning c at the same time, so Im looking to do the same exact
thing in c# that has already been implemented in the c version.

And with everyones help I have learned a great deal along the way. I am
about 90% done.
Thanks Mike
AMP wrote:
Also, theres this qualifier (3), which is the max number to read, in
the following that I would have to deal with.
> sscanf(strdata[linepos], "%3x", blkout[dataframelen]);
I appreciate EVERONES help with this.
AMP wrote:
Hello,
I am the OP.
I am beginning to understand but I have the following:
sscanf(strdata[1], "%lx\n", currentAddr);

currentAddr is an unsigned long in c(which is a uint in c#) 4 bytes,
correct?

I am trying to get the hang of this, but I could use an example with
something I'm using.
I have to convert about 8 of these to dfferent formats, so its
important I can understand what I;m doing.

Thanks
Mike

Mythran wrote:
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:ew**************@TK2MSFTNGP02.phx.gbl...

"Mythran" <ki********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
|
| "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
| news:Ow****************@TK2MSFTNGP02.phx.gbl...
| >
| "Mythran" <ki********@hotmail.comwrote in message
| news:uS**************@TK2MSFTNGP06.phx.gbl...
| |
| | "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in
message
| | news:eQ**************@TK2MSFTNGP05.phx.gbl...
| | >
| | "AMP" <am******@gmail.comwrote in message
| | news:11**********************@k70g2000cwa.googlegr oups.com...
| | | Hello,
| | | Anybody know if anything exists like sscanf in c.
| | | I found a few things OL but most were pretty old. Maybe
something
| has
| | | come along since 2004?
| | | Thanks
| | | Mike
| | |
| | >
| | Ignore my previous post.
| | Using Parse and TryParse methods you can achieve the same (and
more)
| | results
| | as sscanf in C.
| | Consider following sample....
| | >
| | static void Main()
| | {
| | string tokenString = "12 25 56 4";
| | string [] split = tokenString.Split(new Char [] {' '});
| | int Int32Val;
| | char charVal;
| | float floatVal;
| | bool result = Int32.TryParse(split[0], NumberStyles.Integer,
| null,
| | out
| | Int32Val);
| | if(result)
| | Console.WriteLine(Int32Val);
| | result = Char.TryParse(split[1][0].ToString(), out charVal);
| | if(result)
| | Console.WriteLine(charVal);
| | result = Single.TryParse(split[2], NumberStyles.Float, null,
out
| | floatVal);
| | if(result)
| | Console.WriteLine("{0:f}", floatVal);
| | }
| | >
| | >
| | This should output:
| | >
| | 12
| | 2
| | 56,00
| | >
| | Willy.
| | >
| | >
| | >
| | >
| |
| | Well, suppose you have the following string:
| |
| | "Item #1: $32.53 Item #2: $32.54 Sub-Total: $65.07"
| |
| | With sscanf, I believe you can do something like:
| |
| |
| | sscanf(buff, "Item #1: $%d Item #2: $%d Sub-Total: $%d", value1,
value2,
| | value3);
| | printf("Sub-Total: %d", value3);
| |
| |
| | Using regex, you can do something similar...and parsing out yourself
| would
| | be more trickier...
| |
| | HTH,
| | Mythran
| |
| >
| I prefer using TryParse over RegEx, just a matter of taste, and quite
| faster
| ;-)
| >
| // suppose the current culture is en-US...
| Decimal decVal;
| string tokenString = "Item #1: $32.53 Item #2: $32.54 Sub-Total:
| $65.07";
| string [] split = tokenString.Split(new Char [] {' '});
| bool result = Decimal.TryParse(split[7], NumberStyles.Currency,
null,
| out decVal);
| if(result)
| Console.WriteLine("Sub-total: {0:c}", decVal);
| >
| Not really tricky IMO.
| >
| Willy.
| >
| >
|
| grr, that's not my point. The OP requested similar functionality to
sscanf.
| The closest he can get is by using Regex...sure, you can parse it
yourself
| into an array and access the individual elements, but that's not what
the
OP
| was originally asking AFAIK (even though, it should work for the OP).
| Anywho :)
|
| Mythran
|

Sorry, but I'm affraid we'll have to agree to disagree. I don't see how a
simple C library 'function' like sscanf compares to Regex, a 'library' and
a
complex engine on it's own, it's too heavy weight compared to sscanf.

I know you can achieve the same using RegEx, but I can do exactly that
using
some minimal code arround the individual type's Parse and TryParse
methods.
Notice, that the OP asks about sscanf, this means he has a C background,
chances are that he know little/nothing about RegEx, maybe he will prefer
to
use Parse and TryParse, the choice will be on him.


Willy.

|


>
I do see where you are coming from. And yes, I will continue to disagree as
we have previously agreed to allow :) On second thought, maybe I will agree
with you about parsing out manually depending on the amount of data the OP
needs to parse. If it is a larger amount of data, and performance isn't an
issue, then I would then go with Regex for this.
>
:)
>
Mythran
Sep 19 '06 #16

"AMP" <am******@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
| Hello,
| I am the OP.
| I am beginning to understand but I have the following:
| sscanf(strdata[1], "%lx\n", currentAddr);
|
| currentAddr is an unsigned long in c(which is a uint in c#) 4 bytes,
| correct?

Yep.

| I am trying to get the hang of this, but I could use an example with
| something I'm using.
as per my previous sample...

uint currentAddr;
string strData = " ff12fffe ";
bool result = UInt32.TryParse(strData.SubString(1), NumberStyles.HexNumber,
null, out currentAddr);
if(result)
// parsed correctly.
else
// failed to parse.

or, you can use the Parse method.

try {
hexVal = UInt32.Parse(installResult.Substring(1),
NumberStyles.HexNumber);
}
Willy.
Sep 19 '06 #17

"AMP" <am******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
| Also, theres this qualifier (3), which is the max number to read, in
| the following that I would have to deal with.
| sscanf(strdata[linepos], "%3x", blkout[dataframelen]);
|
| I appreciate EVERONES help with this.
|
|
string strdata = " ffc2fffe Hello ";
uint hexVal;
UInt32.TryParse(installResult.Substring(1, 3), NumberStyles.HexNumber, null,
out hexVal);

hexVal will contain 000000ffc;

But why don't you read the docs and try yourself?, it takes 5 minutes to
test all your cases, waiting for an answer may take hours.

Willy.

Sep 19 '06 #18
AMP
I have been trying, but there is a problem with the type conversions
also
sscanf(strdata[linepos], "%3x", blkout[dataframelen]);

strdata[linepos] is a char Array
%3x is "Read a Max of 3 Chars and format it as an int."
blkout[dataframelen]); is a byte Array

So to me I see:Read a max of 3 chars from strdata, format it as an int
and store it in blkout[dataframelen](a byte) I dont know how to do
that.

The entire section of code:
/* Transfer data in line into blkout: */
for(linepos= 0;
linepos < linelen-3; linepos+= 3, dataframelen++)
{
sscanf(&strdata[linepos], "%3x", &blkout[dataframelen]);
/* (Max 16 bytes per line!) */
}
Thanks
Mike

Willy Denoyette [MVP] wrote:
"AMP" <am******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
| Also, theres this qualifier (3), which is the max number to read, in
| the following that I would have to deal with.
| sscanf(strdata[linepos], "%3x", blkout[dataframelen]);
|
| I appreciate EVERONES help with this.
|
|
string strdata = " ffc2fffe Hello ";
uint hexVal;
UInt32.TryParse(installResult.Substring(1, 3), NumberStyles.HexNumber, null,
out hexVal);

hexVal will contain 000000ffc;

But why don't you read the docs and try yourself?, it takes 5 minutes to
test all your cases, waiting for an answer may take hours.

Willy.
Sep 20 '06 #19

"AMP" <am******@gmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
|I have been trying, but there is a problem with the type conversions
| also
| sscanf(strdata[linepos], "%3x", blkout[dataframelen]);
|
| strdata[linepos] is a char Array
| %3x is "Read a Max of 3 Chars and format it as an int."
| blkout[dataframelen]); is a byte Array
|
| So to me I see:Read a max of 3 chars from strdata, format it as an int
| and store it in blkout[dataframelen](a byte) I dont know how to do
| that.
|
| The entire section of code:
| /* Transfer data in line into blkout: */
| for(linepos= 0;
| linepos < linelen-3; linepos+= 3, dataframelen++)
| {
| sscanf(&strdata[linepos], "%3x", &blkout[dataframelen]);
| /* (Max 16 bytes per line!) */
| }
|

Well, consider following sample...here I assume that the input buffer looks
like:
buffer = " ff c2 ff fe de de ba ba cc 12 25 66 9a 63 55 87";

....
byte[] ahexVal = new byte[16];
installResult = " ff c2 ff fe de de ba ba cc 12 25 66 9a 63 55 87";
for(int n = 0, m = 0; n < 48; n+=3, m++)
result = Byte.TryParse(installResult.Substring(n, 3),
NumberStyles.HexNumber, null, out ahexVal[m]);
foreach(Byte b in ahexVal)
Console.Write("{0:x}", b);

So what I'm doing is parsing the buffer, checking for a valid hex number
sequences (every third character starting from 0 with a width of 3
characters)). Every valid hex value will be stored as a byte value in an
array of bytes.
But, I would urge you to start thinking in terms of C# and the framework.
While both C# and C share some common grounds as a language, the C library
and the FCL on the other hand do not, that means that you should not try to
mimic C contructs in C#, it's a waste of time.
Also don't think you can learn both C and C# at the same time, you'll have
to make a choice, and because you are here, your choice should be C#.
Start with C# and spend most of your time with the FCL, it offers a much
richer set of API's than the C library.
Willy.


Sep 20 '06 #20
AMP
Willy and all
Thanks so much for all your help.
My problem was I didnt understand the function.
I had a couple "c" guys take the time to explain it.
>From there I used your example and it worked great.
Mike

Willy Denoyette [MVP] wrote:
"AMP" <am******@gmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
|I have been trying, but there is a problem with the type conversions
| also
| sscanf(strdata[linepos], "%3x", blkout[dataframelen]);
|
| strdata[linepos] is a char Array
| %3x is "Read a Max of 3 Chars and format it as an int."
| blkout[dataframelen]); is a byte Array
|
| So to me I see:Read a max of 3 chars from strdata, format it as an int
| and store it in blkout[dataframelen](a byte) I dont know how to do
| that.
|
| The entire section of code:
| /* Transfer data in line into blkout: */
| for(linepos= 0;
| linepos < linelen-3; linepos+= 3, dataframelen++)
| {
| sscanf(&strdata[linepos], "%3x", &blkout[dataframelen]);
| /* (Max 16 bytes per line!) */
| }
|

Well, consider following sample...here I assume that the input buffer looks
like:
buffer = " ff c2 ff fe de de ba ba cc 12 25 66 9a 63 55 87";

...
byte[] ahexVal = new byte[16];
installResult = " ff c2 ff fe de de ba ba cc 12 25 66 9a 63 55 87";
for(int n = 0, m = 0; n < 48; n+=3, m++)
result = Byte.TryParse(installResult.Substring(n, 3),
NumberStyles.HexNumber, null, out ahexVal[m]);
foreach(Byte b in ahexVal)
Console.Write("{0:x}", b);

So what I'm doing is parsing the buffer, checking for a valid hex number
sequences (every third character starting from 0 with a width of 3
characters)). Every valid hex value will be stored as a byte value in an
array of bytes.
But, I would urge you to start thinking in terms of C# and the framework.
While both C# and C share some common grounds as a language, the C library
and the FCL on the other hand do not, that means that you should not try to
mimic C contructs in C#, it's a waste of time.
Also don't think you can learn both C and C# at the same time, you'll have
to make a choice, and because you are here, your choice should be C#.
Start with C# and spend most of your time with the FCL, it offers a much
richer set of API's than the C library.
Willy.
Sep 20 '06 #21

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Allan Bruce | last post: by
10 posts views Thread by baumann | last post: by
4 posts views Thread by baumann | last post: by
5 posts views Thread by jchludzinski | last post: by
22 posts views Thread by Superfox il Volpone | last post: by
8 posts views Thread by Artemio | last post: by
5 posts views Thread by Alex Mathieu | last post: by
7 posts views Thread by gio | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.