Solved

string back to byte[]

Posted on 2002-04-19
11
278 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 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