简体   繁体   English

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

[英]convert byte array to string in java

I try to convert byte array to string in java using new String( bytes, "UTF-8") method, but they only return the object. 我尝试使用new String( bytes, "UTF-8")方法将字节数组转换为java中的new String( bytes, "UTF-8") ,但它们仅返回对象。 like this @AB4634bSbbfa 像这样@ AB4634bSbbfa

So, I searched some way to solve this problem. 因此,我寻找了解决该问题的方法。

I finally get valid string array, by converting hex-code to basic-character array. 通过将十六进制代码转换为基本字符数组,我终于得到了有效的字符串数组。 like this. 像这样。 char[] chars = {"0", "1", ... "e", "f"};

This never happened before why do i have to convert hex-code to get valid string. 这从来没有发生过,为什么我必须转换十六进制代码才能获得有效的字符串。

Here is method. 这是方法。 byte array which is hashed by Mac-sha-256 with specific key when i hashed. 当我哈希时由Mac-sha-256使用特定键哈希的字节数组。

    public static String getHashString() {
        String algorithm = "HmacSHA256";

        String hashKey = "some_key";
        String message = "abcdefg";

        String hexed = "";

        try {
            Mac sha256_HMAC = Mac.getInstance(algorithm);
            SecretKeySpec secret_key = new SecretKeySpec(hashKey.getBytes(), algorithm);
            sha256_HMAC.init(secret_key);

            byte[] hash = sha256_HMAC.doFinal(message.getBytes("UTF-8"));

            // it doesn't work for me.
//            hexed = new String(hash, "UTF-8");

            // it works.
            hexed = bytesToHex(hash);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return hexed;
    }

    public static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
    public static String bytesToHex(final byte[] data ) {
        final int l = data.length;
        final char[] hexChars = new char[l<<1];
        for( int i=0, j =0; i < l; i++ ) {
            hexChars[j++] = HEX_DIGITS[(0xF0 & data[i]) >>> 4];
            hexChars[j++] = HEX_DIGITS[0x0F & data[i]];
        }
        return new String(hexChars);
    }

Thanks. 谢谢。

Following is a sample which shows Conversion of Byte array to String :- 以下是显示将字节数组转换为字符串的示例:

public class TestByte
{    
public static void main(String[] argv) {

        String example = "This is an example";
        byte[] bytes = example.getBytes();

        System.out.println("Text : " + example);
        System.out.println("Text [Byte Format] : " + bytes);
        System.out.println("Text [Byte Format] : " + bytes.toString());

        String s = new String(bytes);
        System.out.println("Text Decryted : " + s);
     }}

I'm not sure the string you get in the end is what you're after. 我不确定最后得到的字符串是您要的字符串。 I think a common scenario is to use 我认为常见的情况是使用

new BASE64Encoder().encode(hash)

which will return you the hashed message as String. 这会将散列的消息作为String返回。

只是做新的String(byteArray);

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

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