java string 编码_java中GBK编码格式转成UTF8,用一段方法实现怎么做?
时间: 2024-02-09 17:08:36 浏览: 31
可以使用Java中的 `String` 类和 `getBytes()` 方法,以及 `Charset` 类来实现GBK编码格式转成UTF8。
下面是一个示例方法:
```java
public static String convertGBKToUTF8(String gbkStr) {
String utf8Str = null;
try {
// 将字符串编码转换为字节数组,使用GBK编码
byte[] gbkBytes = gbkStr.getBytes("GBK");
// 将字节数组转换为字符串,使用UTF-8编码
utf8Str = new String(gbkBytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return utf8Str;
}
```
在上面的代码中,我们首先将输入的字符串 `gbkStr` 转换为字节数组,使用GBK编码。然后,我们将字节数组转换为字符串,使用UTF-8编码。最后,我们返回转换后的字符串 `utf8Str`。
请注意,如果输入的字符串 `gbkStr` 已经是UTF-8编码,那么转换后的字符串将与原始字符串相同。因此,这种方法只适用于将GBK编码格式转换为UTF-8编码格式的情况。如果需要将其他编码格式转换为UTF-8编码格式,需要根据具体情况进行修改。
相关问题
java 将gbk编码的json字符串转成utf8编码格式
可以使用 Java 中的 `String` 类来实现将 GBK 编码的 JSON 字符串转成 UTF-8 编码格式的字符串,具体步骤如下:
1. 将 GBK 编码的 JSON 字符串转成字节数组,可以使用 `getBytes()` 方法。
```java
byte[] gbkBytes = gbkJsonString.getBytes("GBK");
```
2. 将字节数组按照 GBK 编码格式解码成字符串,然后再用 UTF-8 编码格式重新编码成字节数组。
```java
String utf8JsonString = new String(gbkBytes, "UTF-8");
byte[] utf8Bytes = utf8JsonString.getBytes("UTF-8");
```
3. 将 UTF-8 编码格式的字节数组转成字符串。
```java
String result = new String(utf8Bytes, "UTF-8");
```
完整的代码如下:
```java
String gbkJsonString = "{'name':'张三','age':18}";
byte[] gbkBytes = gbkJsonString.getBytes("GBK");
String utf8JsonString = new String(gbkBytes, "UTF-8");
byte[] utf8Bytes = utf8JsonString.getBytes("UTF-8");
String result = new String(utf8Bytes, "UTF-8");
System.out.println(result);
```
输出结果为:
```
{"name":"张三","age":18}
```
utf8 unicode java_Java实现的utf8,gbk,unicode编码相互转换的代码
以下是 Java 实现的 utf8,gbk,unicode 编码相互转换的代码:
```java
import java.io.UnsupportedEncodingException;
public class EncodingUtil {
public static String utf8ToUnicode(String str) {
try {
byte[] bytes = str.getBytes("UTF-8");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.length; i += 2) {
builder.append("\\u");
String hexStr = Integer.toHexString(bytes[i + 1] & 0xff | (bytes[i] & 0xff) << 8);
if (hexStr.length() == 2) {
builder.append("00");
}
builder.append(hexStr);
}
return builder.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
public static String unicodeToUtf8(String str) {
StringBuilder builder = new StringBuilder();
int i = 0;
while (i < str.length()) {
if (str.charAt(i) == '\\' && str.charAt(i + 1) == 'u') {
String hexStr = str.substring(i + 2, i + 6);
int codePoint = Integer.parseInt(hexStr, 16);
builder.append((char) codePoint);
i += 6;
} else {
builder.append(str.charAt(i));
i++;
}
}
try {
return new String(builder.toString().getBytes("UTF-8"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
public static String gbkToUtf8(String str) {
try {
return new String(str.getBytes("GBK"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
public static String utf8ToGbk(String str) {
try {
return new String(str.getBytes("UTF-8"), "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
public static String unicodeToGbk(String str) {
return utf8ToGbk(unicodeToUtf8(str));
}
public static String gbkToUnicode(String str) {
return utf8ToUnicode(gbkToUtf8(str));
}
}
```
其中,`utf8ToUnicode` 方法将 utf8 编码的字符串转换为 unicode 编码的字符串,`unicodeToUtf8` 方法将 unicode 编码的字符串转换为 utf8 编码的字符串,`gbkToUtf8` 方法将 gbk 编码的字符串转换为 utf8 编码的字符串,`utf8ToGbk` 方法将 utf8 编码的字符串转换为 gbk 编码的字符串,`unicodeToGbk` 方法将 unicode 编码的字符串转换为 gbk 编码的字符串,`gbkToUnicode` 方法将 gbk 编码的字符串转换为 unicode 编码的字符串。