java - Converting a set of strings to a byte[] array -


i tring convert set of strings byte[] array. @ first, convert byte array string:

public string convertbyte(byte[] msg) {     string str = "";             for(int = 0; < msg.length; i++) {         str += (msg[i] + " * ");     }            return str; } 

when try convert byte[] array, don't same values ones when converted string. had gave me incorrect values.

i trying along lines of:

public static byte[] convertstr(string ln) {     system.out.println(ln);      string[] st = ln.split(" * ");     byte[] bytearray = new byte[23];     for(int = 0; < st.length; i++)     {         bytearray[i] = st[i].get byte value or something;     }      return bytearray; } 

if try use getbytes() method string api, returns byte array rather byte , problem.

any appreciated.

using byte.parsebyte may making second snippet work.

but, unless have specific reason use kind of representation, i'd encode strings byte arrays using 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 = 0; < st.length; i++)     {         bytearray[i] = byte.parsebyte(st[i]);     }      return bytearray; } 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -