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

# getCString(_:maxLength:encoding:)

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

```
func getCString(_ buffer: UnsafeMutablePointer<CChar>, maxLength maxBufferCount: Int, encoding: UInt) -> Bool
```

## Parameters

`buffer`

Upon return, contains the converted C-string plus the `NULL` termination byte. The buffer must include room for `maxBufferCount` bytes.

`maxBufferCount`

The maximum number of bytes in the string to return in buffer (*including* the `NULL` termination byte).

`encoding`

The encoding for the returned C string. For possible values, see [`NSStringEncoding`](/documentation/Foundation/NSStringEncoding).

## Return Value

<doc://com.apple.documentation/documentation/Swift/true> if the operation was successful, otherwise <doc://com.apple.documentation/documentation/Swift/false>. Returns <doc://com.apple.documentation/documentation/Swift/false> if conversion is not possible due to encoding errors or if `buffer` is too small.

## Discussion

Note that in the treatment of the `maxBufferCount` argument, this method differs from the deprecated [`getCString(_:maxLength:)`](/documentation/Foundation/NSString/getCString(_:maxLength:)) method which it replaces. (The buffer should include room for `maxBufferCount` bytes; this number should accommodate the expected size of the return value plus the `NULL` termination byte, which this method adds.)

You can use [`canBeConverted(to:)`](/documentation/Foundation/NSString/canBeConverted(to:)) to check whether a string can be losslessly converted to `encoding`. If it can’t, you can use [`data(using:allowLossyConversion:)`](/documentation/Foundation/NSString/data(using:allowLossyConversion:)) to get a C-string representation using `encoding`, allowing some loss of information (note that the data returned by [`data(using:allowLossyConversion:)`](/documentation/Foundation/NSString/data(using:allowLossyConversion:)) is not a strict C-string since it does not have a `NULL` terminator).

## See Also

[`canBeConverted(to:)`](/documentation/Foundation/NSString/canBeConverted(to:))

Returns a Boolean value that indicates whether the receiver can be converted to a given encoding without loss of information.



---

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)