Java中的Unicode到字符串转换 [英] Unicode to string conversion in Java

查看:155
本文介绍了Java中的Unicode到字符串转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一种语言,一种玩具语言。语法 \#0061 应该将给定的Unicode转换为字符:

I am building a language, a toy language. The syntax \#0061 is supposed to convert the given Unicode to an character:

String temp = yytext().subtring(2);

然后尝试附加'\ u'到字符串,我注意到生成了一个错误。

Then after that try to append '\u' to the string, I noticed that generated an error.

我还试过\\+u+ temp; 这种方式不做任何转换。

I also tried to "\\" + "u" + temp; this way does not do any conversion.

我基本上只是通过向方法提供'0061'来将Unicode转换为字符,帮助。

I am basically trying to convert Unicode to a character by supplying only '0061' to a method, help.

推荐答案

剥去'#'并使用 Integer.parseInt(0061,16)将十六进制数字转换为 int 。然后转换为 char

Strip the '#' and use Integer.parseInt("0061", 16) to convert the hex digits to an int. Then cast to a char.

(如果你手动实现了词法分析器,或者当你的词法分析器匹配unicode文字时,可以动态进行转换。但是在重读问题时,我看到你正在使用勒克斯发电机......好动!)

(If you had implemented the lexer by hand, an alternatively would be to do the conversion on the fly as your lexer matches the unicode literal. But on rereading the question, I see that you are using a lexer generator ... good move!)

这篇关于Java中的Unicode到字符串转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆