Convert string to byte array

I want to convert the sixth string to a byte and store it in a byte array. I saw the code on the Internet and tried to adapt it to my program. But I completely lost. I am brand new to java. Can someone please help me in performing my conversion. Below is the code I wrote. thank you

public class Main { static String s = ""; public static void main(String[] args) { } private static byte[] hexStringToByteArray(String s) { int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16)); } return data; } } 
-1
source share
4 answers

The code works if you call the method.

 public static void main(String[] args) { String s=""; byte[] b = hexStringToByteArray(s); System.out.println(b); //edit: System.out.println(Arrays.toString(b)); } 
+2
source

You need a simple class utility here . The following is an example in which we convert from a hex string to a byte[] , and then to a hex string , and then we compare it to make sure the conversion was good.

 public class Test { public static void main(String[] args) { String str = ""; String str1 = new String(encodeHex(hexStringToByteArray(str))); if (str1.equals(str)) { System.out.println("String matches "); } } public static byte[] hexStringToByteArray(String str) { char[] data = str.toCharArray(); int len = data.length; byte[] out = new byte[len >> 1]; for (int i = 0, j = 0; j < len; i++) { int f = Character.digit(data[j], 16) << 4; j++; f = f | Character.digit(data[j], 16); j++; out[i] = (byte) (f & 0xFF); } return out; } public static char[] encodeHex(byte[] data) { int l = data.length; char[] out = new char[l << 1]; for (int i = 0, j = 0; i < l; i++) { out[j++] = DIGITS[(0xF0 & data[i]) >>> 4]; out[j++] = DIGITS[0x0F & data[i]]; } return out; } private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; } 

Output:

  String matches 
0
source

When you run a java program, its call to the main method is empty in your case too. you must call your methods in the main method. Since you have a static hexStringToByteArray method, you can call it like this

  public static void main(String[] args) { hexStringToByteArray(s); } 
0
source

I better read. I thought your question was about conversion, but now I'm thinking about it. When it comes to display, others here have noted that you are not calling anything in your main method (where the magic happens). When it comes to conversion, BigInterger can do the hard work for you. Just in case: this will work (the last 5 lines are just to check if the result matches the input signal).

 public static void main(String[] args) { byte[] bytes = new BigInteger(s, 16).toByteArray(); System.out.println(Arrays.toString(bytes)); List<String> hexToCheck = new ArrayList<String>(bytes.length); for (byte b : bytes) { hexToCheck.add(String.format("%02X", b)); } System.out.println(hexToCheck); } 

(if the correct answer to your question fills in the main method, select one of the other answers as the correct answer)

0
source

Source: https://habr.com/ru/post/949762/


All Articles