Re: Hex IP to string
Re: Hex IP to string
- Subject: Re: Hex IP to string
- From: Greg Titus <email@hidden>
- Date: Tue, 19 Mar 2002 21:18:11 -0800
On Tuesday, March 19, 2002, at 08:56 PM, Manuel Darveau wrote:
Hi!
I want to convert an IP in the form of "0xffffff00" to 255.255.255.0
I tried:
long val;
val = strtol([@"0xffffff00" cString], nil, 16);
NSLog(@"%@:%ld", subnet, val);
NSLog(@"Subnet:%ld.%ld.%ld.%ld", (val & 0xff000000) >> 24, (val &
0x00ff0000) >> 16, (val & 0x0000ff00) >> 8, (val & 0x000000ff));
This work fine with hex like 0x00ffffff but when you enter 0xff000000 it
seem to have an overflow.
Anybody see what I am missing?
Make val an unsigned long instead of a long. Otherwise the left shifts
will propogate the sign bit whenever the sign bit is on. (The high bit.)
With signed values, 0xff00000000 >> 24 == 0xffffffff instead of
0x000000ff.
Hope this helps,
--Greg
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.