UTF-8 转 UNIcode

本文介绍了一种通过将中文字符串转换为Quoted-Printable编码格式的方法来解决中文显示乱码的问题。该方法首先检查输入字符串是否为空,然后使用UTF-8编码将字符串转换为字节数组,并对每个字节进行格式化处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

针对中文显示乱码
private String encodeQuotedPrintable(final String str) {
    if (TextUtils.isEmpty(str)) {
        return "";
    }
    final StringBuilder builder = new StringBuilder();
    int index = 0;
    int lineCount = 0;
    byte[] strArray = null;

    try {
        strArray = str.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        strArray = str.getBytes();
    }
    while (index < strArray.length) {
        builder.append(String.format("=%02X", strArray[index]));
        index += 1;
        lineCount += 3;

        if (lineCount >= 67) {
            // Specification requires CRLF must be inserted before the
            // length of the line
            // becomes more than 76.
            // Assuming that the next character is a multi-byte character,
            // it will become
            // 6 bytes.
            // 76 - 6 - 3 = 67
            builder.append("=\r\n");
            lineCount = 0;
        }
    }
    return builder.toString();
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值