---
product: Chilkat
product_version: 11.7.0
language: cpp
language_label: C++
class: CkByteData
license: free
deprecated: false
reference_html: https://www.chilkatsoft.com/refdoc/vcCkByteDataRef.html
---
# Chilkat CkByteData — C++ — v11.7.0

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

```cpp
// Local variable on the stack
CkByteData obj;

// Dynamically allocate/delete
CkByteData *pObj = new CkByteData();
// ...
delete pObj;
```

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 C++.

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); void put_SecureClear(bool newVal);` |
| `append2` | method | `void append2(const void *pByteData, unsigned long szByteData);` |
| `appendChar` | method | `void appendChar(char ch);` |
| `appendCharN` | method | `void appendCharN(char ch, int numTimes);` |
| `appendEncoded` | method | `void appendEncoded(const char *str, const char *encoding);` |
| `appendEncodedW` | method | `void appendEncodedW(const wchar_t *str, const wchar_t *encoding);` |
| `appendFile` | method | `bool appendFile(const char *path);` |
| `appendFileW` | method | `bool appendFileW(const wchar_t *path);` |
| `appendInt` | method | `void appendInt(int intValue, bool littleEndian);` |
| `appendRandom` | method | `void appendRandom(int numBytes);` |
| `appendShort` | method | `void appendShort(short shortValue, bool littleEndian);` |
| `appendStr` | method | `void appendStr(const char *str);` |
| `appendStrW` | method | `void appendStrW(const wchar_t *str, const wchar_t *charset);` |
| `beginsWith2` | method | `bool beginsWith2(const void *pByteData, unsigned long szByteData);` |
| `borrowData` | method | `void borrowData(const void *pByteData, unsigned long szByteData);` |
| `byteSwap4321` | method | `void byteSwap4321(void);` |
| `clear` | method | `void clear(void);` |
| `dropData` | method | `void dropData(void);` |
| `encode` | method | `void encode(const char *encoding, CkString &str);` |
| `encodeW` | method | `void encodeW(const wchar_t *encoding, CkString &str);` |
| `ensureBuffer` | method | `bool ensureBuffer(unsigned long expectedNumBytes);` |
| `equals2` | method | `bool equals2(const void *pCompareBytes, unsigned long numBytes);` |
| `findBytes2` | method | `int findBytes2(const void *findBytes, unsigned long findBytesLen);` |
| `getByte` | method | `unsigned char getByte(unsigned long byteIndex);` |
| `getChar` | method | `char getChar(unsigned long byteIndex);` |
| `getData` | method | `const void *getData(void);` |
| `getDataAt` | method | `const void *getDataAt(unsigned long byteIndex);` |
| `getEncoded` | method | `const char *getEncoded(const char *encoding);` |
| `getEncodedW` | method | `const wchar_t *getEncodedW(const wchar_t *encoding);` |
| `getInt` | method | `int getInt(unsigned long byteIndex);` |
| `getRange` | method | `const void *getRange(unsigned long byteIndex, unsigned long numBytes);` |
| `getSize` | method | `unsigned long getSize(void);` |
| `getUInt` | method | `unsigned long getUInt(unsigned long byteIndex);` |
| `is7bit` | method | `bool is7bit(void);` |
| `loadFile` | method | `bool loadFile(const char *path);` |
| `loadFileW` | method | `bool loadFileW(const wchar_t *path);` |
| `pad` | method | `void pad(int blockSize, int paddingScheme);` |
| `removeChunk` | method | `void removeChunk(unsigned long startIndex, unsigned long numBytes);` |
| `removeData` | method | `const void *removeData(void);` |
| `replaceChar` | method | `void replaceChar(unsigned char existingByteValue, unsigned char replacementByteValue);` |
| `saveFile` | method | `bool saveFile(const char *path);` |
| `saveFileW` | method | `bool saveFileW(const wchar_t *path);` |
| `shorten` | method | `void shorten(unsigned long numBytes);` |
| `to_ws` | method | `const wchar_t *to_ws(const char *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 · cpp · CkByteData · SecureClear · property · read/write · since 9.5.0.73`

```cpp
bool get_SecureClear(void);

void put_SecureClear(bool newVal);
```

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.append2

`Chilkat 11.7.0 · cpp · CkByteData · append2 · method`

```cpp
void append2(const void *pByteData, unsigned long szByteData);
```

Appends bytes to the data already contained in the object.

### CkByteData.appendChar

`Chilkat 11.7.0 · cpp · CkByteData · appendChar · method`

```cpp
void appendChar(char ch);
```

Appends a single byte.

### CkByteData.appendCharN

`Chilkat 11.7.0 · cpp · CkByteData · appendCharN · method`

```cpp
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 · cpp · CkByteData · appendEncoded · method`

```cpp
void appendEncoded(const char *str, const char *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 · cpp · CkByteData · appendEncodedW · method`

```cpp
void appendEncodedW(const wchar_t *str, const wchar_t *encoding);
```

To be documented soon...

### CkByteData.appendFile

`Chilkat 11.7.0 · cpp · CkByteData · appendFile · method`

```cpp
bool appendFile(const char *path);
```

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

### CkByteData.appendFileW

`Chilkat 11.7.0 · cpp · CkByteData · appendFileW · method`

```cpp
bool appendFileW(const wchar_t *path);
```

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

### CkByteData.appendInt

`Chilkat 11.7.0 · cpp · CkByteData · appendInt · method`

```cpp
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 · cpp · CkByteData · appendRandom · method`

```cpp
void appendRandom(int numBytes);
```

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

### CkByteData.appendShort

`Chilkat 11.7.0 · cpp · CkByteData · appendShort · method`

```cpp
void appendShort(short 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 · cpp · CkByteData · appendStr · method`

```cpp
void appendStr(const char *str);
```

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

### CkByteData.appendStrW

`Chilkat 11.7.0 · cpp · CkByteData · appendStrW · method`

```cpp
void appendStrW(const wchar_t *str, const wchar_t *charset);
```

To be documented soon...

### CkByteData.beginsWith2

`Chilkat 11.7.0 · cpp · CkByteData · beginsWith2 · method`

```cpp
bool beginsWith2(const void *pByteData, unsigned long szByteData);
```

Returns `true` if the caller's data begins with specified bytes.

### CkByteData.borrowData

`Chilkat 11.7.0 · cpp · CkByteData · borrowData · method`

```cpp
void borrowData(const void *pByteData, unsigned long szByteData);
```

Sets the CkByteData's data pointer and size to memory outside the object. This is useful in cases where a CkByteData object is needed, but copying data into the CkByteData is not desired.

**Note:** The borrowData method can only be used for data that is being passed into a Chilkat method call. It is not for use as a buffer for Chilkat to deposit data. This is not the intent of the method.

### CkByteData.byteSwap4321

`Chilkat 11.7.0 · cpp · CkByteData · byteSwap4321 · method`

```cpp
void byteSwap4321(void);
```

4321 byte swaps the data contained within the object.

### CkByteData.clear

`Chilkat 11.7.0 · cpp · CkByteData · clear · method`

```cpp
void clear(void);
```

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 · cpp · CkByteData · dropData · method · since 9.5.0.91`

```cpp
void dropData(void);
```

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 · cpp · CkByteData · encode · method`

```cpp
void encode(const char *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 · cpp · CkByteData · encodeW · method`

```cpp
void encodeW(const wchar_t *encoding, CkString &str);
```

To be documented soon...

### CkByteData.ensureBuffer

`Chilkat 11.7.0 · cpp · CkByteData · ensureBuffer · method`

```cpp
bool ensureBuffer(unsigned long 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.equals2

`Chilkat 11.7.0 · cpp · CkByteData · equals2 · method`

```cpp
bool equals2(const void *pCompareBytes, unsigned long numBytes);
```

Returns `true` if the contents of this CkByteData object is exactly equal in both size and content to the bytes passed in `pCompareBytes` and the size passed in `numBytes`. Otherwise returns `false`.

Note: Call beginsWith2 to test if the 1st N bytes are equal.

### CkByteData.findBytes2

`Chilkat 11.7.0 · cpp · CkByteData · findBytes2 · method`

```cpp
int findBytes2(const void *findBytes, unsigned long findBytesLen);
```

Locates the first occurrence of the specified bytes and returns the index of where these bytes occur in the caller's data. Returns -1 if not found.

### CkByteData.getByte

`Chilkat 11.7.0 · cpp · CkByteData · getByte · method`

```cpp
unsigned char getByte(unsigned long byteIndex);
```

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

### CkByteData.getChar

`Chilkat 11.7.0 · cpp · CkByteData · getChar · method`

```cpp
char getChar(unsigned long 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 · cpp · CkByteData · getData · method`

```cpp
const void *getData(void);
```

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 · cpp · CkByteData · getDataAt · method`

```cpp
const void *getDataAt(unsigned long byteIndex);
```

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

### CkByteData.getEncoded

`Chilkat 11.7.0 · cpp · CkByteData · getEncoded · method · since 9.5.0.47`

```cpp
const char *getEncoded(const char *encoding);
```

Returns the binary data as an encoded string. The encoding may be `base64`, `hex`, `quoted-printable` (or `qp`), `url`, `asc`, `url_rfc1738`, `url_rfc2396`, `url_rfc3986`, or `url_oauth`.

### CkByteData.getEncodedW

`Chilkat 11.7.0 · cpp · CkByteData · getEncodedW · method`

```cpp
const wchar_t *getEncodedW(const wchar_t *encoding);
```

The utf-16 version of getEncoded.

### CkByteData.getInt

`Chilkat 11.7.0 · cpp · CkByteData · getInt · method`

```cpp
int getInt(unsigned long byteIndex);
```

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

### CkByteData.getRange

`Chilkat 11.7.0 · cpp · CkByteData · getRange · method`

```cpp
const void *getRange(unsigned long byteIndex, unsigned long 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 · cpp · CkByteData · getSize · method`

```cpp
unsigned long getSize(void);
```

Returns the number of bytes in the data buffer.

### CkByteData.getUInt

`Chilkat 11.7.0 · cpp · CkByteData · getUInt · method`

```cpp
unsigned long getUInt(unsigned long byteIndex);
```

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

### CkByteData.is7bit

`Chilkat 11.7.0 · cpp · CkByteData · is7bit · method`

```cpp
bool is7bit(void);
```

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

### CkByteData.loadFile

`Chilkat 11.7.0 · cpp · CkByteData · loadFile · method`

```cpp
bool loadFile(const char *path);
```

Equivalent to clear() followed by appendFile().

### CkByteData.loadFileW

`Chilkat 11.7.0 · cpp · CkByteData · loadFileW · method`

```cpp
bool loadFileW(const wchar_t *path);
```

To be documented soon...

### CkByteData.pad

`Chilkat 11.7.0 · cpp · CkByteData · pad · method`

```cpp
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 · cpp · CkByteData · removeChunk · method`

```cpp
void removeChunk(unsigned long startIndex, unsigned long numBytes);
```

Removes (discards) a range from the data.

### CkByteData.removeData

`Chilkat 11.7.0 · cpp · CkByteData · removeData · method`

```cpp
const void *removeData(void);
```

Removes the data from the CkByteData object. The caller will receive a pointer to the memory buffer, and is responsible for deleting it.  
Example:  
unsigned char \*data = byteData.removeData();  
... do something with the data....  
delete \[\] data;

### CkByteData.replaceChar

`Chilkat 11.7.0 · cpp · CkByteData · replaceChar · method`

```cpp
void replaceChar(unsigned char existingByteValue, unsigned char replacementByteValue);
```

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

### CkByteData.saveFile

`Chilkat 11.7.0 · cpp · CkByteData · saveFile · method`

```cpp
bool saveFile(const char *path);
```

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

### CkByteData.saveFileW

`Chilkat 11.7.0 · cpp · CkByteData · saveFileW · method`

```cpp
bool saveFileW(const wchar_t *path);
```

To be documented soon...

### CkByteData.shorten

`Chilkat 11.7.0 · cpp · CkByteData · shorten · method`

```cpp
void shorten(unsigned long numBytes);
```

Discards N bytes from the end of the data.

### CkByteData.to_ws

`Chilkat 11.7.0 · cpp · CkByteData · to_ws · method`

```cpp
const wchar_t *to_ws(const char *charset);
```

To be documented soon...

### CkByteData.unpad

`Chilkat 11.7.0 · cpp · CkByteData · unpad · method`

```cpp
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/cpp/classes/CkString.md)

