How to convert a java string into byte array

0 votes
How can I make a string to byte array? Please help!
Jun 21, 2018 in Java by scarlett
• 1,280 points
52 views

2 answers to this question.

0 votes

You can probably use ByteBuffer: 

byte[] byte = ByteBuffer.allocate(4).putInt(1695609641).array();

for (byte a : byte) {
   System.out.format("0x%x ", a);
}
answered Jun 21, 2018 by Perry
• 17,020 points
0 votes

Try using String.getBytes(). It returns a byte[] representing string data. 

Example:

String data = "sample data";
byte[] byteData = data.getBytes();
answered Aug 22 by Sirajul
• 25,340 points

Related Questions In Java

0 votes
1 answer

How to convert a string representation of a hex dump to a byte array using Java?

public static byte[] hexStringToByteArray(String s) { ...READ MORE

answered Sep 26, 2018 in Java by sharth
• 3,320 points
151 views
0 votes
2 answers

How can I convert byte array into hex string in Java?

public static String byteArrayToHex(byte[] a) { ...READ MORE

answered Aug 29 in Java by Sirajul
• 25,340 points
382 views
0 votes
2 answers

How to convert a JSON String into Object in Java?

You could probably check out Google's Gson: ...READ MORE

answered Aug 21 in Java by Sirajul
• 25,340 points
286 views
0 votes
2 answers

How to convert a byte into Hexadecimal in Java?

private static final String ...READ MORE

answered Aug 29 in Java by Sirajul
• 25,340 points
36 views
0 votes
1 answer

How can we convert Strings to and from UTF8 byte arrays?

To convert STring to byte[]: String s = ...READ MORE

answered Jun 21, 2018 in Java by Akrati
• 3,150 points
29 views
0 votes
1 answer

How can two strings be concatenated in java?

You can concatenate Strings using the + operator: System.out.println("Your number ...READ MORE

answered Jun 6, 2018 in Java by Daisy
• 8,050 points
20 views
0 votes
2 answers

How an object array can be converted to string array in java?

System.arraycopy is the most efficient way, but ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,880 points
92 views
0 votes
1 answer

Why is char[] preferred over a string?

Strings are immutable. That means once you've ...READ MORE

answered Jun 14, 2018 in Java by scarlett
• 1,280 points
27 views
0 votes
2 answers

How can I do url string decoding in java

We can use URLDecoder: URLDecoder.decode( url, "UTF-8" ); READ MORE

answered Dec 7, 2018 in Java by Sushmita
• 6,880 points
29 views
0 votes
2 answers

What is the difference between getAttribute() and getParameter() in java?

getParameter() returns http request parameters. Those passed from ...READ MORE

answered Aug 28 in Java by Sirajul
• 25,340 points
583 views