> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://v3.viem.sh/api/mcp` to find what you need.

# Bytes.fromString

:::info
`Bytes.fromString` is a re-export of ox's [`Bytes.fromString`](https://oxlib.sh/api/Bytes/fromString). Refer to the ox documentation for the full reference.
:::

Encodes a string into [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes).

## Imports

```ts
import { Bytes } from 'viem/utils'
```

## Examples

```ts twoslash
// @noErrors
import { Bytes } from 'viem/utils'

const data = Bytes.fromString('Hello world!')
// @log: Uint8Array([72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33])
```

```ts twoslash
// @noErrors
import { Bytes } from 'viem/utils'

const data = Bytes.fromString('Hello world!', { size: 32 })
// @log: Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
```

## Definition

```ts
function fromString(
  value: string,
  options?: fromString.Options,
): Bytes
```

## Parameters

### value

* **Type:** `string`

String to encode.

### options

* **Type:** `fromString.Options`
* **Optional**

Encoding options.

#### options.size

* **Type:** `number`
* **Optional**

Size of the output bytes.

## Return Type

Encoded [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes).

`Bytes`
