Solved

convert byte[] to string

Posted on 2007-11-19
4
508 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now