Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

string back to byte[]

Posted on 2002-04-19
11
Medium Priority
?
288 Views
Last Modified: 2010-03-31
eddie aks, Apr 19, 2002  [replies:2]
hi experts, l'm encrypting the String to Byte[] and store the byte to string variable as follow. how can i get back the exact byte[] from String variable? how to use string construct the byte array ?

byte[] bt = "123".getBytes(); //assume bt = [@1234
String s = bt.toString; //now s is "[@1234"

byte[] bt_test = s.getBytes() //bt_test now is [@124cdf(example). this is not what i wants, i wanted it back to the first line byte[] variable value which is [@1234
 
0
Comment
Question by:Explorer060599
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 6953484
String s = new String(bt);
0
 
LVL 92

Expert Comment

by:objects
ID: 6955160
byte[] bt = "123".getBytes();
String s = new String(bt);
byte[] bt_test = s.getBytes();
 
0
 

Author Comment

by:Explorer060599
ID: 6957327
what about i store the string like "[@4534" into database and retrieve it back later? am i still able to get back that in byte[] format ? its the same as "[@4534" once i retreive the string from database and convert it using

String sdb = "[@4534"
String s = new String(sdb);
byte[] bt_test = sdb.getBytes();

wouldn't be the bt_test converted to "[@4534" ?
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

Author Comment

by:Explorer060599
ID: 6957332
actually, l'm having doubt in mind that what it's byte stored actually ? when should we used byte ?
0
 
LVL 92

Expert Comment

by:objects
ID: 6957441
"[@4534" is just the string representation of the byte array. It is not the actual byte array.
0
 
LVL 92

Expert Comment

by:objects
ID: 6957443
byte[] bytes = { (byte) 'h', (byte) 'i' };
String hi = new String(bytes);
0
 

Author Comment

by:Explorer060599
ID: 6958697
i do not know why the following code came out with the
different result? is there a way to convert from byte[] back to string "123".

System.out.println("123".getBytes());
String s1 = new String("123".getBytes());
System.out.println(s1.getBytes());
0
 

Author Comment

by:Explorer060599
ID: 6958708
i do not know why the following code came out with the
different result? is there a way to convert from byte[] back to string "123".actually l'm using pbeCipher to do password encryption and return me a byte[]. i needs to store something into database for later decryption and validation. what l'm used to store is the byte[] string from encrypted string, that is why i got to retrieve the byte string from database and convert it to byte[] format for decryption purpose. if that is not the right way, any others good suggestion ?

System.out.println("123".getBytes());
String s1 = new String("123".getBytes());
System.out.println(s1.getBytes());
0
 
LVL 92

Accepted Solution

by:
objects earned 80 total points
ID: 6958716
Because your not printing the CONTENTS of the byte array, instead you're printing the byte array.
These are two different objects, but they both contain the same value.

System.out.println("123".getBytes());
String s1 = new String("123".getBytes());
byte[] b = s1.getBytes();
for (int i=0; i<b.length; i++)
  System.out.print((char)b[i]);
System.out.println();
0
 
LVL 92

Expert Comment

by:objects
ID: 6958733
String a = "123";
// convert string to byte array
byte[] bt = a.getBytes();
// convert byte array back to string
String b = new String(bt);
// Display results
System.out.println(a+"="+b);
0
 
LVL 92

Expert Comment

by:objects
ID: 6958886
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question