Java byte []到字符串转换输出字节 [英] Java byte[] to string conversion outputting byte

查看:78
本文介绍了Java byte []到字符串转换输出字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我正在通过Internet发送一个编码为字节数组的txt文件,然后将消息转换回另一侧并显示它.问题是,当我尝试显示它时,它总是显示为"[B @ 1ef9f1d"或"[B @ 1764be1""等.

In my code, I am sending a txt file encoded into a byte array over the internet, and then converting the message back on the other side and displaying it. The problem is that when I try displaying it, it always comes out as "[B@1ef9f1d" or "[B@1764be1" etc.

这就是接收数据的地方

private void parsePacket(byte[] data, InetAddress address, int port) {
    String datasent[] = (new String(data).trim()).split(",");
    String type = datasent[0];
    String message = datasent[1];
    switch(type){//Data we are receiving from client, type is 5 char
    default:
        System.out.println(type);
        System.out.println(message);
    case "invalid":
        println("Invalid packet", new Color(255, 155, 155));
        break;
    case "login":
        addConnection(message, address, port);
        break;
    case "SendLog":
        printLog(message);
        break;
    }
}
private void printLog(String message) {
    int charperline = 10;
    String line ="";
    for (int i = 0; i < message.length() / charperline; i++){
        for (int j = 0; j < charperline; j++){
        line += message.charAt(i + j);
        }
        println("LOG: " + line);
        line = "";
    }

}

这就是发送它的原因:

public void sendLog(){
    System.out.println("sendlog()");
    InputStream is = getClass().getResourceAsStream("/LOG.txt");
    try
    {
        byte[] text = new byte[10000];
        is.read(text);
            sendData(("SendLog," + text).getBytes());
        //is.close();
        new File("/LOG.txt").delete();
    } catch (IOException e) {
        e.printStackTrace();
    } 
}
public void sendData(byte[] data){
    DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, 1332);
    try {
        socket.send(packet);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NullPointerException e){
        e.printStackTrace();
    }
}

推荐答案

您所看到的

[B@1ef9f1d

是方法 toString()的结果,该方法的所有类都继承自 Object 类,因为Java中的所有类都扩展了 Object .这是实现为

is the result of the method toString() that all classes inherit from the Object class, since all classes in Java extend Object. This is implemented as

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

在这种情况下, getClass().getName()将返回 [B ",因为它是字节数组.

In this case, getClass().getName() would return [B because it is an array of Bytes.

这是因为Java中的数组对象没有自定义的 toString()方法,它们继承了 Object 的.

This is because array objects in Java do not have a custom toString() method, they inherit Object's.

如果要打印数组的内容,请尝试

If you want to print the contents of an array, try

Arrays.toString(yourByteArray);

对于自定义类,您应该始终实现(覆盖)自己的自定义 toString()方法.这对于日志记录很有用.请注意,将字符串串联与引用类型一起使用时,将隐式使用 toString()方法将您的对象转换为String表示形式.

For custom classes, you should always implement (override) your own custom toString() method. It is useful for logging. Note that String concatenation when used with reference types uses the toString() method implicitly to convert your object into a String representation.

这篇关于Java byte []到字符串转换输出字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆