let write_to_native_string c ~buf ~index =
let sz = size c in
try
let first_byte =
match sz with
| 1 -> ((c lsr 0) land 0b0111_1111) lor 0b0000_0000
| 2 -> ((c lsr 6) land 0b0001_1111) lor 0b1100_0000
| 3 -> ((c lsr 12) land 0b0000_1111) lor 0b1110_0000
| 4 -> ((c lsr 18) land 0b0000_0111) lor 0b1111_0000
| 5 -> ((c lsr 24) land 0b0000_0011) lor 0b1111_1000
| 6 -> ((c lsr 30) land 0b0000_0001) lor 0b1111_1100
| _ -> assert false in
buf.[index] <- char_of_int first_byte;
for i = 2 to sz do
let ith_byte =
((c lsr (6 * (i - 2))) land 0b0011_1111) lor 0b1000_0000 in
buf.[index + sz - i + 1] <- char_of_int ith_byte;
done;
return sz
with _ -> fail `out_of_bounds