aspose file tools*
The moose likes Beginning Java and the fly likes casting from String to byte[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "casting from String to byte[]" Watch "casting from String to byte[]" New topic
Author

casting from String to byte[]

Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jammy Wells
Greenhorn

Joined: Oct 04, 2007
Posts: 17
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]);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting from String to byte[]