Converting a set of strings to a byte[] array

前端 未结 6 1019
甜味超标
甜味超标 2021-01-23 04:45

I am tring to convert a set of strings to a byte[] array. At first, I do something like this to convert a byte array to a string:

public String convertByte(byte[         


        
6条回答
  •  醉话见心
    2021-01-23 05:32

    Using Byte.parseByte may help making your second snippet work.

    But, unless you have some specific reason to use that kind of representation, I'd encode strings to byte arrays using the Java methods mentioned in other answers.

    public static byte[] convertStr(String ln)
    {
        System.out.println(ln);
    
        String[] st = ln.split(" * ");
        byte[] byteArray = new byte[23];
        for(int i = 0; i < st.length; i++)
        {
            byteArray[i] = Byte.parseByte(st[i]);
        }
    
        return byteArray;
    }
    

提交回复
热议问题