Re: Corrupted NSMutableString
Re: Corrupted NSMutableString
- Subject: Re: Corrupted NSMutableString
- From: "Alastair J.Houghton" <email@hidden>
- Date: Sat, 2 Aug 2003 18:00:19 +0100
On Saturday, August 2, 2003, at 03:30 pm, Matthew wrote:
And I think you are treading on thin ice by using [NSData
getBytes] to read short integers. You seem perhaps to be relying
on the byte order and that sizeof(short int) is 2 ? Better not to
build such dependencies into your code.
Since the binary file's definition uses 2-byte short integers (for this
section), I'm kind of stuck. Since C defines short integers as being
two bytes, it "should" not be a problem.
No, it doesn't. C defines:
char is 1 byte
short int is *at least* 2 bytes and at most the same size as int
int is *at least* a large as short int
long int is *at least* 4 bytes and must be the same size or larger
than int
There are a few more recent additions from the C99 spec, specifically
bool and long long int, but I can't remember the definitions off the
top of my head.
Having said that, both the newer versions of the C standard and the
Carbon API define fixed-size types, including
in C99's <stdint.h>: int8_t int16_t int32_t uint8_t uint16_t uint32_t
in Carbon: UInt8 UInt16 UInt32 UInt64 SInt8 SInt16 SInt32 SInt64
(The byte order is specified in the file, but I don't know how to swap
the bits if I had to.)
There are several ways to swap bytes; the simplest is to store
everything in network byte order (big-endian - i.e. MSB first), in
which case you can use ntohs, ntohl, htons and htonl. Alternatively,
you can do things like
x = ((x >> 16) & 0x0000ffff) | ((x << 16) & 0xffff0000);
x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
which will swap the bytes in a 32-bit variable "x". You shouldn't need
to swap bits (that is only necessary for a few specialised
applications).
And anyway, why accumulate this all into one string if all you
are going to do is write it to a file? Easier to read some data,
format it, and write it out to the file before going on to read
the next piece of data.
I would LOVE to write directly to a file, but I've had a few problems:
Have you tried using NSString's -dataUsingEncoding: with the parameter
NSASCIIStringEncoding? That will give you an NSData that you can write
to an NSFileHandle using -writeData:. You might need to use
-dataUsingEncoding:allowLossyConversion: if you've got e.g. accented
characters in your string.
For example
NSString *myString = @"Hello World!\n";
NSFileHandle *myFile = [NSFileHandle
fileHandleForWritingAtPath:@"hello.txt"];
NSData *myData = [myString dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
unsigned n;
for (n = 0; n < 10; ++n)
[myFile write
Data:myData];
should result in a file that says "Hello World!" ten times. (Of
course, the usual warning applies... I haven't tested this code, there
may be bugs.)
Kind regards,
Alastair.
_______________________________________________
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.