Solved

convert byte[] to string

Posted on 2007-11-19
4
531 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 38
ForLoop Example 3 49
jdbc error in jsp application 20 46
Which non-HTML GUI front end to use with Java? 3 22
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

803 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