Convert escape sequence to its corresponding UNICODE code from a string

Hello, my problem is this:

From a lexer I receive a string containing the value of a character literal, for example:

'A'
'B'
'+'
'\n'

From these strings I want to obtain its corresponding UNICODE code, since it is a string, the first thing I do is trim the string with the character (single quote) "'", so I get the following strings

A
B
+
\n

From the above strings I would like to get the UNICODE code value and store it, what I have tried is the following:

trimmedValue := strings.Trim(charLiteral, "'")
chars := []rune(trimmedValue)

From here I can get the value for the first three cases, 65, 66, 43 but how to convert the string \n to its unicode value, since in this way I get a slice of two different values, that is, how to make \n be interpreted as a only value/character? Is there a direct way to do it?

Thanks

FWIW: That gives you the ASCII value AFAIK.

You may want to take a look at: string - How can I get the Unicode value of a character in go? - Stack Overflow

1 Like

If you have the character literals, then you already have the unicode values.

From

a character constant is called a rune constant in Go

and a rune is a unicode code point, and a rune is uint32.

If instead you have strings, those are utf8 encoded and you get a rune (the unicode code point) from a string as described in the link referred to by @freeformz (tldr utf8 package - unicode/utf8 - Go Packages)

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.