I also just realized that in my particular case (having access to the string that contains the character c at position i), I can use codeunit(str, i+j) with j between 0 and ncodeunits(c)-1.
@JeffreySarnoff thanks, very readable (though it will take 2 more methods (Val{3} and Val{4}) to cover all UTF-8 characters).
@pixel27 this gives each character in a string, but I need each byte in a single character (UTF-8 characters take between 1 and 4 bytes).
Maybe it would make sense to add a method codeunit(c::Char, i::Integer) in Base (and leave it optional to implement codeunit(::AbstractChar, ::Integer) for other character types)…
Since Base already implements codeunits for strings, I think it would make sense to add for Char as well. Would you mind opening an issue or even a PR with your solution?
(Also note that you probably meant to overload Base.eltype(::Type{<:CodeUnitIterator}) = UInt8 instead of Base.eltype(::Type{CodeUnitIterator}) = UInt8, which would fix collect accidentally returning an array with element type Any here.)