Convert a Char to hex

Ok so Im building a emulator in roblox pretty simple one however I need help I need to convert a char to hex(I need this because Im going to need to convert each char that the cpu is going to read to be converted into a hex value that can be read and used just like a number in lua.) I dont know where to start on this and need help. I hope what I’m giving isn’t to vague

Also I hate to be rude but I just want help on this function not about the emulator thank you so much :slight_smile:

Heres a graph of what I hope for this to look like

1 Like

Do you mean you want to turn A0 (base 16) into 160 (base 10) or do you mean you want the ascii / utf8 value of a character?

Sorry I had to edit this but i wanted it to convert it like how a hex editor does it. For example
image

The website wasnt that good of an example and I think this servers as a better example again sorry this is so vague

Usually these convert to ascii or utf-8. You want string.char()

Can you show me an example of string.char or how that can convert something to hex

Edit: that converts from hex to char I need it the other way around

You cant necessarily do the other way around since not all possible values are printable, but for the ones that are I think its string.byte().

1 Like
--!strict

local function toHex(input: string): string
	local hexReturn: string = ""
	for char: string in string.gmatch(input, ".") do
		hexReturn ..= string.format("%x", utf8.codepoint(char)) .. " "
	end
	return hexReturn
end

Example implementation:

print(toHex("Hello, world!"))

Output:

48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21
2 Likes

Ill try this out thank you so much