printinguser-inputodin

I can't print user input as ascii characters in Odin


I just want to take user input and then print it back but it will just print numbers. I don't know how to convert a byte array to a string or an ascii character.

I just started trying Odin. There is a lot of information on the main site, variables, loops, strings etc. However I can't find anything about how to get user input and then print it. My attempt:

import "core:fmt"
import "core:os"
import "core:strings"

main :: proc() 
{
    
    fmt.print("Enter something: ");
    buf: [256]byte; 
    num_bytes, err := os.read(os.stdin, buf[:]);

    fmt.println("You entered:");
    fmt.print(buf);

}

This will just print all 256 numbers. I'm on Windows so I looked at the read function in the 'os' module:

read :: proc(fd: Handle, data: []byte) -> (total_read: int, err: Errno) {
...

So it takes in an array of bytes. All I need to do is print the byte array in char format. The output when I type "hello":

[104, 101, 108, 108, 111, 13, 10, 0, ...

I don't want to see numbers, I want to see text.

How do I print a byte array as ascii characters?

Or how do I convert the byte array into a string and print that?


Solution

  • I want to see text.

    How do I print a byte array as ascii characters?

        fmt.printf("%s", buf);