# Class Bytes

- [TopLevel.Object](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md)
  - [dw.util.Bytes](/docs/commerce/b2c-commerce/references/b2c-script-api/dw.util.Bytes.md)

A simple immutable class representing an array of bytes, used for working
with binary data in a scripting context.

It acts as a view to [ArrayBuffer](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.ArrayBuffer.md). The buffer can be accessed through [asUint8Array()](/docs/commerce/b2c-commerce/references/b2c-script-api/dw.util.Bytes.md#asuint8array).

**Limitation:**
The size of the resulting byte representation is limited by the quota *api.jsArrayBufferSize* that is defining the max size for a [ArrayBuffer](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.ArrayBuffer.md).

## Constant Summary

| Constant                                                                                                                | Description                                                           |
| ----------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- |
| ~~[MAX_BYTES](#max_bytes): [Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md) = 10240~~ | The maximum number of bytes that a Bytes object can represent == 10KB |

## Property Summary

| Property                                                                                                            | Description                                             |
| ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------- |
| [length](#length): [Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md) `(read-only)` | Returns the number of bytes represented by this object. |

## Constructor Summary

| Constructor                                                                                                                                                                                          | Description                                                                                                                                   |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| [Bytes](#bytesobject)([Object](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md))                                                                                            | Construct a Bytes object from the given [ArrayBuffer](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.ArrayBuffer.md) or view. |
| [Bytes](#bytesstring)([String](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.String.md))                                                                                            | Construct a Bytes object from the given string using the default  encoding.                                                                   |
| [Bytes](#bytesstring-string)([String](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.String.md), [String](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.String.md)) | Construct a Bytes object from the given string using the given encoding.                                                                      |

## Method Summary

| Method                                                                                                                                                                                                                                                                         | Description                                                                                                                                                                                                                                |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [asUint8Array](/docs/commerce/b2c-commerce/references/b2c-script-api/dw.util.Bytes.md#asuint8array)()                                                                                                                                                                          | Returns a [Uint8Array](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Uint8Array.md) based on the [ArrayBuffer](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.ArrayBuffer.md) used for this Bytes object. |
| [byteAt](/docs/commerce/b2c-commerce/references/b2c-script-api/dw.util.Bytes.md#byteatnumber)([Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md))                                                                                              | Returns the value of the byte at position index as an integer.                                                                                                                                                                             |
| [bytesAt](/docs/commerce/b2c-commerce/references/b2c-script-api/dw.util.Bytes.md#bytesatnumber-number)([Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md), [Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md)) | Return a new Bytes object containing the subsequence of this object's bytes specified by the index and length  parameters.                                                                                                                 |
| [getLength](/docs/commerce/b2c-commerce/references/b2c-script-api/dw.util.Bytes.md#getlength)()                                                                                                                                                                                | Returns the number of bytes represented by this object.                                                                                                                                                                                    |
| [intAt](/docs/commerce/b2c-commerce/references/b2c-script-api/dw.util.Bytes.md#intatnumber)([Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md))                                                                                                | Absolute get method for reading a signed integer value (32 bit) in  network byte order(= big endian).                                                                                                                                      |
| [reverse](/docs/commerce/b2c-commerce/references/b2c-script-api/dw.util.Bytes.md#reverse)()                                                                                                                                                                                    | Return a new Bytes object which has the same bytes as this one in reverse  order.                                                                                                                                                          |
| [shortAt](/docs/commerce/b2c-commerce/references/b2c-script-api/dw.util.Bytes.md#shortatnumber)([Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md))                                                                                            | Absolute get method for reading a signed short value (16 bit) in network  byte order(= big endian).                                                                                                                                        |
| [toString](/docs/commerce/b2c-commerce/references/b2c-script-api/dw.util.Bytes.md#tostring)()                                                                                                                                                                                  | Constructs a new String by decoding this array of bytes using the  default encoding.                                                                                                                                                       |
| [toString](/docs/commerce/b2c-commerce/references/b2c-script-api/dw.util.Bytes.md#tostringstring)([String](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.String.md))                                                                                          | Constructs a new String by decoding this array of bytes using the  specified encoding.                                                                                                                                                     |

### Methods inherited from class Object

[assign](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#assignobject-object), [create](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#createobject), [create](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#createobject-object), [defineProperties](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#definepropertiesobject-object), [defineProperty](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#definepropertyobject-object-object), [entries](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#entriesobject), [freeze](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#freezeobject), [fromEntries](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#fromentriesiterable), [getOwnPropertyDescriptor](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#getownpropertydescriptorobject-object), [getOwnPropertyNames](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#getownpropertynamesobject), [getOwnPropertySymbols](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#getownpropertysymbolsobject), [getPrototypeOf](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#getprototypeofobject), [hasOwnProperty](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#hasownpropertystring), [is](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#isobject-object), [isExtensible](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#isextensibleobject), [isFrozen](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#isfrozenobject), [isPrototypeOf](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#isprototypeofobject), [isSealed](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#issealedobject), [keys](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#keysobject), [preventExtensions](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#preventextensionsobject), [propertyIsEnumerable](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#propertyisenumerablestring), [seal](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#sealobject), [setPrototypeOf](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#setprototypeofobject-object), [toLocaleString](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#tolocalestring), [toString](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#tostring), [valueOf](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#valueof), [values](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md#valuesobject)

## Constant Details

### MAX_BYTES

- ~~MAX_BYTES: [Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md) = 10240~~
  - : The maximum number of bytes that a Bytes object can represent == 10KB

    **Deprecated:**

:::warning
No longer used by the Bytes class.
:::

***

## Property Details

### length

- length: [Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md) `(read-only)`
  - : Returns the number of bytes represented by this object.

***

## Constructor Details

### Bytes(Object)

- Bytes(arrayBufferOrView: [Object](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md))
  - : Construct a Bytes object from the given [ArrayBuffer](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.ArrayBuffer.md) or view. The bytes object also acts as a
    view on the underlying [ArrayBuffer](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.ArrayBuffer.md). If a view is given that makes only a part of the storage
    array visible then this Bytes object will also make only the same part visible. The storage data is not copied.

    **Parameters:**

    - arrayBufferOrView - An [ArrayBuffer](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.ArrayBuffer.md) or view to a buffer that is the storage.

    **API Version:**

:::note
Available from version 21.2.
:::

***

### Bytes(String)

- Bytes(string: [String](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.String.md))
  - : Construct a Bytes object from the given string using the default
    encoding. Convenience for Bytes( string, "UTF-8" ).

    **Parameters:**

    - string - The string to encode into a Bytes object, must not be null.

    **Throws:**

    - IllegalArgumentException - If the encoded byte sequence exceeds the              maximum number of bytes.

***

### Bytes(String, String)

- Bytes(string: [String](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.String.md), encoding: [String](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.String.md))
  - : Construct a Bytes object from the given string using the given encoding.
    This method always replaces malformed input and unmappable character
    sequences with encoding defaults.

    **Parameters:**

    - string - The string to encode into a Bytes object, must not be null.
    - encoding - The name of a supported encoding, or null in which case             the default encoding (UTF-8) is used.

    **Throws:**

    - IllegalArgumentException - If the named encoding is not supported              or if the encoded byte sequence exceeds the maximum number of              bytes.

***

## Method Details

### asUint8Array()

- asUint8Array(): [Object](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Object.md)
  - : Returns a [Uint8Array](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Uint8Array.md) based on the [ArrayBuffer](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.ArrayBuffer.md) used for this Bytes object.
    Changes to the returned [ArrayBuffer](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.ArrayBuffer.md) will be visible in the Bytes object.

    **Returns:**

    - A newly created [Uint8Array](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Uint8Array.md) based on the existing [ArrayBuffer](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.ArrayBuffer.md).

    **API Version:**

:::note
Available from version 21.2.
:::

***

### byteAt(Number)

- byteAt(index: [Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md)): [Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md)
  - : Returns the value of the byte at position index as an integer. If index
    is out of range an exception is thrown. The byte is interpreted as signed
    and so the value returned will always be between -128 and +127.

    **Parameters:**

    - index - The index of the byte.

    **Returns:**

    - The byte value at the specified index.

    **Throws:**

    - IndexOutOfBoundsException - If the index argument is negative or              not less than the length of this byte array.

***

### bytesAt(Number, Number)

- bytesAt(index: [Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md), length: [Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md)): [Bytes](/docs/commerce/b2c-commerce/references/b2c-script-api/dw.util.Bytes.md)
  - : Return a new Bytes object containing the subsequence of this object's bytes specified by the index and length
    parameters. The returned object is a new view onto the same data, no data is copied.

    **Parameters:**

    - index - The initial index for the new view, inclusive.
    - length - The number of bytes visible in the new view.

    **Returns:**

    - a new Bytes object representing a subsequence of this Bytes object.

    **Throws:**

    - ArrayIndexOutOfBoundsException - If index < 0 or index > getLength() or index + length >              getLength()
    - IllegalArgumentException - If length < 0

***

### getLength()

- getLength(): [Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md)
  - : Returns the number of bytes represented by this object.

    **Returns:**

    - The number of bytes.

***

### intAt(Number)

- intAt(index: [Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md)): [Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md)
  - : Absolute get method for reading a signed integer value (32 bit) in
    network byte order(= big endian).

    **Parameters:**

    - index - The byte index at which to read the number.

    **Returns:**

    - The read number.

    **Throws:**

    - IndexOutOfBoundsException - If index is negative or not smaller              than the number of bytes minus three.

***

### reverse()

- reverse(): [Bytes](/docs/commerce/b2c-commerce/references/b2c-script-api/dw.util.Bytes.md)
  - : Return a new Bytes object which has the same bytes as this one in reverse
    order.

    **Returns:**

    - a new Bytes object representing the reverse of this Bytes object.

***

### shortAt(Number)

- shortAt(index: [Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md)): [Number](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.Number.md)
  - : Absolute get method for reading a signed short value (16 bit) in network
    byte order(= big endian).

    **Parameters:**

    - index - The byte index at which to read the number.

    **Returns:**

    - The read number.

    **Throws:**

    - IndexOutOfBoundsException - If index is negative or not smaller              than the number of bytes minus one.

***

### toString()

- toString(): [String](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.String.md)
  - : Constructs a new String by decoding this array of bytes using the
    default encoding. Convenience for toString( "UTF-8" ).
    **Limitation:**
    The method is protected by the quota *api.jsStringLength* that prevents creation of too long strings.

    **Returns:**

    - A String representing the decoded array of bytes.

***

### toString(String)

- toString(encoding: [String](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.String.md)): [String](/docs/commerce/b2c-commerce/references/b2c-script-api/TopLevel.String.md)
  - : Constructs a new String by decoding this array of bytes using the
    specified encoding.
    **Limitation:**
    The method is protected by the quota *api.jsStringLength* that prevents creation of too long strings.

    **Parameters:**

    - encoding - The name of a supported encoding.

    **Returns:**

    - A String representing the decoded array of bytes.

    **Throws:**

    - IllegalArgumentException - If the named encoding is not supported.

***
