• 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: default C String Encoding
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: default C String Encoding


  • Subject: Re: default C String Encoding
  • From: Julien Jalon <email@hidden>
  • Date: Thu, 8 Nov 2001 19:36:46 +0100

Le mercredi 7 novembre 2001, ` 07:54 PM, Max Horn a icrit :
> I think this is very bad code. It basically acts like strdup,
> implicitly allocating a buffer. I think in most cases you are much
> better to use code like this:
>
> NSData *data = [myString dataUsingEncoding:NSISOLatin1StringEncoding
> allowLossyConversion:YES];
>
> myCFunction([data bytes]);

No... [data bytes] is not a valid C string. It lacks the '\0' at the
end. (and I forgot to add it in my code :-)

> There are very few cases in which you will want a method like the one
> above, I think; most of the time you either want to use a c string only
> once or maybe a few times in a row. In the cases were you actually want
> to store them for a longer time, I'd ask myself if it is really a good
> way to do it like that. Why not store the NSString, instead of a lossy
> converted result?

you need those methods every time you want to interface your application
with classic C libraries... it's the case for Adium and Fire and many
other front-end application which does not support correctly the accents
because of the lack ot those methods...

Le mercredi 7 novembre 2001, ` 08:26 PM, David Young a icrit :
[...]
> just replace malloc() with:
>
> returnValue = [[NSMutableData dataWithLength:dataLength+1] mutableBytes]
>
> or you could use NSAutoreleasedMallocMemory() or whatever that call is.

good idea [NSMutableData ...]
didn't find NSAutoreleasedMallocMemory (or whatever...)

So after all this discussion, I rewrote my category with the general
encoding C string additions (and for the ones interested, the specific
ones for ISOLatin1 encoding):

[demime 0.98b removed an attachment of type application/octet-stream which had a name of NSStringEncodedCStringAdditions.m]
/*
* NSStringEncodedCStringAdditions.h
* TestEncoding
*
* Created by jalon on Thu Nov 08 2001.
* Copyright (c) 2001 Julien Jalon. All rights reserved.
*
*/

#import <Foundation/Foundation.h>

@interface NSString (EncodedCStringAdditions)

+ (NSString *)stringWithCString:(const char *)cString encoding:(NSStringEncoding)encoding;
- (const char *)cStringUsingEncoding:(NSStringEncoding)encoding;

+ (NSString *)stringWithISOLatin1EncodedCString:(const char *)cString;
- (const char *)ISOLatin1EncodedCString;

@end

const char *convertCString(const char *cString, NSStringEncoding encoding, NSStringEncoding toEncoding);
you can still complete them with the -getCString: if you want.

I also added a little C function to convert a C string to the same C
string using a different encoding. Where FoundationKit enhances plain
C :-)

--Julien


  • Prev by Date: Re: Does cocoa allow string resource
  • Next by Date: Re: [Newbie] Basic file handling questions...
  • Previous by thread: Re: default C String Encoding
  • Next by thread: "incompatible type" queerness
  • Index(es):
    • Date
    • Thread