{"route":"/en-US-v0.14.1/reference/foundations/bytes/","title":"Bytes","description":"Documentation for the Bytes type.","part":null,"outline":[{"id":"summary","name":"Summary","children":[]},{"id":"constructor","name":"Constructor","children":[{"id":"constructor-value","name":"value","children":[]}]},{"id":"definitions","name":"Definitions","children":[{"id":"definitions-len","name":"Length","children":[]},{"id":"definitions-at","name":"At","children":[{"id":"definitions-at-index","name":"index","children":[]},{"id":"definitions-at-default","name":"default","children":[]}]},{"id":"definitions-slice","name":"Slice","children":[{"id":"definitions-slice-start","name":"start","children":[]},{"id":"definitions-slice-end","name":"end","children":[]},{"id":"definitions-slice-count","name":"count","children":[]}]}]}],"body":{"kind":"type","content":{"name":"bytes","title":"Bytes","keywords":[],"oneliner":"A sequence of bytes.","details":"<p>A sequence of bytes.</p>\n<p>This is conceptually similar to an array of <a href=\"/en-US-v0.14.1/reference/foundations/int/\">integers</a> between <code><span class=\"typ-num\">0</span></code>\nand <code><span class=\"typ-num\">255</span></code>, but represented much more efficiently. You can iterate over it\nusing a <a href=\"/en-US-v0.14.1/reference/scripting/#loops\">for loop</a>.</p>\n<p>You can convert</p>\n<ul>\n<li>a <a href=\"/en-US-v0.14.1/reference/foundations/str/\">string</a> or an <a href=\"/en-US-v0.14.1/reference/foundations/array/\" title=\"array\">array</a> of integers to bytes with the <a href=\"/en-US-v0.14.1/reference/foundations/bytes/\" title=\"`bytes`\"><code>bytes</code></a>\nconstructor</li>\n<li>bytes to a string with the <a href=\"/en-US-v0.14.1/reference/foundations/str/\" title=\"`str`\"><code>str</code></a> constructor, with UTF-8 encoding</li>\n<li>bytes to an array of integers with the <a href=\"/en-US-v0.14.1/reference/foundations/array/\" title=\"`array`\"><code>array</code></a> constructor</li>\n</ul>\n<p>When <a href=\"/en-US-v0.14.1/reference/data-loading/read/\">reading</a> data from a file, you can decide whether to load it\nas a string or as raw bytes.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-func\">#</span><span class=\"typ-func\">bytes</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">123</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">160</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">22</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">0</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">bytes</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;Hello 😃&quot;</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> data <span class=\"typ-op\">=</span> <span class=\"typ-func\">read</span><span class=\"typ-punct\">(</span>\n  <span class=\"typ-str\">&quot;rhino.png&quot;</span><span class=\"typ-punct\">,</span>\n  encoding<span class=\"typ-punct\">:</span> <span class=\"typ-key\">none</span><span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span>\n\n<span class=\"typ-comment\">// Magic bytes.</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">array</span><span class=\"typ-punct\">(</span>data<span class=\"typ-punct\">.</span><span class=\"typ-func\">slice</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">0</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">4</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">str</span><span class=\"typ-punct\">(</span>data<span class=\"typ-punct\">.</span><span class=\"typ-func\">slice</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">1</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">4</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/b09b581605724240d910b1078dee72c3.png\" alt=\"Preview\"></div></div>","constructor":{"path":[],"name":"bytes","title":"Construct","keywords":[],"oneliner":"Converts a value to bytes.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Converts a value to bytes.</p>\n<ul>\n<li>Strings are encoded in UTF-8.</li>\n<li>Arrays of integers between <code><span class=\"typ-num\">0</span></code> and <code><span class=\"typ-num\">255</span></code> are converted directly. The\ndedicated byte representation is much more efficient than the array\nrepresentation and thus typically used for large byte buffers (e.g. image\ndata).</li>\n</ul>"},{"kind":"example","content":{"body":"<div class=\"previewed-code\"><pre><code><span class=\"typ-func\">#</span><span class=\"typ-func\">bytes</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;Hello 😃&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">bytes</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">123</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">160</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">22</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">0</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/3e57d56a31a67c32cc63aa7c5f84b707.png\" alt=\"Preview\"></div></div>","title":null}}],"self":false,"params":[{"name":"value","details":[{"kind":"html","content":"<p>The value that should be converted to bytes.</p>"}],"types":["str","bytes","array"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":["bytes"],"scope":[]},"scope":[{"path":["bytes"],"name":"len","title":"Length","keywords":[],"oneliner":"The length in bytes.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>The length in bytes.</p>"}],"self":true,"params":[],"returns":["int"],"scope":[]},{"path":["bytes"],"name":"at","title":"At","keywords":[],"oneliner":"Returns the byte at the specified index.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Returns the byte at the specified index. Returns the default value if\nthe index is out of bounds or fails with an error if no default value\nwas specified.</p>"}],"self":true,"params":[{"name":"index","details":[{"kind":"html","content":"<p>The index at which to retrieve the byte.</p>"}],"types":["int"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"default","details":[{"kind":"html","content":"<p>A default value to return if the index is out of bounds.</p>"}],"types":["any"],"strings":[],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false}],"returns":["any"],"scope":[]},{"path":["bytes"],"name":"slice","title":"Slice","keywords":[],"oneliner":"Extracts a subslice of the bytes.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Extracts a subslice of the bytes. Fails with an error if the start or\nend index is out of bounds.</p>"}],"self":true,"params":[{"name":"start","details":[{"kind":"html","content":"<p>The start index (inclusive).</p>"}],"types":["int"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"end","details":[{"kind":"html","content":"<p>The end index (exclusive). If omitted, the whole slice until the end\nis extracted.</p>"}],"types":["none","int"],"strings":[],"default":"<code><span class=\"typ-key\">none</span></code>","positional":true,"named":false,"required":false,"variadic":false,"settable":false},{"name":"count","details":[{"kind":"html","content":"<p>The number of items to extract. This is equivalent to passing\n<code>start + count</code> as the <code>end</code> position. Mutually exclusive with\n<code>end</code>.</p>"}],"types":["int"],"strings":[],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false}],"returns":["bytes"],"scope":[]}]}}}