You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java中实现不同字符集间的代码点转换?

不同字符集间编码值转换的实现方案

要实现你需要的转换,核心思路是通过字节序列作为中间桥梁:把源字符集的编码值转成字节,解码为Java内部的Unicode字符串,再编码为目标字符集的字节,最后把字节组合成目标编码值。具体步骤如下:

实现步骤

  1. 将源代码点转为源字符集的字节序列
    • 单字节字符集(如windows-1250):直接把代码点转成单个字节(处理无符号情况,用(byte)(sourceCodePoint & 0xFF))。
    • 多字节字符集(如GB2312):把代码点拆分为对应字节(比如GB2312是双字节,将十进制代码点转成十六进制后拆成两个字节)。
  2. 用源字符集解码字节序列,得到Unicode字符串
  3. 用目标字符集编码Unicode字符串,得到目标字节序列
  4. 将目标字节序列组合成目标编码值

示例代码

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;

public class CharsetCodePointConverter {
    public static Integer convertCodePoint(Charset sourceCharset, int sourceCodePoint, Charset targetCharset) {
        // 步骤1:将源代码点转为源字符集的字节序列
        ByteBuffer sourceBytes;
        if (sourceCharset.newEncoder().maxBytesPerChar() == 1) {
            // 单字节字符集处理
            sourceBytes = ByteBuffer.wrap(new byte[]{(byte) (sourceCodePoint & 0xFF)});
        } else {
            // 双字节字符集处理(如GB2312)
            byte highByte = (byte) ((sourceCodePoint >> 8) & 0xFF);
            byte lowByte = (byte) (sourceCodePoint & 0xFF);
            sourceBytes = ByteBuffer.wrap(new byte[]{highByte, lowByte});
        }

        // 步骤2:解码为Unicode字符
        CharsetDecoder decoder = sourceCharset.newDecoder();
        CharBuffer charBuffer = CharBuffer.allocate(1);
        CoderResult decodeResult = decoder.decode(sourceBytes, charBuffer, true);
        decoder.flush(charBuffer);
        charBuffer.flip();
        if (charBuffer.length() == 0 || decodeResult.isError()) {
            return null; // 解码失败返回null
        }
        String unicodeStr = charBuffer.toString();

        // 步骤3:编码为目标字符集的字节序列
        CharsetEncoder encoder = targetCharset.newEncoder();
        ByteBuffer targetBytes = ByteBuffer.allocate((int) (unicodeStr.length() * encoder.maxBytesPerChar()));
        CoderResult encodeResult = encoder.encode(CharBuffer.wrap(unicodeStr), targetBytes, true);
        encoder.flush(targetBytes);
        targetBytes.flip();
        if (targetBytes.limit() == 0 || encodeResult.isError()) {
            return null; // 编码失败返回null
        }

        // 步骤4:将字节序列转为目标编码值
        int targetCodePoint = 0;
        while (targetBytes.hasRemaining()) {
            targetCodePoint = (targetCodePoint << 8) | (targetBytes.get() & 0xFF);
        }
        return targetCodePoint;
    }

    public static void main(String[] args) {
        // 测试GB2312转UTF-8
        Charset sourceCharset = Charset.forName("GB2312");
        int sourceCodePoint = 45257; // "吧"的GB2312编码值
        Charset targetCharset = Charset.forName("UTF-8");

        Integer targetCodePoint = convertCodePoint(sourceCharset, sourceCodePoint, targetCharset);
        if (targetCodePoint != null) {
            System.out.println("UTF-8编码值:" + targetCodePoint); // 输出19988
        } else {
            System.out.println("转换失败");
        }

        // 测试windows-1250转Unicode
        Charset win1250 = Charset.forName("windows-1250");
        int winCode = 248; // ř的windows-1250编码值
        Charset unicode = Charset.forName("UTF-16");

        Integer unicodeCode = convertCodePoint(win1250, winCode, unicode);
        if (unicodeCode != null) {
            System.out.println("Unicode代码点:" + unicodeCode); // 输出345
        }
    }
}

注意事项

  • 字符兼容性:如果源字符集的字符在目标字符集中没有对应映射,转换会返回null,需根据业务场景处理该情况。
  • 多字节适配:示例默认处理双字节字符集,对于可变长度字符集(如UTF-8),目标编码值是字节序列的组合整数,实际使用中可根据需求调整输出形式。
  • 异常捕获:实际部署时建议捕获UnsupportedCharsetException,避免传入无效字符集名称。

内容的提问来源于stack exchange,提问作者Jindra Petřík

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 10:05:22