Re: Safest way to convert char's to int's
From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 08/14/04
- Next message: Alwyn: "Re: Logic C++ error. Why?"
- Previous message: Chris \( Val \): "Re: Logic C++ error. Why?"
- In reply to: Alwyn: "Re: Safest way to convert char's to int's"
- Next in thread: Alwyn: "Re: Safest way to convert char's to int's"
- Reply: Alwyn: "Re: Safest way to convert char's to int's"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Alwyn: "Re: Logic C++ error. Why?"
- Previous message: Chris \( Val \): "Re: Logic C++ error. Why?"
- In reply to: Alwyn: "Re: Safest way to convert char's to int's"
- Next in thread: Alwyn: "Re: Safest way to convert char's to int's"
- Reply: Alwyn: "Re: Safest way to convert char's to int's"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|