Solved

convert byte[] to string

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

689 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