---
product: Chilkat
product_version: 11.7.0
language: ckperl
language_label: Perl
class: CkByteData
license: free
deprecated: false
reference_html: https://www.chilkatsoft.com/refdoc/perlCkByteDataRef.html
---
# Chilkat CkByteData — Perl — v11.7.0

> Chilkat 11.7.0 · language=ckperl · class=CkByteData
> Other versions: replace `11.7.0` in this URL. Other languages: `https://www.chilkatsoft.com/ai-docs/v/11.7.0/<lang>/classes/CkByteData.md`.
> Language-wide rules (install, unlock, error model, strings/bytes): `https://www.chilkatsoft.com/ai-docs/v/11.7.0/ckperl/guide.md`.

## Purpose

Represents a chunk of byte data and provides methods for accessing it, adding to it, or saving and loading from files.

## Object creation, unlocking and error handling

**Object Creation**

```perl
$obj = chilkat::CkByteData->new();
```

Call `Global.UnlockBundle` once at the start of the program, before using this or any other Chilkat class. Any string unlocks a fully functional 30-day trial; a purchased unlock code is used in production. See `guide.md` for the exact code in Perl.

How failure is reported depends on the method's return type and is stated in the note under each method below. In every case the diagnostic text is in the `LastErrorText` property of the object whose method was called.

## Member index

| Member | Kind | Signature |
|---|---|---|
| `SecureClear` | property, read/write | `# $boolVal is a boolean $boolVal = $ckByteData->get_SecureClear(); $ckByteData->put_SecureClear($boolVal);` |
| `appendChar` | method | `# $ch is a char $ckByteData->appendChar($ch);` |
| `appendCharN` | method | `# $ch is a char # $numTimes is an integer $ckByteData->appendCharN($ch, $numTimes);` |
| `appendEncoded` | method | `# $str is a string # $encoding is a string $ckByteData->appendEncoded($str, $encoding);` |
| `appendEncodedW` | method | `# $str is a utf-16 string # $encoding is a utf-16 string $ckByteData->appendEncodedW($str, $encoding);` |
| `appendFile` | method | `# $path is a string $retBool = $ckByteData->appendFile($path);` |
| `appendFileW` | method | `# $path is a utf-16 string $retBool = $ckByteData->appendFileW($path);` |
| `appendInt` | method | `# $intValue is an integer # $littleEndian is a boolean $ckByteData->appendInt($intValue, $littleEndian);` |
| `appendRandom` | method | `# $numBytes is an integer $ckByteData->appendRandom($numBytes);` |
| `appendShort` | method | `# $shortValue is an integer # $littleEndian is a boolean $ckByteData->appendShort($shortValue, $littleEndian);` |
| `appendStr` | method | `# $str is a string $ckByteData->appendStr($str);` |
| `appendStrW` | method | `# $str is a utf-16 string # $charset is a utf-16 string $ckByteData->appendStrW($str, $charset);` |
| `byteSwap4321` | method | `$ckByteData->byteSwap4321();` |
| `clear` | method | `$ckByteData->clear();` |
| `dropData` | method | `$ckByteData->dropData();` |
| `encode` | method | `# $encoding is a string # $str is a CkString $ckByteData->encode($encoding, $str);` |
| `encodeW` | method | `# $encoding is a utf-16 string # $str is a CkString $ckByteData->encodeW($encoding, $str);` |
| `ensureBuffer` | method | `# $expectedNumBytes is an integer $status = $ckByteData->ensureBuffer($expectedNumBytes);` |
| `getByte` | method | `# $byteIndex is an integer $retByte = $ckByteData->getByte($byteIndex);` |
| `getChar` | method | `# $byteIndex is an integer $retChar = $ckByteData->getChar($byteIndex);` |
| `getData` | method | `$binary_data = $ckByteData->getData();` |
| `getDataAt` | method | `# $byteIndex is an integer $binary_data = $ckByteData->getDataAt($byteIndex);` |
| `getEncodedW` | method | `# $encoding is a utf-16 string $utf16_text = $ckByteData->getEncodedW($encoding);` |
| `getInt` | method | `# $byteIndex is an integer $retInt = $ckByteData->getInt($byteIndex);` |
| `getRange` | method | `# $byteIndex is an integer # $numBytes is an integer $binary_data = $ckByteData->getRange($byteIndex, $numBytes);` |
| `getSize` | method | `$retInt = $ckByteData->getSize();` |
| `is7bit` | method | `$retBool = $ckByteData->is7bit();` |
| `loadFile` | method | `# $path is a string $retBool = $ckByteData->loadFile($path);` |
| `loadFileW` | method | `# $path is a utf-16 string $retBool = $ckByteData->loadFileW($path);` |
| `pad` | method | `# $blockSize is an integer # $paddingScheme is an integer $ckByteData->pad($blockSize, $paddingScheme);` |
| `removeChunk` | method | `# $startIndex is an integer # $numBytes is an integer $ckByteData->removeChunk($startIndex, $numBytes);` |
| `replaceChar` | method | `# $existingByteValue is a byte # $replacementByteValue is a byte $ckByteData->replaceChar($existingByteValue, $replacementByteValue);` |
| `saveFile` | method | `# $path is a string $retBool = $ckByteData->saveFile($path);` |
| `saveFileW` | method | `# $path is a utf-16 string $retBool = $ckByteData->saveFileW($path);` |
| `shorten` | method | `# $numBytes is an integer $ckByteData->shorten($numBytes);` |
| `to_ws` | method | `# $charset is a string $utf16_text = $ckByteData->to_ws($charset);` |
| `unpad` | method | `# $blockSize is an integer # $paddingScheme is an integer $ckByteData->unpad($blockSize, $paddingScheme);` |

Deprecated members are listed at the end of this file with their replacements; do not use them in new code.

## Properties

### CkByteData.SecureClear

`Chilkat 11.7.0 · ckperl · CkByteData · SecureClear · property · read/write · since 9.5.0.73`

```perl
# $boolVal is a boolean

$boolVal = $ckByteData->get_SecureClear();

$ckByteData->put_SecureClear($boolVal);
```

If set to `1`, then memory is always first overwritten with zero bytes prior to deallocation. The default value of this property is `0`.

## Methods

### CkByteData.appendChar

`Chilkat 11.7.0 · ckperl · CkByteData · appendChar · method`

```perl
# $ch is a char

$ckByteData->appendChar($ch);
```

Appends a single byte.

### CkByteData.appendCharN

`Chilkat 11.7.0 · ckperl · CkByteData · appendCharN · method`

```perl
# $ch is a char

# $numTimes is an integer

$ckByteData->appendCharN($ch, $numTimes);
```

Appends a single char N times. The equivalent of calling appendChar N times.

### CkByteData.appendEncoded

`Chilkat 11.7.0 · ckperl · CkByteData · appendEncoded · method`

```perl
# $str is a string

# $encoding is a string

$ckByteData->appendEncoded($str, $encoding);
```

Appends binary data from an encoded string. The encoding can be specified as `hex`, `base64`, `url`, `quoted-printable`, `modBase64`, `base58`, or `base32`. The input string is decoded from the specified encoding and the binary data is appended to the calling object's content.

### CkByteData.appendEncodedW

`Chilkat 11.7.0 · ckperl · CkByteData · appendEncodedW · method`

```perl
# $str is a utf-16 string

# $encoding is a utf-16 string

$ckByteData->appendEncodedW($str, $encoding);
```

To be documented soon...

### CkByteData.appendFile

`Chilkat 11.7.0 · ckperl · CkByteData · appendFile · method`

```perl
# $path is a string

$retBool = $ckByteData->appendFile($path);
```

Opens a file for binary read, appends the file contents, and closes the file.

### CkByteData.appendFileW

`Chilkat 11.7.0 · ckperl · CkByteData · appendFileW · method`

```perl
# $path is a utf-16 string

$retBool = $ckByteData->appendFileW($path);
```

Opens a file for binary read, appends the file contents, and closes the file.

### CkByteData.appendInt

`Chilkat 11.7.0 · ckperl · CkByteData · appendInt · method`

```perl
# $intValue is an integer

# $littleEndian is a boolean

$ckByteData->appendInt($intValue, $littleEndian);
```

Appends a 32-bit signed integer (4 bytes) to the data already contained in the object. `littleEndian` determines whether the big endian or little endian byte ordering is used.

### CkByteData.appendRandom

`Chilkat 11.7.0 · ckperl · CkByteData · appendRandom · method`

```perl
# $numBytes is an integer

$ckByteData->appendRandom($numBytes);
```

Appends `numBytes` random bytes to the data already contained within the object.

### CkByteData.appendShort

`Chilkat 11.7.0 · ckperl · CkByteData · appendShort · method`

```perl
# $shortValue is an integer

# $littleEndian is a boolean

$ckByteData->appendShort($shortValue, $littleEndian);
```

Appends a 16-bit signed integer (2 bytes) to the data already contained in the object. `littleEndian` determines whether the big endian or little endian byte ordering is used.

### CkByteData.appendStr

`Chilkat 11.7.0 · ckperl · CkByteData · appendStr · method`

```perl
# $str is a string

$ckByteData->appendStr($str);
```

Appends a null-terminated string to the data, without including the terminating null.

### CkByteData.appendStrW

`Chilkat 11.7.0 · ckperl · CkByteData · appendStrW · method`

```perl
# $str is a utf-16 string

# $charset is a utf-16 string

$ckByteData->appendStrW($str, $charset);
```

To be documented soon...

### CkByteData.byteSwap4321

`Chilkat 11.7.0 · ckperl · CkByteData · byteSwap4321 · method`

```perl
$ckByteData->byteSwap4321();
```

4321 byte swaps the data contained within the object.

### CkByteData.clear

`Chilkat 11.7.0 · ckperl · CkByteData · clear · method`

```perl
$ckByteData->clear();
```

Clears the CkByteData object of all data. The internal memory is deallocated. To clear without deallocating, call dropData instead.

### CkByteData.dropData

`Chilkat 11.7.0 · ckperl · CkByteData · dropData · method · since 9.5.0.91`

```perl
$ckByteData->dropData();
```

Sets the size of the data to 0. Does not deallocate the existing internal buffer. (This is a fast way of `clearing` the CkByteData object, such that the existing data remains in memory and is overwritten on the next append.)

If the SecureClear property is set to `1`, then the internal memory buffer is zeroed out before resetting the size to 0.

### CkByteData.encode

`Chilkat 11.7.0 · ckperl · CkByteData · encode · method`

```perl
# $encoding is a string

# $str is a CkString

$ckByteData->encode($encoding, $str);
```

Encodes binary data according to the encoding requested. The encoding can be specified as `hex`, `base64`, `url`, `quoted-printable`, `modBase64`, `base58`, `base32`, `qp-`, `url_rfc1738`, `url_rfc2396`, or `url_rfc3986`.

### CkByteData.encodeW

`Chilkat 11.7.0 · ckperl · CkByteData · encodeW · method`

```perl
# $encoding is a utf-16 string

# $str is a CkString

$ckByteData->encodeW($encoding, $str);
```

To be documented soon...

### CkByteData.ensureBuffer

`Chilkat 11.7.0 · ckperl · CkByteData · ensureBuffer · method`

```perl
# $expectedNumBytes is an integer

$status = $ckByteData->ensureBuffer($expectedNumBytes);
```

This method can be called to help optimize internal memory re-allocation. If, for example, many calls will be made to append data, and the total size is approximately known, then this method can be called to pre-allocate the internal buffer to the expected total size.

If the internal buffer is already larger than the `expectedNumBytes` then nothing happens. The existing internal buffer is kept.

Returns 1 for success, 0 for failure.

### CkByteData.getByte

`Chilkat 11.7.0 · ckperl · CkByteData · getByte · method`

```perl
# $byteIndex is an integer

$retByte = $ckByteData->getByte($byteIndex);
```

Returns the Nth byte of the binary data. The 1st byte is at index 0.

### CkByteData.getChar

`Chilkat 11.7.0 · ckperl · CkByteData · getChar · method`

```perl
# $byteIndex is an integer

$retChar = $ckByteData->getChar($byteIndex);
```

Returns the Nth byte of the binary content as a `char`. The 1st byte is at index 0.

### CkByteData.getData

`Chilkat 11.7.0 · ckperl · CkByteData · getData · method`

```perl
$binary_data = $ckByteData->getData();
```

Returns a pointer to the internal buffer. Be careful with this method because if additional data is appended, the data within the object may be relocated and the pointer may cease to be valid.

### CkByteData.getDataAt

`Chilkat 11.7.0 · ckperl · CkByteData · getDataAt · method`

```perl
# $byteIndex is an integer

$binary_data = $ckByteData->getDataAt($byteIndex);
```

Same as getData, except it gets a pointer to the data at a byte offset. (0 = the start of buffer)

### CkByteData.getEncodedW

`Chilkat 11.7.0 · ckperl · CkByteData · getEncodedW · method`

```perl
# $encoding is a utf-16 string

$utf16_text = $ckByteData->getEncodedW($encoding);
```

The utf-16 version of getEncoded.

### CkByteData.getInt

`Chilkat 11.7.0 · ckperl · CkByteData · getInt · method`

```perl
# $byteIndex is an integer

$retInt = $ckByteData->getInt($byteIndex);
```

Returns the 4-byte integer located at a specific byte index.

### CkByteData.getRange

`Chilkat 11.7.0 · ckperl · CkByteData · getRange · method`

```perl
# $byteIndex is an integer

# $numBytes is an integer

$binary_data = $ckByteData->getRange($byteIndex, $numBytes);
```

Copies a range of bytes to a separate internal memory buffer and returns the pointer to the bytes. The returned pointer is only valid while the object exists. Also, any subsequent calls to getRange, getRangeStr, or to_s will invalidate the buffer.

### CkByteData.getSize

`Chilkat 11.7.0 · ckperl · CkByteData · getSize · method`

```perl
$retInt = $ckByteData->getSize();
```

Returns the number of bytes in the data buffer.

### CkByteData.is7bit

`Chilkat 11.7.0 · ckperl · CkByteData · is7bit · method`

```perl
$retBool = $ckByteData->is7bit();
```

Returns `1` if all the bytes are in the range 0x00 to 0x7F.

### CkByteData.loadFile

`Chilkat 11.7.0 · ckperl · CkByteData · loadFile · method`

```perl
# $path is a string

$retBool = $ckByteData->loadFile($path);
```

Equivalent to clear() followed by appendFile().

### CkByteData.loadFileW

`Chilkat 11.7.0 · ckperl · CkByteData · loadFileW · method`

```perl
# $path is a utf-16 string

$retBool = $ckByteData->loadFileW($path);
```

To be documented soon...

### CkByteData.pad

`Chilkat 11.7.0 · ckperl · CkByteData · pad · method`

```perl
# $blockSize is an integer

# $paddingScheme is an integer

$ckByteData->pad($blockSize, $paddingScheme);
```

Pads the data to a multiple of the `blockSize` using a cryptographic padding scheme specified by `paddingScheme`. The possible integer values for `paddingScheme` are the same as those listed for the PaddingScheme property of the CkCrypt2 class.

### CkByteData.removeChunk

`Chilkat 11.7.0 · ckperl · CkByteData · removeChunk · method`

```perl
# $startIndex is an integer

# $numBytes is an integer

$ckByteData->removeChunk($startIndex, $numBytes);
```

Removes (discards) a range from the data.

### CkByteData.replaceChar

`Chilkat 11.7.0 · ckperl · CkByteData · replaceChar · method`

```perl
# $existingByteValue is a byte

# $replacementByteValue is a byte

$ckByteData->replaceChar($existingByteValue, $replacementByteValue);
```

Replaces all occurrences of `existingByteValue` with `replacementByteValue`.

### CkByteData.saveFile

`Chilkat 11.7.0 · ckperl · CkByteData · saveFile · method`

```perl
# $path is a string

$retBool = $ckByteData->saveFile($path);
```

Saves the byte data to a file. If the file already exists, it will be overwritten.

### CkByteData.saveFileW

`Chilkat 11.7.0 · ckperl · CkByteData · saveFileW · method`

```perl
# $path is a utf-16 string

$retBool = $ckByteData->saveFileW($path);
```

To be documented soon...

### CkByteData.shorten

`Chilkat 11.7.0 · ckperl · CkByteData · shorten · method`

```perl
# $numBytes is an integer

$ckByteData->shorten($numBytes);
```

Discards N bytes from the end of the data.

### CkByteData.to_ws

`Chilkat 11.7.0 · ckperl · CkByteData · to_ws · method`

```perl
# $charset is a string

$utf16_text = $ckByteData->to_ws($charset);
```

To be documented soon...

### CkByteData.unpad

`Chilkat 11.7.0 · ckperl · CkByteData · unpad · method`

```perl
# $blockSize is an integer

# $paddingScheme is an integer

$ckByteData->unpad($blockSize, $paddingScheme);
```

Unpads the data from a multiple of the `blockSize` to the original data size using a cryptographic padding scheme specified by `paddingScheme`. The possible integer values for `paddingScheme` are the same as those listed for the PaddingScheme property of the CkCrypt2 class.

## Deprecated members — do not use in new code

| Member | Kind | Deprecated in | Use instead |
|---|---|---|---|
| `append` | method | before 11.6.1 | — |
| `appendRange` | method | before 11.6.1 | — |
| `beginsWith` | method | before 11.6.1 | — |
| `equals` | method | before 11.6.1 | — |
| `findBytes` | method | before 11.6.1 | — |
| `getBytes` | method | before 11.6.1 | — |
| `preAllocate` | method | before 11.6.1 | — |

## Related classes

[`CkString`](https://www.chilkatsoft.com/ai-docs/v/11.7.0/ckperl/classes/CkString.md)

