Solved

string back to byte[]

Posted on 2002-04-19
11
284 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

738 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