You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a CID v1, JS users can convert back to v0 using the `toV0()` method provided by the [`multiformats`](https://www.npmjs.com/package/multiformats) library:
A CID can be represented as both text and as a stream of bytes. The latter may be a better choice when speed and storage efficiency are considerations.
94
+
:::callout
95
+
**See CID conversion in action**
96
+
See the [interactive code sandbox](#codesandbox-converting-between-cid-versions-and-encodings) for an example JS application that converts between CID versions and ecnodings.
97
+
:::
82
98
83
-
To convert a CIDv1 from text to binary form, simply read the first character
84
-
and then decode the remainder using the encoding specified in the [multibase table](https://github.com/multiformats/multibase#multibase-table).
99
+
### Converting between CID base encodings
85
100
86
-
JS users can leverage the [`cids`](https://www.npmjs.com/package/cids) library to get a binary version as `Uint8Array`:
101
+
A CID can be encoded using any of the encodings specified in the [multibase table](https://github.com/multiformats/multibase#multibase-table). The use of different encodings can impact speed and storage efficiency.
87
102
103
+
To convert a CIDv1 `cidV1` from one encoding to another, use the `toString()` method. By default, `toString()` will return the `base32` string representation of the CID, but you can use other string representations:
::: warning Be mindful about parsing CIDs correctly. Avoid shortcuts.
96
-
97
-
Unless you are the one who imported the data to IPFS, the length of a CID is not deterministic and depends on the length of the multihash inside of it.
109
+
The following example returns the base256 emoji encoding of the CID:
See the [interactive code sandbox](#codesandbox-converting-between-cid-versions-and-encodings) for an example JS application that converts between CID versions and ecnodings.
109
123
:::
110
124
111
125
112
126
### CID to hex
113
127
114
128
Sometimes, a [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) representation of raw bytes is preferred for debug purposes.
115
-
To get the hex for raw `.bytes` of an entire CID, one can use built-in support for `base16` encoding and skip the `f` (multibase prefix):
To get the hex for raw `.bytes` of a CIDv1 `cidV1`, use `base16` encoding:
120
130
121
-
>cid.toString('base16').substring(1)
122
-
'01701220c3c4733ec8affd06cf9e9ff50ffc6bcd2ec85a6170004bb709669c31de94391a'// "cid as hex"
131
+
```js
132
+
constcidV1StringBase256=cidV1.toString(base16);
123
133
```
124
134
125
-
To convert back to a CIDv1, prepend the hex value with `f` ([multibase prefix](https://github.com/multiformats/multibase#multibase-table) for lowercase base16).
126
-
Use it as-is (it is a [valid CID](https://ipfs.io/ipfs/f01701220c3c4733ec8affd06cf9e9ff50ffc6bcd2ec85a6170004bb709669c31de94391a)), or convert to a different multibase by passing it as an argument to `toString`:
See the [interactive code sandbox](#codesandbox-converting-between-cid-versions-and-encodings) for an example JS application that converts between CID versions and ecnodings.
138
+
:::
132
139
133
140
::: tip
134
141
[Subdomain gateways](../how-to/address-ipfs-on-web.md#subdomain-gateway) convert paths with custom bases like base16 to base32 or base36, in an effort to fit a CID in a DNS label:
@@ -138,6 +145,17 @@ Use it as-is (it is a [valid CID](https://ipfs.io/ipfs/f01701220c3c4733ec8affd06
138
145
:::
139
146
140
147
148
+
### CodeSandbox: Converting between CID versions and encodings
149
+
150
+
For a hand-on, interactive application that converts between CID versions and encodings, use the CodeSandbox below.
0 commit comments