有谁知道如何将字符串从 ISO-8859-1 转换为 UTF-8 并返回 Java?
我从网上获取一个字符串并将其保存在 RMS (J2ME) 中,但我想保留特殊字符并从 RMS 获取字符串,但使用 ISO-8859-1 编码。我该怎么做呢?
有谁知道如何将字符串从 ISO-8859-1 转换为 UTF-8 并返回 Java?
我从网上获取一个字符串并将其保存在 RMS (J2ME) 中,但我想保留特殊字符并从 RMS 获取字符串,但使用 ISO-8859-1 编码。我该怎么做呢?
一般来说,你不能这样做。UTF-8 能够编码任何 Unicode 代码点。ISO-8859-1 只能处理其中的一小部分。因此,从 ISO-8859-1 转码到 UTF-8 是没有问题的。当发现不受支持的字符时,从 UTF-8 倒退到 ISO-8859-1 将导致“替换字符”(�) 出现在您的文本中。
要转码文本:
byte[] latin1 = ...
byte[] utf8 = new String(latin1, "ISO-8859-1").getBytes("UTF-8");
或者
byte[] utf8 = ...
byte[] latin1 = new String(utf8, "UTF-8").getBytes("ISO-8859-1");
您可以使用较低级别的Charset
API 进行更多控制。例如,您可以在找到不可编码的字符时引发异常,或者使用不同的字符替换文本。
这对我 有用:(“üzüm bağları”是用土耳其语写的正确)
将 ISO-8859-1 转换为 UTF-8:
String encodedWithISO88591 = "üzüm baÄları";
String decodedToUTF8 = new String(encodedWithISO88591.getBytes("ISO-8859-1"), "UTF-8");
//Result, decodedToUTF8 --> "üzüm bağları"
将 UTF-8 转换为 ISO-8859-1
String encodedWithUTF8 = "üzüm bağları";
String decodedToISO88591 = new String(encodedWithUTF8.getBytes("UTF-8"), "ISO-8859-1");
//Result, decodedToISO88591 --> "üzüm baÄları"
如果你有一个String
,你可以这样做:
String s = "test";
try {
s.getBytes("UTF-8");
} catch(UnsupportedEncodingException uee) {
uee.printStackTrace();
}
如果你有一个 'broken' String
,你做错了什么,将 a 转换为String
另String
一种编码的 a 显然不是要走的路!您可以将 a 转换String
为 a byte[]
,反之亦然(给定编码)。在 JavaString
中是 AFAIK 编码的,UTF-16
但这是一个实现细节。
假设您有 a InputStream
,您可以读入 a byte[]
,然后将其转换为 a String
using
byte[] bs = ...;
String s;
try {
s = new String(bs, encoding);
} catch(UnsupportedEncodingException uee) {
uee.printStackTrace();
}
甚至更好(感谢埃里克森)InputStreamReader
这样使用:
InputStreamReader isr;
try {
isr = new InputStreamReader(inputStream, encoding);
} catch(UnsupportedEncodingException uee) {
uee.printStackTrace();
}
这是字符串输出的一种简单方法(我创建了一个方法来执行此操作):
public static String (String input){
String output = "";
try {
/* From ISO-8859-1 to UTF-8 */
output = new String(input.getBytes("ISO-8859-1"), "UTF-8");
/* From UTF-8 to ISO-8859-1 */
output = new String(input.getBytes("UTF-8"), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return output;
}
// Example
input = "Música";
output = "Música";
正则表达式也可以很好并且可以有效地使用(替换所有未ISO-8859-1
用空格覆盖的 UTF-8 字符):
String input = "€Tes¶ti©ng [§] al€l o€f i¶t _ - À ÆÑ with some 9umbers as"
+ " w2921**#$%!@# well Ü, or ü, is a chaŒracte⚽";
String output = input.replaceAll("[^\\u0020-\\u007e\\u00a0-\\u00ff]", " ");
System.out.println("Input = " + input);
System.out.println("Output = " + output);
Apache Commons IO Charsets 类可以派上用场:
String utf8String = new String(org.apache.commons.io.Charsets.ISO_8859_1.encode(latinString).array())
这是一个将 UNICODE (ISO_8859_1) 转换为 UTF-8 的函数
public static String String_ISO_8859_1To_UTF_8(String strISO_8859_1) {
final StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < strISO_8859_1.length(); i++) {
final char ch = strISO_8859_1.charAt(i);
if (ch <= 127)
{
stringBuilder.append(ch);
}
else
{
stringBuilder.append(String.format("%02x", (int)ch));
}
}
String s = stringBuilder.toString();
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
String strUTF_8 =new String(data, StandardCharsets.UTF_8);
return strUTF_8;
}
测试
String strA_ISO_8859_1_i = new String("الغلاف".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
System.out.println("ISO_8859_1 strA est = "+ strA_ISO_8859_1_i + "\n String_ISO_8859_1To_UTF_8 = " + String_ISO_8859_1To_UTF_8(strA_ISO_8859_1_i));
结果
ISO_8859_1 strA est = اÙغÙا٠String_ISO_8859_1To_UTF_8 = الغلاف
将 ISO-8859-1 字符串转换为 UTF-8 字符串的最简单方法。
private static String convertIsoToUTF8(String example) throws UnsupportedEncodingException {
return new String(example.getBytes("ISO-8859-1"), "utf-8");
}
如果我们想将 UTF-8 字符串转换为 ISO-8859-1 字符串。
private static String convertUTF8ToISO(String example) throws UnsupportedEncodingException {
return new String(example.getBytes("utf-8"), "ISO-8859-1");
}
此外,一种无需使用 String 类的构造函数即可将 ISO-8859-1 字符串转换为 UTF-8 字符串的方法。
public static String convertISO_to_UTF8_personal(String strISO_8859_1) {
String res = "";
int i = 0;
for (i = 0; i < strISO_8859_1.length() - 1; i++) {
char ch = strISO_8859_1.charAt(i);
char chNext = strISO_8859_1.charAt(i + 1);
if (ch <= 127) {
res += ch;
} else if (ch == 194 && chNext >= 128 && chNext <= 191) {
res += chNext;
} else if(ch == 195 && chNext >= 128 && chNext <= 191){
int resNum = chNext + 64;
res += (char) resNum;
} else if(ch == 194){
res += (char) 173;
} else if(ch == 195){
res += (char) 224;
}
}
char ch = strISO_8859_1.charAt(i);
if (ch <= 127 ){
res += ch;
}
return res;
}
}
该方法基于将本网站的 utf-8 编码为 iso-8859-1。 将 utf-8 编码为 iso-8859-1