<!--
{
  "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/getBytes(_:maxLength:usedLength:encoding:options:range:remaining:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSString(im)getBytes:maxLength:usedLength:encoding:options:range:remainingRange:"
  },
  "title" : "getBytes(_:maxLength:usedLength:encoding:options:range:remaining:)"
}
-->

# getBytes(_:maxLength:usedLength:encoding:options:range:remaining:)

Gets a given range of characters as bytes in a specified encoding.

```
func getBytes(_ buffer: UnsafeMutableRawPointer?, maxLength maxBufferCount: Int, usedLength usedBufferCount: UnsafeMutablePointer<Int>?, encoding: UInt, options: NSString.EncodingConversionOptions = [], range: NSRange, remaining leftover: NSRangePointer?) -> Bool
```

## Parameters

`buffer`

A buffer into which to store the bytes from the receiver. The returned bytes are *not* `NULL`-terminated.

`maxBufferCount`

The maximum number of bytes to write to `buffer`.

`usedBufferCount`

The number of bytes used from `buffer`. Pass `NULL` if you do not need this value.

`encoding`

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

`options`

A mask to specify options to use for converting the receiver’s contents to `encoding` (if conversion is necessary).

`range`

The range of characters in the receiver to get.

`leftover`

The remaining range. Pass `NULL` If you do not need this value.

## Return Value

<doc://com.apple.documentation/documentation/Swift/true> if some characters were converted, otherwise <doc://com.apple.documentation/documentation/Swift/false>.

## Discussion

Conversion might stop when the buffer fills, but it might also stop when the conversion isn’t possible due to the chosen encoding.

---

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)