Solved

string back to byte[]

Posted on 2002-04-19
11
280 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 20 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 70
Impossible to extract MSI from new JAVA releases 2 47
netstat -ano | find "8000" and taskkill /f /pid 2984 3 32
tomcat not starting 6 29
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now