Reading ascii chars

Could somebody please explain to me why reading this:
let my_str = "\0\0\0~";
with:
let mut buf = [0; 4];

socket.read(&mut buf).await
Correctly fills that buf with [0, 0, 0, 126]
but reading this:
"\0\0\0ª"

fills this buf with [0, 0, 0, 195] instead of [0, 0, 0, 166]

I think that this has something to do with extended ascii table, but if so, how to prepare that my_str var so it correctly reads on the socket side?

There are no ASCII characters above 127 (0x7F). There is a trillion legacy eight-bit extensions to ASCII, each having their own set of characters in the upper 128..=255. None of them should be used today except when interfacing with legacy systems. If you do, you have to ensure somehow that both ends use the same encoding, either by convention or by protocol. Rust has no specific support for these encodings.

1 Like

Hi and thank you for your answer.
Is there a way to actually write it as a u8 literal?
Something like "\u0166MyString"

fn main() {
    let s = b"a\xa6b";
    println!("{s:?}");
}

prints:

[97, 166, 98]
3 Likes

Hi and thank you very much for your help. Would you believe it that literally before I read your answer I finally got the same idea. It just shows that it is helpful to ask.
Once again, thank you.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.