Solved

convert byte[] to string

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

856 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