Solved

convert byte[] to string

Posted on 2007-11-19
4
543 Views
Last Modified: 2008-02-01
I am trying to convert a byte[] array to a string. I've followed some of the suggestions here on ee, but it doesn't seem to work. I have:

            byte[] uPW = pwCrypt(request.getParameter("password"));
            String usrPW = new String(uPW,"ASCII");

My pwCrypt() function does return a byte[]. After the String() constructor I display both the encrypted uPW byte[] and the usrPW String. The byte[] displays properly, but the String is gobbletygook; nothing resembling the original byte array value. What am I doing wrong?
0
Comment
Question by:jmarkfoley
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20317797
you need to use the coorrect encoding.

But what exactly is pwCrypt doing, if its encrypting the string then it probably will look like gobbletygook.

How ru displaying the encrypted array?
0
 
LVL 9

Expert Comment

by:ysnky
ID: 20317907
you are encrypting the original string then converting it into byte array and then constructions a string from this byte array. to expect have the original string you must decrypt the encrypted string after custructing the new string.

maybe like this;
byte[] uPW = pwCrypt(request.getParameter("password"));
String usrPW = new String(uPW,"ASCII");
usrPW = pwDeCrypt(usrPW);
0
 
LVL 1

Accepted Solution

by:
yyyc186 earned 500 total points
ID: 20332240
Perhaps I'm just on a caffeine and nicotine low, but as I recall,  byte arrays don't convert well to String.  In the little snippet below, recbuf is a byte array I needed to pull a string field out of.  There are other ways to do it and I've done quite a few of them, this is just the first one I found in CMS.

                        char c[] = new char[ rFld.sizeInBytes];
                        l_y = rFld.startPos;
                        for (l_x=0; l_x < rFld.sizeInBytes; l_x++)
                            c [l_x] = (char)recbuf[ l_y + l_x];

                        String tmpStr = new String(c);
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20359010
yyyc186: if you are suffering from caffeine/nicotine deprivation, I doubt it is affecting your reason. Indeed covnersion of byte[] to String has proven difficult. In another ee problem I have been working this too. my byte array is generated by the java encryption digest() method. I can output the byte[]s legibly to the screen, but all my attempts to convert them to an ASCII String for storage in a database result in gobbledegook: lots of 'square' graphics and other non-printables.

I'm sure the problem is solvable, but the effort exceeds the value of my solution. I have found another way. essential by using string.hashCode(): one line of code verses dozens of lines of encryption functions followed by half-a-dozen or so lines of byte[] convertion.   IMO Java suffers from a lack of simplicity in providing simple one-way encryption as a shipped method. Unix/C provides such a simple one-liner: char *crypt(const char *key, const char *salt); hashCode() gets me close enough with 4 billion possible hashed values.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tomcat: Unable to run tomcat service. 2 30
ejb example issues 3 55
add projects t working set in maven 2 44
running on tomcat not jboss eap 7.0 3 33
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

730 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