Solved

string back to byte[]

Posted on 2002-04-19
11
282 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 77
map related example 6 48
use lov values 2 50
Problem to Alipay 10 22
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 …
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

770 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