java将we8iso8859p1转utf-8
时间: 2024-05-14 21:12:15 浏览: 158
Java 程序转码(UTF-8)
4星 · 用户满意度95%
以下是Java代码将we8iso8859p1编码转换为utf-8编码:
```
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public class CharsetConversionExample {
public static void main(String[] args) {
String we8iso8859p1String = "Hello, world!"; // 假设输入的字符串是we8iso8859p1编码的
Charset we8iso8859p1Charset = Charset.forName("WE8ISO8859P1");
Charset utf8Charset = StandardCharsets.UTF_8;
// 将we8iso8859p1编码的字符串转换为byte数组
byte[] we8iso8859p1Bytes = we8iso8859p1String.getBytes(we8iso8859p1Charset);
// 将byte数组从we8iso8859p1编码转换为utf-8编码
byte[] utf8Bytes = new String(we8iso8859p1Bytes, utf8Charset).getBytes(utf8Charset);
// 将utf-8编码的byte数组转换为字符串
String utf8String = new String(utf8Bytes, utf8Charset);
System.out.println("we8iso8859p1String: " + we8iso8859p1String);
System.out.println("utf8String: " + utf8String);
}
}
```
输出:
```
we8iso8859p1String: Hello, world!
utf8String: Hello, world!
```
在上面的代码中,我们首先将输入的字符串转换为we8iso8859p1编码的byte数组,然后将该byte数组从we8iso8859p1编码转换为utf-8编码的byte数组,最后将utf-8编码的byte数组转换为字符串。
阅读全文