SSCANF!! 
Author Message
 SSCANF!!

Hi folks!! whats the equivalent of the following in CSharp ? with RegEx or
Split or anything else?

string str="000100";
int *arr=new int[6];
sscanf(str.c_str(),"%d%d%d%d%d%d",arr[0],arr[1],
                arr[2],arr[3],arr[4],arr[5]);

Thanks a lot!

--
*************
Best Regards
Satch
*************



Wed, 06 Oct 2004 03:28:10 GMT  
 SSCANF!!
Ok folks found the round way...
char [] arr=str.ToCharArray();
and then bascially
int i=Int32.Parse(arr[0].ToString());

If someone has a better soln...u are welcome!


Quote:
> Hi folks!! whats the equivalent of the following in CSharp ? with RegEx or
> Split or anything else?

> string str="000100";
> int *arr=new int[6];
> sscanf(str.c_str(),"%d%d%d%d%d%d",arr[0],arr[1],
>                 arr[2],arr[3],arr[4],arr[5]);

> Thanks a lot!

> --
> *************
> Best Regards
> Satch
> *************



Wed, 06 Oct 2004 05:19:05 GMT  
 SSCANF!!
Satch,
// If I'm reading what you want correctly, then you can skip the char [] and
ToCharArray() by doing this:

string str="000100";
int [] arr = new int[str.Length];
for( int i = 0; i < str.Length; i++ )
    arr[i] = (int)str[i];

Chris R.


Quote:
> Ok folks found the round way...
> char [] arr=str.ToCharArray();
> and then bascially
> int i=Int32.Parse(arr[0].ToString());

> If someone has a better soln...u are welcome!



> > Hi folks!! whats the equivalent of the following in CSharp ? with RegEx
or
> > Split or anything else?

> > string str="000100";
> > int *arr=new int[6];
> > sscanf(str.c_str(),"%d%d%d%d%d%d",arr[0],arr[1],
> >                 arr[2],arr[3],arr[4],arr[5]);

> > Thanks a lot!

> > --
> > *************
> > Best Regards
> > Satch
> > *************



Wed, 06 Oct 2004 12:00:56 GMT  
 SSCANF!!
Chris that gives me the ASCII val of the char.
If its 0 then I need 0 basically ur routine is giving me value 86 instead of
0.

Quote:
> Satch,
> // If I'm reading what you want correctly, then you can skip the char []
and
> ToCharArray() by doing this:

> string str="000100";
> int [] arr = new int[str.Length];
> for( int i = 0; i < str.Length; i++ )
>     arr[i] = (int)str[i];

> Chris R.



> > Ok folks found the round way...
> > char [] arr=str.ToCharArray();
> > and then bascially
> > int i=Int32.Parse(arr[0].ToString());

> > If someone has a better soln...u are welcome!



> > > Hi folks!! whats the equivalent of the following in CSharp ? with
RegEx
> or
> > > Split or anything else?

> > > string str="000100";
> > > int *arr=new int[6];
> > > sscanf(str.c_str(),"%d%d%d%d%d%d",arr[0],arr[1],
> > >                 arr[2],arr[3],arr[4],arr[5]);

> > > Thanks a lot!

> > > --
> > > *************
> > > Best Regards
> > > Satch
> > > *************



Wed, 06 Oct 2004 22:44:54 GMT  
 SSCANF!!
    Actually, this technique is an old C style way of converting char to
numbers... it's just been so long that I forgot the details.  Ooops!
    I get 48 (Hex 0x30) for the ASCII value of '0' (not 86).  Luckily, the
numbers are in order 0 through 9, so, you could easily change:

Quote:
> >     arr[i] = (int)str[i];

    To
arr[i] = (int)str[i] - (int)('0');

    This is the correct value you are seeking.  It may not seem as graceful
as you'd like, but I can guarantee you that it's faster than the other
method using 6 calls to Parse() and 6 calls to ToString().

Chris R.


Quote:
> Ok folks found the round way...
> char [] arr=str.ToCharArray();
> and then bascially
> int i=Int32.Parse(arr[0].ToString());

> If someone has a better soln...u are welcome!



> > Hi folks!! whats the equivalent of the following in CSharp ? with RegEx
or
> > Split or anything else?

> > string str="000100";
> > int *arr=new int[6];
> > sscanf(str.c_str(),"%d%d%d%d%d%d",arr[0],arr[1],
> >                 arr[2],arr[3],arr[4],arr[5]);

> > Thanks a lot!

> > --
> > *************
> > Best Regards
> > Satch
> > *************



Thu, 07 Oct 2004 01:21:21 GMT  
 SSCANF!!
    Actually, this technique is an old C style way of converting char to
numbers... it's just been so long that I forgot the details.  Ooops!
    I get 48 (Hex 0x30) for the ASCII value of '0' (not 86).  Luckily, the
numbers are in order 0 through 9, so, you could easily change:

Quote:
> >     arr[i] = (int)str[i];

    To
arr[i] = (int)str[i] - (int)('0');

    This is the correct value you are seeking.  It may not seem as graceful
as you'd like, but I can guarantee you that it's faster than the other
method using 6 calls to Parse() and 6 calls to ToString().

Chris R.


Quote:
> Ok folks found the round way...
> char [] arr=str.ToCharArray();
> and then bascially
> int i=Int32.Parse(arr[0].ToString());

> If someone has a better soln...u are welcome!



> > Hi folks!! whats the equivalent of the following in CSharp ? with RegEx
or
> > Split or anything else?

> > string str="000100";
> > int *arr=new int[6];
> > sscanf(str.c_str(),"%d%d%d%d%d%d",arr[0],arr[1],
> >                 arr[2],arr[3],arr[4],arr[5]);

> > Thanks a lot!

> > --
> > *************
> > Best Regards
> > Satch
> > *************



Thu, 07 Oct 2004 01:21:21 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. is there anything like SSCANF?

2. sscanf behavior (bug in glibc?)

3. CW PS2, sscanf and unsigned longs : bug?

4. sscanf() : segmentation fault

5. sscanf

6. sscanf to Verify Numeric IP Address

7. sscanf and short ints

8. multiple vars from sscanf

9. fgets/sscanf

10. sscanf one program three compilers three results

11. quotes in sscanf() ????????????

12. sscanf u conversion

 

 
Powered by phpBB® Forum Software