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,290 points
1,400 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,100 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, 2019 by Sirajul
• 59,230 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,370 points
2,044 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, 2019 in Java by Sirajul
• 59,230 points
2,857 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, 2019 in Java by Sirajul
• 59,230 points
3,213 views
0 votes
2 answers

How to convert a byte into Hexadecimal in Java?

private static final String ...READ MORE

answered Aug 29, 2019 in Java by Sirajul
• 59,230 points
1,993 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,190 points
1,796 views
0 votes
1 answer

How to convert Java String into byte[]?

The object your method decompressGZIP() needs is a byte[]. So the ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,890 points
1,522 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,140 points
787 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,920 points
5,468 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,920 points
777 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, 2019 in Java by Sirajul
• 59,230 points
22,343 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP