---
product: Chilkat
product_version: 11.7.0
language: phpext
language_label: PHP Extension
class: CkByteData
license: free
deprecated: false
reference_html: https://www.chilkatsoft.com/refdoc/phpCkByteDataRef.html
---
# Chilkat CkByteData — PHP Extension — v11.7.0

> Chilkat 11.7.0 · language=phpext · 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/phpext/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**

```php
$obj = new CkByteData();
```

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 PHP Extension.

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 | `bool get_SecureClear() void put_SecureClear(bool boolVal);` |
| `appendChar` | method | `void appendChar(char $ch);` |
| `appendCharN` | method | `void appendCharN(char $ch, int $numTimes);` |
| `appendEncoded` | method | `void appendEncoded(string $str, string $encoding);` |
| `appendEncodedW` | method | `void appendEncodedW((utf-16) string $str, (utf-16) string $encoding);` |
| `appendFile` | method | `bool appendFile(string $path);` |
| `appendFileW` | method | `bool appendFileW((utf-16) string $path);` |
| `appendInt` | method | `void appendInt(int $intValue, bool $littleEndian);` |
| `appendRandom` | method | `void appendRandom(int $numBytes);` |
| `appendShort` | method | `void appendShort(int $shortValue, bool $littleEndian);` |
| `appendStr` | method | `void appendStr(string $str);` |
| `appendStrW` | method | `void appendStrW((utf-16) string $str, (utf-16) string $charset);` |
| `byteSwap4321` | method | `void byteSwap4321();` |
| `clear` | method | `void clear();` |
| `dropData` | method | `void dropData();` |
| `encode` | method | `void encode(string $encoding, CkString $str);` |
| `encodeW` | method | `void encodeW((utf-16) string $encoding, CkString $str);` |
| `ensureBuffer` | method | `bool ensureBuffer(int $expectedNumBytes);` |
| `getByte` | method | `byte getByte(int $byteIndex);` |
| `getChar` | method | `char getChar(int $byteIndex);` |
| `getData` | method | `(binary) string getData();` |
| `getDataAt` | method | `(binary) string getDataAt(int $byteIndex);` |
| `getEncodedW` | method | `(utf-16) string getEncodedW((utf-16) string $encoding);` |
| `getInt` | method | `int getInt(int $byteIndex);` |
| `getRange` | method | `(binary) string getRange(int $byteIndex, int $numBytes);` |
| `getSize` | method | `int getSize();` |
| `is7bit` | method | `bool is7bit();` |
| `loadFile` | method | `bool loadFile(string $path);` |
| `loadFileW` | method | `bool loadFileW((utf-16) string $path);` |
| `pad` | method | `void pad(int $blockSize, int $paddingScheme);` |
| `removeChunk` | method | `void removeChunk(int $startIndex, int $numBytes);` |
| `replaceChar` | method | `void replaceChar(byte $existingByteValue, byte $replacementByteValue);` |
| `saveFile` | method | `bool saveFile(string $path);` |
| `saveFileW` | method | `bool saveFileW((utf-16) string $path);` |
| `shorten` | method | `void shorten(int $numBytes);` |
| `to_ws` | method | `(utf-16) string to_ws(string $charset);` |
| `unpad` | method | `void unpad(int $blockSize, int $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 · phpext · CkByteData · SecureClear · property · read/write · since 9.5.0.73`

```php
bool get_SecureClear()

void put_SecureClear(bool boolVal);
```

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

## Methods

### CkByteData.appendChar

`Chilkat 11.7.0 · phpext · CkByteData · appendChar · method`

```php
void appendChar(char $ch);
```

Appends a single byte.

### CkByteData.appendCharN

`Chilkat 11.7.0 · phpext · CkByteData · appendCharN · method`

```php
void appendCharN(char $ch, int $numTimes);
```

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

### CkByteData.appendEncoded

`Chilkat 11.7.0 · phpext · CkByteData · appendEncoded · method`

```php
void appendEncoded(string $str, string $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 · phpext · CkByteData · appendEncodedW · method`

```php
void appendEncodedW((utf-16) string $str, (utf-16) string $encoding);
```

To be documented soon...

### CkByteData.appendFile

`Chilkat 11.7.0 · phpext · CkByteData · appendFile · method`

```php
bool appendFile(string $path);
```

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

### CkByteData.appendFileW

`Chilkat 11.7.0 · phpext · CkByteData · appendFileW · method`

```php
bool appendFileW((utf-16) string $path);
```

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

### CkByteData.appendInt

`Chilkat 11.7.0 · phpext · CkByteData · appendInt · method`

```php
void appendInt(int $intValue, bool $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 · phpext · CkByteData · appendRandom · method`

```php
void appendRandom(int $numBytes);
```

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

### CkByteData.appendShort

`Chilkat 11.7.0 · phpext · CkByteData · appendShort · method`

```php
void appendShort(int $shortValue, bool $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 · phpext · CkByteData · appendStr · method`

```php
void appendStr(string $str);
```

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

### CkByteData.appendStrW

`Chilkat 11.7.0 · phpext · CkByteData · appendStrW · method`

```php
void appendStrW((utf-16) string $str, (utf-16) string $charset);
```

To be documented soon...

### CkByteData.byteSwap4321

`Chilkat 11.7.0 · phpext · CkByteData · byteSwap4321 · method`

```php
void byteSwap4321();
```

4321 byte swaps the data contained within the object.

### CkByteData.clear

`Chilkat 11.7.0 · phpext · CkByteData · clear · method`

```php
void 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 · phpext · CkByteData · dropData · method · since 9.5.0.91`

```php
void 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 `true`, then the internal memory buffer is zeroed out before resetting the size to 0.

### CkByteData.encode

`Chilkat 11.7.0 · phpext · CkByteData · encode · method`

```php
void encode(string $encoding, CkString $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 · phpext · CkByteData · encodeW · method`

```php
void encodeW((utf-16) string $encoding, CkString $str);
```

To be documented soon...

### CkByteData.ensureBuffer

`Chilkat 11.7.0 · phpext · CkByteData · ensureBuffer · method`

```php
bool ensureBuffer(int $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 true for success, false for failure.

### CkByteData.getByte

`Chilkat 11.7.0 · phpext · CkByteData · getByte · method`

```php
byte getByte(int $byteIndex);
```

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

### CkByteData.getChar

`Chilkat 11.7.0 · phpext · CkByteData · getChar · method`

```php
char getChar(int $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 · phpext · CkByteData · getData · method`

```php
(binary) string 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 · phpext · CkByteData · getDataAt · method`

```php
(binary) string getDataAt(int $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 · phpext · CkByteData · getEncodedW · method`

```php
(utf-16) string getEncodedW((utf-16) string $encoding);
```

The utf-16 version of getEncoded.

### CkByteData.getInt

`Chilkat 11.7.0 · phpext · CkByteData · getInt · method`

```php
int getInt(int $byteIndex);
```

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

### CkByteData.getRange

`Chilkat 11.7.0 · phpext · CkByteData · getRange · method`

```php
(binary) string getRange(int $byteIndex, int $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 · phpext · CkByteData · getSize · method`

```php
int getSize();
```

Returns the number of bytes in the data buffer.

### CkByteData.is7bit

`Chilkat 11.7.0 · phpext · CkByteData · is7bit · method`

```php
bool is7bit();
```

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

### CkByteData.loadFile

`Chilkat 11.7.0 · phpext · CkByteData · loadFile · method`

```php
bool loadFile(string $path);
```

Equivalent to clear() followed by appendFile().

### CkByteData.loadFileW

`Chilkat 11.7.0 · phpext · CkByteData · loadFileW · method`

```php
bool loadFileW((utf-16) string $path);
```

To be documented soon...

### CkByteData.pad

`Chilkat 11.7.0 · phpext · CkByteData · pad · method`

```php
void pad(int $blockSize, int $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 · phpext · CkByteData · removeChunk · method`

```php
void removeChunk(int $startIndex, int $numBytes);
```

Removes (discards) a range from the data.

### CkByteData.replaceChar

`Chilkat 11.7.0 · phpext · CkByteData · replaceChar · method`

```php
void replaceChar(byte $existingByteValue, byte $replacementByteValue);
```

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

### CkByteData.saveFile

`Chilkat 11.7.0 · phpext · CkByteData · saveFile · method`

```php
bool saveFile(string $path);
```

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

### CkByteData.saveFileW

`Chilkat 11.7.0 · phpext · CkByteData · saveFileW · method`

```php
bool saveFileW((utf-16) string $path);
```

To be documented soon...

### CkByteData.shorten

`Chilkat 11.7.0 · phpext · CkByteData · shorten · method`

```php
void shorten(int $numBytes);
```

Discards N bytes from the end of the data.

### CkByteData.to_ws

`Chilkat 11.7.0 · phpext · CkByteData · to_ws · method`

```php
(utf-16) string to_ws(string $charset);
```

To be documented soon...

### CkByteData.unpad

`Chilkat 11.7.0 · phpext · CkByteData · unpad · method`

```php
void unpad(int $blockSize, int $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/phpext/classes/CkString.md)

