简体   繁体   English

将Java字符串转换为字节数组

[英]Convert Java string to byte array

I have a byte array which I'm encrypting then converting to a string so it can be transmitted. 我有一个字节数组,我正在加密然后转换为字符串,以便它可以传输。 When I receive the string I then have to convert the string back into a byte array so it can be decrypted. 当我收到字符串时,我必须将字符串转换回字节数组,以便解密。 I have checked that the received string matches the sent string (including length) but when I use something like str.getBytes() to convert it to a byte array, it does not match my original byte array. 我已检查收到的字符串是否匹配发送的字符串(包括长度),但是当我使用类似str.getBytes()的内容将其转换为字节数组时,它与我的原始字节数组不匹配。

example output: 示例输出:

SENT: WzShnf/fOV3NZO2nqnOXZbM1lNwVpcq3qxmXiiv6M5xqC1A3
SENT STR: [B@3e4a9a7d
RECEIVED STR: [B@3e4a9a7d
RECEIVED: W0JAM2U0YTlhN2Q=

any ideas how i can convert the received string to a byte array which matches the sent byte array? 任何想法我如何将收到的字符串转换为匹配发送的字节数组的字节数组?

Thanks 谢谢

You used array.toString() , which is implemented like this: 你使用了array.toString() ,它实现如下:

return "[B@" + Integer.toString(this.hashCode(), 16);

(In fact it inherits the definition from Object, and the part before the @ simply is the result of getClass().getName() .) (实际上它继承了Object的定义,而@之前的部分只是getClass().getName() 。)

And the hashCode here does not depend on the content. 而hashCode在这里并不依赖于内容。

Instead, use new String(array, encoding). 而是使用新的String(数组,编码)。

Of course, this only works for byte-arrays which are really representable as Java strings (which then contain readable characters), not for arbitrary arrays. 当然,这只适用于字节数组,它实际上可以表示为Java字符串(然后包含可读字符),而不是任意数组。 There better use base64 like Bozho recommended (but make sure to use it on both sides of the channel). 最好使用像Bozho推荐的base64(但一定要在频道的两侧使用它)。

This looks like Base64. 这看起来像Base64。 Take a look at commons-codec Base64 class. 看看commons-codec Base64类。

You can't just use getBytes() on two different machines, since getBytes uses the plattform's default charset. 你不能只在两台不同的机器上使用getBytes(),因为getBytes使用了plattform的默认字符集。

Decode and encode the array with a specified charset (ie UTF-8) to make sure you get the correct results. 使用指定的字符集(即UTF-8)对数组进行解码和编码,以确保获得正确的结果。

First do convertion of your byte array to proper string, by doing 首先将您的字节数组转换为正确的字符串

String line= new String(Arrays.toString(your_array))

Then send it and use function below 然后发送它并使用下面的功能

public static byte[] StringToByteArray(String line)
{

    String some=line.substring(1, line.length()-1);     
    int element_counter=1;

    for(int i=0; i<some.length(); i++)
    {           
        if (some.substring(i, i+1).equals(","))
        {
            element_counter++;
        }       

    }
    int [] comas =new int[element_counter-1];
    byte [] a=new byte[element_counter];
    if (a.length==1)
    {
        a[0]= Byte.parseByte(some.substring(0));
    }       
    else 
    {
        int j=0;
        for (int i = 0; i < some.length(); i++) 
        {
            if (some.substring(i, i+1).equals(","))
            {
                comas[j]=i;
                j++;
            }
        }           
        for (int i=0; i<element_counter; i++)
        {
            if(i==0)
            {
                a[i]=Byte.parseByte(some.substring(0, comas[i]));
            }
            else if (i==element_counter-1)
            {
                a[i]=Byte.parseByte(some.substring(comas[comas.length-1]+2));
            }
            else
            {
                a[i]=Byte.parseByte(some.substring(comas[i-1]+2, comas[i]));
            }

        }
    }
    return a;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM