• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

casting from String to byte[]

 
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74381
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]

 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]);
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic