• 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: Specific byte size data type?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Specific byte size data type?


  • Subject: Re: Specific byte size data type?
  • From: Esteban <email@hidden>
  • Date: Wed, 6 Mar 2002 18:10:29 -0800

This sounds to me like a job for a C struct or bit field

You can do this by creating a struct bit field like so...

struct protocol_data {
unsigned header:8
unsigned field1:16
unsigned field2:24
...
};

You specify the bit field members as bits, hence 1 byte = 8 bits, and header is 8 bits long. Bit field members must be unsigned or int. Make sure the bits add up to a multiple of 8.

I'd probably go find an adequate c++ or c book, pdf, or online manual, to make sure everything is set up right, and learn the best ways to get at the data and deal with it (unions, etc)

I dont know of an equivalent Objective C way of doing this, but you could give NSData a try and just set different ranges to access different parts of the protocol data. Although with ranges you can only access 1 byte minimum. You can have a bit field member of 2 bits, in a bit field stucture.

-Esteban Uribe

On Wednesday, March 6, 2002, at 07:57 AM, Andrew Baldwin wrote:

I need to read packets from the network and I want to save the data into a structure with variables which will be of a specific size (a 1 byte header, a 2 or 3 byte field, followed by another field, etc. all conforming to a protocol I'm implementing). How can I do this in cocoa -- what types should I use?

Thanks in advance for helping a newbie.
_______________________________________________
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.
_______________________________________________
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.

References: 
 >Specific byte size data type? (From: Andrew Baldwin <email@hidden>)

  • Prev by Date: Re: NSThread - plz help!!!
  • Next by Date: Re: Is resizing slower that it used to be?
  • Previous by thread: Specific byte size data type?
  • Next by thread: Question on Objective C++
  • Index(es):
    • Date
    • Thread