Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting from String to byte[]

 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a string

String gString="00000000";
and i want to convert this to an array of byte[]

that is


byte[] result should equal{0,0,0,0,0,0,0,0}. please how do i do this
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you check the API, you will see that String already has a method that will return an array of bytes.

However, a String is made up of chars, and the char '0' does not have a numeric (byte) value of zero, so you will need to adjust for this. See ASCIITable.com.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But your String does not equal {0,0,0,0,etc}. It equals {30, 30, 30 etc} in hexadecimal.
You would have to parse each individual character in an array and cast it to a byte and insert the value in a new array.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: In Java, a String is not simply an array of chars, so you cannot "cast" a String to an array. (See JLS - 10.9 An Array of Characters is Not a String.)
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you certainly can't do this by casting, because a String is now an array of bytes. But it's possible to write a method that will take a String "00000000" as input, and return a byte[] containing { 0, 0, 0, 0, 0, 0, 0, 0 } as output - if that's really what you want. A rough algorithm:

Is that what you're trying to do?
[ October 19, 2007: Message edited by: Jim Yingst ]
 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes jim thats what i want

Originally posted by Jim Yingst:
Well, you certainly can't do this by casting, because a String is now an array of bytes. But it's possible to write a method that will take a String "00000000" as input, and return a byte[] containing { 0, 0, 0, 0, 0, 0, 0, 0 } as output - if that's really what you want. A rough algorithm:

Is that what you're trying to do?

[ October 19, 2007: Message edited by: Jim Yingst ]
 
Henry Wong
author
Marshal
Pie
Posts: 21412
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes jim thats what i want


Well, Jim did a good job writing the pseudo code for you. It should be easy for you to convert that to Java.

Henry
 
Jammy Wells
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the getBytes() method of String class also ...and then cast each byte of the array to char for further use....here is the code below...

class StringByte
{
public static void main(String[] args)
{
String str = "00000000";
byte[] b = str.getBytes();

System.out.println("Hello World!-----"+b.length);

for(int i = 0; i < b.length; i++)
System.out.println("element ----"+(char)b[i]);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic