Chr/ ORD

var txt="a";
var ord=txt.charCodeAt(0); /* 0. Position -> 65 */
var chr=String.fromCharCode(65); /* A */
print(chr);

This is how you convert a character to its ASCII value and back. But I can’t seem to find a way how to do it Unity. Is this possible in Unity at all?

you need to use the .NET Convert class:

so using your example this works:

import System;

var ord = Convert.ToInt32("A"[0]);
print (ord); //65 lowercase a gives 97
var chr = Convert.ToChar(ord);
print (chr); //A

Yes you need to feed ToInt32 an array element.

Cheers.

Thanks! .NET is such a total maze of commands and classes :slight_smile: The ToInt32 works, but the other one results in: “InvalidCastException: This conversion is not supported”.

EDIT: Solved. Also regular ints need to be converted to an Int32. Probably due the fact the conversion supports Unicode characters.

static function numberToAlphabet(dNumber) {
	dNumber+=65;
	var c=Convert.ToInt32(dNumber);
	var chr = Convert.ToChar(c);  
	return(chr);
	}