• 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: Creating an NSString from a data buffer
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Creating an NSString from a data buffer


  • Subject: Re: Creating an NSString from a data buffer
  • From: David Remahl <email@hidden>
  • Date: Wed, 18 Dec 2002 06:18:26 +0100

Pascal,

First you have to know what encoding your data is in. The method you are using, +stringWithCharacters:length: has the following definition:

+ (id)stringWithCharacters:(const unichar *)characters length:(unsigned)length;

That means that it takes on array of unichar's, ie 2 byte characters. _IF_ the data pointed to by buffer really is valid unicode with 2 bytes per character, then the correct method call would be:

[NSString stringWithCharacters:(unichar *)buffer
length:(end-begin) / 2];

I don't know what you did to get the exception, but I think you must have made some spelling mistake, or left out length.

However, it is more likely that what you really want is the +[NSString stringWithData:(NSData *)data encoding:(NSStringEncoding)encoding] which will allow you to specify the string encoding.

I also think you should study the distinction between class ('+') and instance ('-') methods, so that deciding wether to have an alloc in there does not have to be guess work.

/ Regards, David Remahl

On Wednesday, December 18, 2002, at 05:47 AM, email@hidden wrote:

Hello!

Sorry for this newbie question, but I have been around
for quite a while, and I think I tried everything but
the
right thing.

I am building an object that can read / write a file
format that contains strings and binary codes.

At one point in the program, I have a buffer, which is
valid and contains the data I want. I want to extract an
unicode string from it. After having found the begin
and end positions of the string inside of the data
buffer, I would likw to create an NSString from it.
------
char * buffer; // My data buffer
NSString * S; // One NSString into which I want to
store data

char *begin;
char *end;

// At this pont, begin and end have been found and have
// the right values (end > begin)

// I tried, among many other things:
S = [NSString stringWithCharacters:begin
length:(end-begin)];
------
I tried the same, but with an alloc before calling
stringWithCharacters, etc...

Project builder keeps telling me that this method
(stringWithCharacters) cannot be found, although it
is in NSString.h:

+[NSString stringWithCharacters::]: selector not
recognized

Do I have to specify that I want to use some protocol?
Or some category?
I also thought I could maybe put all my data into an
NSString, maybe mutable, but my data does not correspond
to a string encoding...

Thanks for any help.

Pascal
_______________________________________________
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.

  • Prev by Date: Re: Creating an NSString from a data buffer
  • Next by Date: Bug in TextEdit
  • Previous by thread: Re: Creating an NSString from a data buffer
  • Next by thread: Bug in TextEdit
  • Index(es):
    • Date
    • Thread