前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[1252]java byte[] 转string

[1252]java byte[] 转string

作者头像
周小董
发布2023-12-31 08:10:14
1850
发布2023-12-31 08:10:14
举报
文章被收录于专栏:python前行者python前行者

JavaScript byte[] 和string 相互转换

byteToString byte[] 格式转字符串
代码语言:javascript
复制
/**
 * byte[] 格式转字符串
 * @param {byte[]} arr
 */
function byteToString(arr) {
    if(typeof arr === 'string') {
        return arr;
    }
    var str = '',
        _arr = arr;
    for(var i = 0; i < _arr.length; i++) {
        var one = _arr[i].toString(2),
            v = one.match(/^1+?(?=0)/);
        if(v && one.length == 8) {
            var bytesLength = v[0].length;
            var store = _arr[i].toString(2).slice(7 - bytesLength);
            for(var st = 1; st < bytesLength; st++) {
                store += _arr[st + i].toString(2).slice(2);
            }
            str += String.fromCharCode(parseInt(store, 2));
            i += bytesLength - 1;
        } else {
            str += String.fromCharCode(_arr[i]);
        }
    }
    return str;
}
stringToByte 字符串格式转byte[]
代码语言:javascript
复制
/**
 * stringToByte  字符串格式转byte[] 
 * @param {String} str
 */
function stringToByte(str) {
    var bytes = new Array();
    var len, c;
    len = str.length;
    for (var i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if (c >= 0x010000 && c <= 0x10FFFF) {
            bytes.push(((c >> 18) & 0x07) | 0xF0);
            bytes.push(((c >> 12) & 0x3F) | 0x80);
            bytes.push(((c >> 6) & 0x3F) | 0x80);
            bytes.push((c & 0x3F) | 0x80);
        } else if (c >= 0x000800 && c <= 0x00FFFF) {
            bytes.push(((c >> 12) & 0x0F) | 0xE0);
            bytes.push(((c >> 6) & 0x3F) | 0x80);
            bytes.push((c & 0x3F) | 0x80);
        } else if (c >= 0x000080 && c <= 0x0007FF) {
            bytes.push(((c >> 6) & 0x1F) | 0xC0);
            bytes.push((c & 0x3F) | 0x80);
        } else {
            bytes.push(c & 0xFF);
        }
    }
    return bytes;
}

Java byte[] 转string

有以下几种不同的方法可以将Java的byte数组转换为字符串:

方法一:使用String类的构造函数
代码语言:javascript
复制
byte[] byteArray = {65, 66, 67, 68};
String str = new String(byteArray);
代码语言:javascript
复制
//Original String
String string = "hello world";
 
//Convert to byte[]
byte[] bytes = string.getBytes();
 
//Convert back to String
String s = new String(bytes);
 
//Check converted string against original String
System.out.println("Decoded String : " + s);

这种方式使用平台默认字符集

方法二:使用String类的静态方法valueOf()
代码语言:javascript
复制
byte[] byteArray = {65, 66, 67, 68};
String str = String.valueOf(byteArray);
方法三:使用Base64编码

Java 8 开始可以使用Base64类

代码语言:javascript
复制
import java.util.Base64;
 
byte[] byteArray = {65, 66, 67, 68};
String str = Base64.getEncoder().encodeToString(byteArray);
代码语言:javascript
复制
//Original byte[]
byte[] bytes = "hello world".getBytes();
 
//Base64 Encoded
String encoded = Base64.getEncoder().encodeToString(bytes);
 
//Base64 Decoded
byte[] decoded = Base64.getDecoder().decode(encoded);
 
//Verify original content
System.out.println( new String(decoded) );
方法四:使用Apache Commons Codec库的Hex类
代码语言:javascript
复制
import org.apache.commons.codec.binary.Hex;
 
byte[] byteArray = {65, 66, 67, 68};
String str = Hex.encodeHexString(byteArray);

这些方法在将byte数组转换为字符串时可能会有一些差异,具体选择哪种方法取决于具体的需求和环境。

参考:https://www.jianshu.com/p/2a9e82f48540 https://blog.csdn.net/ximaiyao1984/article/details/130954088

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-12-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • JavaScript byte[] 和string 相互转换
    • byteToString byte[] 格式转字符串
      • stringToByte 字符串格式转byte[]
      • Java byte[] 转string
        • 方法一:使用String类的构造函数
          • 方法二:使用String类的静态方法valueOf()
            • 方法三:使用Base64编码
              • 方法四:使用Apache Commons Codec库的Hex类
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档