<!--
{
  "availability" : [
    "iOS: 2.0.0 - 2.0.0",
    "iPadOS: 2.0.0 - 2.0.0",
    "tvOS: 9.0.0 - 9.0.0",
    "visionOS: 1.0.0 - 1.0.0",
    "watchOS: 2.0.0 - 2.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSString/getCString(_:maxLength:range:remaining:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSString(im)getCString:maxLength:range:remainingRange:"
  },
  "title" : "getCString(_:maxLength:range:remaining:)"
}
-->

# getCString(_:maxLength:range:remaining:)

Converts the receiver’s content to the default C-string encoding and stores them in a given buffer.

```
func getCString(_ bytes: UnsafeMutablePointer<CChar>, maxLength: Int, range aRange: NSRange, remaining leftoverRange: NSRangePointer?)
```

## Discussion

`buffer` must be large enough to contain `maxLength` bytes plus a terminating zero character (which this method adds). Copies and converts as many characters as possible from `aRange` and stores the range of those not converted in the range given by `leftoverRange` (if it’s non-`nil`). Raises an `NSRangeException` if any part of `aRange` lies beyond the end of the string.

Raises an `NSCharacterConversionException` if the receiver can’t be represented in the default C-string encoding without loss of information. Use [`canBeConverted(to:)`](/documentation/Foundation/NSString/canBeConverted(to:)) if necessary to check whether a string can be losslessly converted to the default C-string encoding. If it can’t, use [`lossyCString()`](/documentation/Foundation/NSString/lossyCString()) or [`data(using:allowLossyConversion:)`](/documentation/Foundation/NSString/data(using:allowLossyConversion:)) to get a C-string representation with some loss of information.

## See Also

[`-  cStringUsingEncoding:`](/documentation/Foundation/NSString/cString(using:))

Returns a representation of the string as a C string using a given encoding.

[`UTF8String`](/documentation/Foundation/NSString/utf8String)

A null-terminated UTF8 representation of the string.

[`-  getCString:maxLength:encoding:`](/documentation/Foundation/NSString/getCString(_:maxLength:encoding:))

Converts the string to a given encoding and stores it in a buffer.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)