- 2023-05-09 18:06:52
- 2764 热度
- 0 评论
在做Java开发的时候,常常会出现一些乱码,或者无法正确识别或读取的文件,比如常见的validator验证用的消息资源(properties)文 件就需要进行Unicode重新编码。原因是java默认的编码方式为Unicode,而计算机系统编码常常是GBK等编码。需要将系统的编码转换 为java正确识别的编码问题就解决了。
native2ascii 工具将带有本机编码字符(非拉丁 1 和非单一码字符)的文件转换成带有Unicode编码字符的文件。
源文件:old.properties
key1=java無名 key2=博客 key3=测试
转换为Unicode编码,注意指定编码格式,测试也可以默认
native2ascii -encoding UTF-8 old.properties new.properties
native2ascii.exe是一个应用比较简单的转码工具,并且转码是可逆的。
将Unicode编码转为普通编码,注意-reverse参数
新文件:new.properties
native2ascii -reverse -encoding UTF-8 new.properties new2.properties
JDK自带的工具native2ascii可以将uncode编码的文件转换为本地编码的文件,但是不能批量转换文件。
Java代码中的使用,注意,如果你的命令行时没有加编码参数,那么在Eclipse里面使用的时候可能会出现乱码:
public class Native2AsciiUtils { /** * prefix of ascii string of native character */ private static String PREFIX = "\\u"; /** * Native to ascii string. It's same as execut native2ascii.exe. * @param str native string * @return ascii string */ public static String native2Ascii(String str) { char[] chars = str.toCharArray(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < chars.length; i++) { sb.append(char2Ascii(chars[i])); } return sb.toString(); } /** * Native character to ascii string. * @param c native character * @return ascii string */ private static String char2Ascii(char c) { if (c > 255) { StringBuilder sb = new StringBuilder(); sb.append(PREFIX); int code = (c >> 8); String tmp = Integer.toHexString(code); if (tmp.length() == 1) { sb.append("0"); } sb.append(tmp); code = (c & 0xFF); tmp = Integer.toHexString(code); if (tmp.length() == 1) { sb.append("0"); } sb.append(tmp); return sb.toString(); } else { return Character.toString(c); } } /** * Ascii to native string. It's same as execut native2ascii.exe -reverse. * @param str ascii string * @return native string */ public static String ascii2Native(String str) { StringBuilder sb = new StringBuilder(); int begin = 0; int index = str.indexOf(PREFIX); while (index != -1) { sb.append(str.substring(begin, index)); sb.append(ascii2Char(str.substring(index, index + 6))); begin = index + 6; index = str.indexOf(PREFIX, begin); } sb.append(str.substring(begin)); return sb.toString(); } /** * Ascii to native character. * @param str ascii string * @return native character */ private static char ascii2Char(String str) { if (str.length() != 6) { throw new IllegalArgumentException("Ascii string of a native character must be 6 character."); } if (!PREFIX.equals(str.substring(0, 2))) { throw new IllegalArgumentException("Ascii string of a native character must start with \"\\u\"."); } String tmp = str.substring(2, 4); int code = Integer.parseInt(tmp, 16) << 8; tmp = str.substring(4, 6); code += Integer.parseInt(tmp, 16); return (char) code; } public static void main(String[] args) { String uni = "java\u5c0f\u5f3a"; System.out.println(ascii2Native(uni)); } }
运行效果如下:
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Java(82)
- Cloud(82)
- Spring Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- IDE(24)
- Dalston(24)
- mongoDB(22)
- MVC(22)
- JDBC(22)
- IDEA(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- SpringBoot(17)
- Docker(17)
- Vue(16)
- Git(16)
- Eclipse(16)
- JPA(15)
- Apache(15)
- ORA(15)
- jdk(14)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- Oracle(14)
- XML(13)
- JdbcTemplate(13)
- OAuth(13)
- Nacos(13)
- Pro(13)
- JSON(12)
- OAuth2(12)
- Data(12)
- int(11)
- Myeclipse(11)
- stream(11)
- not(10)
- Bug(10)
- maven(9)
- Map(9)
- Hystrix(9)
- ast(9)
- session(8)
- Window(8)
- Swagger(8)
- APP(8)
- Bit(8)
- API(8)
- Cache(7)
- File(7)
- mail(7)
- IntelliJ(7)
- windows(7)
- too(7)
- HTML(7)
- Github(7)
- JavaMail(7)
- apt(6)
- read(6)
- Freemarker(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- nginx(6)
- Server(6)
- jar(6)
- ueditor(6)
- ehcache(6)
- UDP(6)
- RabbitMQ(6)
- and(6)
- star(6)
- Excel(6)
- Log4J(6)
- pushlet(6)
- string(5)
- Syntaxhighlighter(5)
- script(5)
- Tool(5)
- Controller(5)
- swagger2(5)
- ldquo(5)
- input(5)
- Servlet(5)
- Config(5)
- Emlog(5)
- discuz(5)