Home Explore Blog CI



nushell

commands/docs/encode.md
97d94d6c422a4fdd999ee2cd30a7c275a64f7333d5781ed90000000300000ad6
---
title: encode
categories: |
  strings
version: 0.104.0
strings: |
  Encode a string into bytes.
usage: |
  Encode a string into bytes.
editLink: false
contributors: false
---
<!-- This file is automatically generated. Please edit the command in https://github.com/nushell/nushell instead. -->

# `encode` for [strings](/commands/categories/strings.md)

<div class='command-title'>Encode a string into bytes.</div>

## Signature

```> encode {flags} (encoding)```

## Flags

 -  `--ignore-errors, -i`: when a character isn't in the given encoding, replace with a HTML entity (like `&amp;#127880;`)

## Parameters

 -  `encoding`: The text encoding to use.


## Input/output types:

| input  | output |
| ------ | ------ |
| string | binary |
## Examples

Encode an UTF-8 string into Shift-JIS
```nu
> "負けると知って戦うのが、遥かに美しいのだ" | encode shift-jis
Length: 40 (0x28) bytes | printable whitespace ascii_other non_ascii
00000000:   95 89 82 af  82 e9 82 c6  92 6d 82 c1  82 c4 90 ed   ×××××××××m××××××
00000010:   82 a4 82 cc  82 aa 81 41  97 79 82 a9  82 c9 94 fc   ×××××××A×y××××××
00000020:   82 b5 82 a2  82 cc 82 be                             ××××××××

```

Replace characters with HTML entities if they can't be encoded
```nu
> "🎈" | encode --ignore-errors shift-jis
Length: 9 (0x9) bytes | printable whitespace ascii_other non_ascii
00000000:   26 23 31 32  37 38 38 30  3b                         &amp;#127880;

```

## Notes
Multiple encodings are supported; here are a few:
big5, euc-jp, euc-kr, gbk, iso-8859-1, cp1252, latin5

Note that since the Encoding Standard doesn't specify encoders for utf-16le and utf-16be, these are not yet supported.
More information can be found here: https://docs.rs/encoding_rs/latest/encoding_rs/#utf-16le-utf-16be-and-unicode-encoding-schemes

For a more complete list of encodings, please refer to the encoding_rs
documentation link at https://docs.rs/encoding_rs/latest/encoding_rs/#statics

## Subcommands:

| name                                                     | description                                        | type     |
| -------------------------------------------------------- | -------------------------------------------------- | -------- |
| [`encode base32`](/commands/docs/encode_base32.md)       | Encode a string or binary value using Base32.      | built-in |
| [`encode base32hex`](/commands/docs/encode_base32hex.md) | Encode a binary value or a string using base32hex. | built-in |
| [`encode base64`](/commands/docs/encode_base64.md)       | Encode a string or binary value using Base64.      | built-in |
| [`encode hex`](/commands/docs/encode_hex.md)             | Hex encode a binary value or a string.             | built-in |

Chunks
37185bf3 (1st chunk of `commands/docs/encode.md`)
Title: encode
Summary
The `encode` command in Nushell is used to encode a string into bytes using a specified text encoding. It supports various encodings like Shift-JIS, UTF-8, and others. The command can also handle encoding errors by replacing characters that can't be encoded with HTML entities. It includes subcommands for Base32, Base32hex, Base64, and hexadecimal encoding.