Completely stumped ASCII Value to String

Hi All,

Ok I’m completely stumped. Question how do you convert an ASCII value to a string, say 65 for A to the String “A”. For use in the program not printing out.

I just can’t figure this one out. Probably the most boneheaded question.

Kindly

Run

So it depends on what the lifetime of the string needs to be

package main

import "core:fmt"
import "core:unicode/utf8"

main :: proc() {
	ascii: u8 = 65

	// no allocation
	{
		buf, n := utf8.encode_rune(rune(ascii))
		str := string(buf[:n])
	}
	// no allocation
	{
		str := string([]u8{ascii})
	}
	// allocation with temp allocator
	{
		str := fmt.tprint(rune(ascii))
	}
	// allocation with context.allocator or a custom allocator
	{
		str := fmt.aprint(rune(ascii))
	}
}

in the first two examples str is a pointer to a stack variable so you need to be careful with lifetimes there

2 Likes

Thanks so much I have been banging my head all day with this one.

Best

Ryn