473,383 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

convert string to byte[]

254 100+
I have this simple question but i dont know which method to convert it..

the case is like this:

i have encrypted a string in byte[] form.

and since it is a string already, but in the end i still need to decrypted it back to original string. for example:

Expand|Select|Wrap|Line Numbers
  1. String str = "Peter is smart";
  2.  
  3. // encrypt it
  4. byte[] data = enc.encryptString(str);
  5.  
  6. String test_str = data.toString();
  7. System.out.println(test_str);     // this value is : [B@356f
  8.  
  9.  
Expand|Select|Wrap|Line Numbers
  1. String str2 = getTest_Str();  // this String field is a must and value is [B@356f
  2.  
  3. // here i want to make the string into byte[]
  4. byte[] bb = str2.someMethod();  
  5.  
  6. System.out.println(bb);    // i want this bb value to be exactly the same as test_str above, is [B@356f.
  7.  
  8. // here i can do decryption which take byte[] as parameter.
  9. System.out.println(enc.decryptString( bb ));  // this should print "Peter is smart"
  10.  
how to make the string to byte[] without changing the value?

any idea of what i'm talking about ?
any idea ?

thanks in advance.
Nick
Jan 3 '08 #1
9 5706
JosAH
11,448 Expert 8TB
One of the constructors of the String class takes a byte[] array as an argument.

kind regards,

Jos
Jan 3 '08 #2
nickyeng
254 100+
One of the constructors of the String class takes a byte[] array as an argument.

kind regards,

Jos
i know.

maybe the way i explain is confusing but the string is [@B43f.

decryptString(byte[] b) takes byte[] as argument.

So i have to make "[@B43f" to byte[] first, and then pass to decryptString(xx) method.

but i dont know how to make a string to byte[] without changing the value(in this case is [@B43f ) so that the string is [@B43f and the byte array is also [@B43f.

String(byte[]) returns string, not return byte[], so it not help in this case.

any idea?
Jan 3 '08 #3
nickyeng
254 100+
somehow look like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. String str = "[@B43f";
  3.  
  4. byte[] b = str.clone();
  5.  
  6. System.out.println("b = " + b);   // if this value is [@B43f, then means this is what i want.
  7.  
  8.  
but i'm not sure whether this is the way of doing it cos i dont have Eclipse or jdk in my pc here.
Jan 3 '08 #4
BigDaddyLH
1,216 Expert 1GB
somehow look like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. String str = "[@B43f";
  3.  
  4. byte[] b = str.clone();
  5.  
  6. System.out.println("b = " + b);   // if this value is [@B43f, then means this is what i want.
  7.  
  8.  
but i'm not sure whether this is the way of doing it cos i dont have Eclipse or jdk in my pc here.
Wait until you have access to a compiler, then. Us telling you yes or no isn't going to help you learn.
Jan 3 '08 #5
nickyeng
254 100+
Wait until you have access to a compiler, then. Us telling you yes or no isn't going to help you learn.
it doesn't work in j2me application.

i've tried.

any method to do that?
Jan 4 '08 #6
BigDaddyLH
1,216 Expert 1GB
it doesn't work in j2me application.
Why doesn't it work? Analyzing why it failed may help you find a solution.
Jan 4 '08 #7
nickyeng
254 100+
i found code from website eventually that helps me with that.

thanks for your time tho.

NickyEng
Jan 4 '08 #8
nickyeng
254 100+
Why doesn't it work? Analyzing why it failed may help you find a solution.
you're talking like talk pointless.

if everyone could analyze the question, then we don't need to come here and post question tho.

anyway, thanks.
Jan 4 '08 #9
BigDaddyLH
1,216 Expert 1GB
you're talking like talk pointless.

if everyone could analyze the question, then we don't need to come here and post question tho.

anyway, thanks.
I was replying to your post where you said

it doesn't work in j2me application.
It's important to learn how to express problems in better detail. You don't just go to your doctor and only say "I am unwell." Perhaps this article should be in a sticky post at the top of the forum:

How to ask questions the smart way.
Jan 4 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Nathan | last post by:
Is there a way to convert a string to a CipherMessage? I am calling a function that decrypts a CipherMessage and returns the value. The only problem is when I want to use an encrypted value stored...
1
by: Swarup | last post by:
I am reading a file (txt, xml, gif, ico, bmp etc) byte by byte and filling it into a byte arry. Now i have to convert it into a string to store it in the database. I use...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
25
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As...
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
6
by: Bob Altman | last post by:
Hi all, I'm looking for the fastest way to convert an array of bytes to String. I also need to convert a String back to its original Byte() representation. Convert.ToBase64String and...
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
19
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.