Re: Safest way to convert char's to int's

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 08/14/04


Date: Sat, 14 Aug 2004 20:37:26 +1000


"Alwyn" <dt015a1979@mac.com.invalid> wrote in message
news:130820041744281414%dt015a1979@mac.com.invalid...
| In article <2o3t9eF6bjciU1@uni-berlin.de>, Val \
| <chrisval@bigpond.com.au> wrote:
|
| > I'm not a big fan of using pointer arithmetic in
| > C++ as your code demonstrates, when there are safer
| > constructs available, and I feel much more confidant
| > with the code following.
|
| Well, I think Objective-C does it best of all. Here is my version:

Ooohhh, a challenge :-)

Let's see...

| #import <Foundation/Foundation.h>
|
| int main (void)
| {
| unsigned char oid[16];
| NSString *oidStr = @"1.3.6.1.5.1.11.3.2.1.4.25.0";
| NSArray *oids = [oidStr componentsSeparatedByString:@"."];
| NSEnumerator *oidsEnum = [oids objectEnumerator];
| NSString *number;
| int i = 0;
| while ((number = [oidsEnum nextObject]) != nil) {
| oid[i++] = [number intValue];
| }
|
| for (int j = 0; j < i; j++) {
| printf("%d ", oid[j]);
| }
| puts("");
|
| return 0;
| }
|
| No fiddling about with substrings or pointers, just splitting the
| string into an array of numbers in one go and iterating over that. Why
| can't std::string do something similar?

Hmn...not that I understand Objective-C, but the iteration
section is very much similar to the construct I had shown
with std::string.

The only real difference I see is in the part before the
iteration, where it seems there is some pre-parsing going
on, but again, I could do something similar with for_each
and functors possibly.

Now if that was supposed to be simpler, how about this ?

int main()
 {
  std::string Buffer;
  std::istringstream Iss( "1.3.6.1.5.1.11.3.2.1.4.25.0" );

  while( std::getline( Iss, Buffer, '.' ) )
         std::cout << Buffer << std::endl;

  return 0;
 }

Cheers.
Chris Val



Relevant Pages

  • Re: Why does the instance change?
    ... public PositionVector(int _x, int _y, int _z) ... myClass myObject = new Object; ... on first iteration: ... I presume that a new memory allocation isn't being made and so on ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Local variables within looping constructs
    ... > the iteration. ... variable like an int there may well be no cost at all. ... That isn't within the loop, though, that is exactly equivalent to ... variables local to where they are used inproves clarity and reduces the ...
    (comp.lang.c)
  • Re: Unsure about Something
    ... I don't think there is any other way to do this (declare a local ... >>In each iteration you have declared a new variable with the name "a". ... > int n = i; ... > is re-created on each iteration of the loop. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Pointer difficulties
    ... Each iteration of the top loop results in populating ywith the j'th column ... two stores to an "element" one past the final element of an array. ... int main ... $ gcc -DVERS1 -o code code.c ...
    (comp.lang.c)
  • Re: Task Problem!!!!
    ... or at least a mix of iteration and ... recursion, like the following: ... size_t y, int clr, int bg_clr) ... Dig the even newer still, yet more improved, sig! ...
    (comp.lang.c)