• 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: char Array to NSString to cString?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: char Array to NSString to cString?


  • Subject: Re: char Array to NSString to cString?
  • From: Douglas Davidson <email@hidden>
  • Date: Fri, 1 Jun 2007 08:38:18 -0700


On Jun 1, 2007, at 8:15 AM, Vince Ackerman wrote:

I'm having a bit of trouble trying to convert an array of 7 (unsigned char) to a NSString. I need to pass this array of hex values to another object that is expecting an NSString that it will convert to a C string and send it out the serial port using this function:

-(int)writeString:(NSString *)string
{
return write(fileDescriptor, [string cString], [string cStringLength]);
}


I need it to write these hex values exactly without any conversion or encoding. I'm getting a bit lost (as a Newbie) as to how to convert from an array to NSString then back again to c string since 10.4 deprecates all the cString methods. The methods I've tried seem to be changing the hex values.

This is an example of hex numbers in myArray:

(unsigned char) myArray[7];

0xff 0x01 0x00 0x02 0x20 0x00 0x23


Can anyone help me understand how to do this?

If you want to write an arbitrary sequence of bytes without conversion or encoding, then NSString is not the right object. NSData is the object used to represent arbitrary sequences of bytes. NSString represents a string, conceptually a sequence of Unicode characters.


A string cannot be converted to or from a sequence of bytes without reference to some encoding. There are encodings that can in principle be used to interpret arbitrary sequences of bytes, but I cannot recommend doing so; furthermore, in practice there are almost always restrictions--for example, anything involving C strings is not likely be able to handle the embedded nulls in your example. In particular, if you use cString, you definitely will not be able to rely on the results, since they will depend on the user's default encoding.

Douglas Davidson


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >char Array to NSString to cString? (From: Vince Ackerman <email@hidden>)

  • Prev by Date: Re: char Array to NSString to cString?
  • Next by Date: NSSegmentedCell in table column
  • Previous by thread: Re: char Array to NSString to cString?
  • Next by thread: Re: char Array to NSString to cString?
  • Index(es):
    • Date
    • Thread