# byte[]

> Raw bytes — a file's contents, a digest, a key. Write one as C# does, `new byte[] { 0x89, 0x50 }`, or as a collection expression `[0x89, 0x50]` wherever a `byte[]` is expected. Each element is an integer constant from 0 to 255, converted exactly as C# converts it.

<!-- id: types-byte-array · area: types · stability: stable · html: https://osysharp.com/reference/types/byte-array/ -->

## Summary        {#summary}

A `byte[]` is raw bytes: what `File.ReadAllBytes` hands back, what `Crypto.Sha256` answers, what an entity's `byte[]`
member stores. When you know the bytes up front — a file signature, a fixed prefix, a test fixture — write them down
the way you would in C#.

## Signature      {#signature}

```osy syntax
byte[] a = new byte[] { 0x89, 0x50, 0x4E, 0x47 };   // the explicit array creation
byte[] b = [0x89, 0x50, 0x4E, 0x47];                // a collection expression, target-typed to byte[]
byte[] zeros = new byte[16];                        // sixteen zero bytes — the length must be a constant
File.WriteAllBytes("sig.bin", [1, 2, 3]);           // an argument is a target too
```

## Description    {#description}

### It is one value, not a list      {#one-value}
In Osy# a `byte[]` is a single value — the same one `Convert.FromBase64String` or `Text.ToBytes` produces — and not a
collection of numbers. So everything that takes bytes takes a literal: the file store, `Convert.ToBase64String`,
the `Crypto` family, `Http.Put`, and an entity's `byte[]` member. There is no index write; bytes you compute come from
the functions that produce them rather than from filling in a buffer.

### Where `[ … ]` becomes bytes      {#target-typed}
`new byte[] { … }` is a `byte[]` anywhere, because you named the element type. A bare `[1, 2, 3]` is a `byte[]` only
where one is **expected** — a `byte[]` local, return, parameter or member, or an argument whose parameter takes bytes.
Anywhere else it is an `int[]`, as it would be in C#:

```osy title="the same brackets, two meanings" syntax
byte[] b = [1, 2];     // bytes: the declaration asks for them
var n = [1, 2];        // an int[]: nothing asked
```

### What each element may be      {#elements}
Each element is converted the way C# converts a constant `int` to a `byte`, and refused where C# refuses it:

| element | answer |
|---|---|
| a constant from 0 to 255 — `7`, `0xFF`, `255` | that byte |
| a constant outside that range — `256`, `-1` | an error: *constant value '256' cannot be converted to a 'byte'* |
| a computed `int` — a variable, a parameter | an error: the elements must be constants |
| anything that is not an integer | an error naming its type |

Bytes built from values you have at run time come from `Text.ToBytes(s)`, `Encoding.UTF8.GetBytes(s)` (or any other
encoding, code pages included — see [Encoding — Base64, hex, text encodings, URL, HTML](https://osysharp.com/reference/stdlib/encoding/)), `Convert.FromBase64String(s)`, `Convert.FromHexString(hex)`
or `Crypto.FromHex(hex)` — see [Signing with raw bytes — Crypto.HmacSha256, Sha256, ToHex, and Text.ToBytes](https://osysharp.com/reference/function/crypto-bytes/).

### Reading part of one      {#slicing}
A `byte[]` is sliced the way C# slices one — `file.Take(8)` (the first eight bytes), `file.Skip(8)` (all but the
first eight), `file.Slice(4, 2)` (two bytes from index 4, `Span`'s spelling) — and `file.Length` is its size. Each
answers a new `byte[]`; the original is never changed.

## Examples       {#examples}

```osy title="a file signature, written down as bytes" test app=byte-array
bool LooksLikePng(byte[] file) {
  byte[] png = new byte[] { 0x89, 0x50, 0x4E, 0x47 };
  return Crypto.ToHex(file).StartsWith(Crypto.ToHex(png));
}
```

```osy title="a collection expression passed where bytes are expected" test app=byte-array
string Encoded() => Convert.ToBase64String([0x01, 0x02, 0xFF]);
```

## See also       {#see-also}
- [Signing with raw bytes — Crypto.HmacSha256, Sha256, ToHex, and Text.ToBytes](https://osysharp.com/reference/function/crypto-bytes/) — bytes from text and hex, and the hashes that take them
- [Constant expressions](https://osysharp.com/reference/types/constant-expressions/) — what counts as a constant
