Shift-JIS编码字符串

时间:2016-06-23 12:54:42

标签: java character-encoding character non-ascii-characters shift-jis

在Java中,我正在从以Shift-JIS格式编码的文件中读取一个字节数组,但是获取的字符串中的字符“样式”看起来与普通字符串不同(更宽?)。 这是我对“P”字母的意思的一个例子:
P - P
正如您所看到的,Shift-JIS中的第一个看起来与第二个不同。即使对于Shift-JIS字符串,有没有办法使用“普通”字符?
我正在使用这段代码来执行转换:

String jis = new String(byteArray, Charset.forName("Shift_JIS"));

2 个答案:

答案 0 :(得分:1)

严格来说,这些是不同的角色。第一个是Unicode中的Fullwidth Latin Capital Letter P,来自日本JIS X 0208字符集(U + FF30)。第二个P是ASCII(U + 0050)中的Latin Capital Letter P

因此,您必须将全角字符转换为半角字符。您可以使用ICU4J Transliterator进行此操作。

Transliterator transliterator = Transliterator.getInstance("Halfwidth-Fullwidth");
String result = transliterator.transliterate("P - P");
System.out.println(result); // You will get "P - P"

答案 1 :(得分:0)

下表显示了J2SE 5.0支持的Japanese编码集。新java.nio API使用的规范名称在很多情况下与java.io和java.lang API中使用的名称不同。您可以使用其他日语字体。

----------------------------------------------------------------------------------------------
|Canonical Name for  | Canonical Name for java.io  |               Description               |
|   java.nio API     |      and java.lang API      |                                         |
----------------------------------------------------------------------------------------------
|      EUC-JP        |           EUC_JP            | JISX 0201, 0208 and 0212, EUC encoding  | 
|                    |                             |               Japanese                  |
----------------------------------------------------------------------------------------------
|    ISO-2022-JP     |         ISO2022JP           | JIS X 0201, 0208, in ISO 2022 form,     | 
|                    |                             |               Japanese                  |
----------------------------------------------------------------------------------------------
|      Shift_JIS     |             SJIS            |            Shift-JIS, Japanese          | 
----------------------------------------------------------------------------------------------
|    windows-31j     |           MS932             |             Windows Japanese            | 
----------------------------------------------------------------------------------------------
|  x-euc-jp-linux    |        EUC_JP_LINUX         | JISX 0201, 0208, EUC encoding Japanese  | 
----------------------------------------------------------------------------------------------
|   x-eucJP-Open     |       EUC_JP_Solaris        | JISX 0201, 0208, 0212, EUC encoding     | 
|                    |                             |               Japanese                  |
----------------------------------------------------------------------------------------------
|     x-IBM33722     |           Cp33722           | IBM-eucJP - Japanese (superset of 5050) | 
----------------------------------------------------------------------------------------------
|      x-IBM930      |            Cp930            | Japanese Katakana-Kanji mixed with 4370 | 
|                    |                             |         UDC, superset of 5026           |
----------------------------------------------------------------------------------------------
|      x-IBM939      |            Cp939            | Japanese Latin Kanji mixed with 4370    | 
|                    |                             |         UDC, superset of 5035           |
----------------------------------------------------------------------------------------------
|      x-IBM942      |            Cp942            |  IBM OS/2 Japanese, superset of Cp932   | 
----------------------------------------------------------------------------------------------
|      x-IBM943      |            Cp943            | IBM OS/2 Japanese, superset of Cp932    | 
|                    |                             |         and Shift-JIS                   |
----------------------------------------------------------------------------------------------

资源链接:

Painting Japanese characters using Arial fonts with method drawString(..) (Graphics2D)