473,779 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
20 21443

"Mythran" <ki********@hot mail.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
|
| "Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
| news:Ow******** ********@TK2MSF TNGP02.phx.gbl. ..
| >
| "Mythran" <ki********@hot mail.comwrote in message
| news:uS******** ******@TK2MSFTN GP06.phx.gbl...
| |
| | "Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
| | news:eQ******** ******@TK2MSFTN GP05.phx.gbl...
| | >
| | "AMP" <am******@gmail .comwrote in message
| | news:11******** **************@ k70g2000cwa.goo glegroups.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.Spl it(new Char [] {' '});
| | int Int32Val;
| | char charVal;
| | float floatVal;
| | bool result = Int32.TryParse( split[0], NumberStyles.In teger,
| null,
| | out
| | Int32Val);
| | if(result)
| | Console.WriteLi ne(Int32Val);
| | result = Char.TryParse(s plit[1][0].ToString(), out charVal);
| | if(result)
| | Console.WriteLi ne(charVal);
| | result = Single.TryParse (split[2], NumberStyles.Fl oat, null,
out
| | floatVal);
| | if(result)
| | Console.WriteLi ne("{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.Spl it(new Char [] {' '});
| bool result = Decimal.TryPars e(split[7], NumberStyles.Cu rrency,
null,
| out decVal);
| if(result)
| Console.WriteLi ne("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.bewro te in message
news:ew******** ******@TK2MSFTN GP02.phx.gbl...
>
"Mythran" <ki********@hot mail.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
|
| "Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
| news:Ow******** ********@TK2MSF TNGP02.phx.gbl. ..
| >
| "Mythran" <ki********@hot mail.comwrote in message
| news:uS******** ******@TK2MSFTN GP06.phx.gbl...
| |
| | "Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in
message
| | news:eQ******** ******@TK2MSFTN GP05.phx.gbl...
| | >
| | "AMP" <am******@gmail .comwrote in message
| | news:11******** **************@ k70g2000cwa.goo glegroups.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.Spl it(new Char [] {' '});
| | int Int32Val;
| | char charVal;
| | float floatVal;
| | bool result = Int32.TryParse( split[0], NumberStyles.In teger,
| null,
| | out
| | Int32Val);
| | if(result)
| | Console.WriteLi ne(Int32Val);
| | result = Char.TryParse(s plit[1][0].ToString(), out charVal);
| | if(result)
| | Console.WriteLi ne(charVal);
| | result = Single.TryParse (split[2], NumberStyles.Fl oat, null,
out
| | floatVal);
| | if(result)
| | Console.WriteLi ne("{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.Spl it(new Char [] {' '});
| bool result = Decimal.TryPars e(split[7], NumberStyles.Cu rrency,
null,
| out decVal);
| if(result)
| Console.WriteLi ne("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********@hot mail.comwrote in message
news:uw******** ********@TK2MSF TNGP04.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 == RegQueryValueEx A( hKey, "whateverString ", NULL,
NULL, (LPBYTE)szResul t, &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("wh ateverString");
bool result = Int32.TryParse( installResult, NumberStyles.He xNumber,
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.bewro te in message
news:ew******** ******@TK2MSFTN GP02.phx.gbl...

"Mythran" <ki********@hot mail.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
|
| "Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
| news:Ow******** ********@TK2MSF TNGP02.phx.gbl. ..
| >
| "Mythran" <ki********@hot mail.comwrote in message
| news:uS******** ******@TK2MSFTN GP06.phx.gbl...
| |
| | "Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in
message
| | news:eQ******** ******@TK2MSFTN GP05.phx.gbl...
| | >
| | "AMP" <am******@gmail .comwrote in message
| | news:11******** **************@ k70g2000cwa.goo glegroups.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.Spl it(new Char [] {' '});
| | int Int32Val;
| | char charVal;
| | float floatVal;
| | bool result = Int32.TryParse( split[0], NumberStyles.In teger,
| null,
| | out
| | Int32Val);
| | if(result)
| | Console.WriteLi ne(Int32Val);
| | result = Char.TryParse(s plit[1][0].ToString(), out charVal);
| | if(result)
| | Console.WriteLi ne(charVal);
| | result = Single.TryParse (split[2], NumberStyles.Fl oat, null,
out
| | floatVal);
| | if(result)
| | Console.WriteLi ne("{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.Spl it(new Char [] {' '});
| bool result = Decimal.TryPars e(split[7], NumberStyles.Cu rrency,
null,
| out decVal);
| if(result)
| Console.WriteLi ne("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.bewro te in message
news:ew******** ******@TK2MSFTN GP02.phx.gbl...
>
"Mythran" <ki********@hot mail.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
|
| "Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
| news:Ow******** ********@TK2MSF TNGP02.phx.gbl. ..
| >
| "Mythran" <ki********@hot mail.comwrote in message
| news:uS******** ******@TK2MSFTN GP06.phx.gbl...
| |
| | "Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in
message
| | news:eQ******** ******@TK2MSFTN GP05.phx.gbl...
| | >
| | "AMP" <am******@gmail .comwrote in message
| | news:11******** **************@ k70g2000cwa.goo glegroups.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.Spl it(new Char [] {' '});
| | int Int32Val;
| | char charVal;
| | float floatVal;
| | bool result = Int32.TryParse( split[0], NumberStyles.In teger,
| null,
| | out
| | Int32Val);
| | if(result)
| | Console.WriteLi ne(Int32Val);
| | result = Char.TryParse(s plit[1][0].ToString(), out charVal);
| | if(result)
| | Console.WriteLi ne(charVal);
| | result = Single.TryParse (split[2], NumberStyles.Fl oat, null,
out
| | floatVal);
| | if(result)
| | Console.WriteLi ne("{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.Spl it(new Char [] {' '});
| bool result = Decimal.TryPars e(split[7], NumberStyles.Cu rrency,
null,
| out decVal);
| if(result)
| Console.WriteLi ne("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.bewro te in message
news:ew******** ******@TK2MSFTN GP02.phx.gbl...

"Mythran" <ki********@hot mail.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
|
| "Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
| news:Ow******** ********@TK2MSF TNGP02.phx.gbl. ..
| >
| "Mythran" <ki********@hot mail.comwrote in message
| news:uS******** ******@TK2MSFTN GP06.phx.gbl...
| |
| | "Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in
message
| | news:eQ******** ******@TK2MSFTN GP05.phx.gbl...
| | >
| | "AMP" <am******@gmail .comwrote in message
| | news:11******** **************@ k70g2000cwa.goo glegroups.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.Spl it(new Char [] {' '});
| | int Int32Val;
| | char charVal;
| | float floatVal;
| | bool result = Int32.TryParse( split[0], NumberStyles.In teger,
| null,
| | out
| | Int32Val);
| | if(result)
| | Console.WriteLi ne(Int32Val);
| | result = Char.TryParse(s plit[1][0].ToString(), out charVal);
| | if(result)
| | Console.WriteLi ne(charVal);
| | result = Single.TryParse (split[2], NumberStyles.Fl oat, null,
out
| | floatVal);
| | if(result)
| | Console.WriteLi ne("{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.Spl it(new Char [] {' '});
| bool result = Decimal.TryPars e(split[7], NumberStyles.Cu rrency,
null,
| out decVal);
| if(result)
| Console.WriteLi ne("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.goo glegroups.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.SubStr ing(1), NumberStyles.He xNumber,
null, out currentAddr);
if(result)
// parsed correctly.
else
// failed to parse.

or, you can use the Parse method.

try {
hexVal = UInt32.Parse(in stallResult.Sub string(1),
NumberStyles.He xNumber);
}
Willy.
Sep 19 '06 #17

"AMP" <am******@gmail .comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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.He xNumber, 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.goog legroups.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.He xNumber, 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******** *************@k 70g2000cwa.goog legroups.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(i nstallResult.Su bstring(n, 3),
NumberStyles.He xNumber, 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

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

Similar topics

7
473
by: Allan Bruce | last post by:
If I have sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName); and the last string contains spaces, e.g. my complete string "FL:1234ABCD:3:FileName With Spaces.txt\n" does sscanf just make lFileName the string up to the whitespace? even though I tell it the end of string as at the \n ? Thanks
4
4259
by: smshahriar | last post by:
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
10
5473
by: baumann | last post by:
hi, 1) first test program code #include <stdio.h> int main(void) { char * file = "aaa 23 32 m 2.23 ammasd"; int i2,i3;
4
2030
by: baumann | last post by:
hi all there has 2 program 1) the first test program code #include <stdio.h> int main(void) {
5
6405
by: jchludzinski | last post by:
I'm using strtok() to parse thru a line and read different numbers: float value; char *token; token = strtok( line, " " ); .... sscanf( token, "%f", &value ); These results are less precise than I had expected:
22
2963
by: Superfox il Volpone | last post by:
Hello I have some problem with sscanf, I tryed this code but it doesn't works : char* stringa = "18/2005" char mese; char anno; int i_letture; i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);
8
2389
by: Artemio | last post by:
Dear folks, I need some help with using the sscanf() function. I need to parse a string which has several parameters given in a "A=... B=... C=..." way, and each has a different type (one is a text string, another is a decimal, next one is float, etc.). I have GCC 4.0.1 on Mac OS X Tiger. Here is an example of what I am trying to do.
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
7
11629
by: gio | last post by:
suppose I have: .... char str1; char str2; int ret; fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0' ret=sscanf(str1, "%s", str2); ....
0
9471
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
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
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
10071
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
9925
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...
0
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4036
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
3631
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.