• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Hex IP to string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Follow-Ups:
    • Re: Hex IP to string
      • From: Manuel Darveau <email@hidden>
References: 
 >Hex IP to string (From: Manuel Darveau <email@hidden>)

  • Prev by Date: Re: Scripting a Cocoa app
  • Next by Date: trapping enter/tab on NSTableView
  • Previous by thread: Hex IP to string
  • Next by thread: Re: Hex IP to string
  • Index(es):
    • Date
    • Thread