convert char[] to byte[]

hi !!

i need to read a file, calculate its SHA-1, and then encode each byte in the SHA-1 digest as %hh , (i.e., from a 20 byte SHA-1 value, i need to get a string of length 60) .
anywayz, i wrote the code, and then found that the string of length 60 was coming out wrong, so i began verifying whether each step in the entire process was working or not .. anywayz, i found a problem ... i think ..

(1) i read the file using bufferedreader into a char[] array
(2) for testing, i copy char by char into a byte array
( b[i]=(byte)c[i]; )
(3) (yet for testing) i copy byte by byte back into a char array
( c1[i]=b[i]; )
(4)i then write this char array using bufferedwriter into a file ..

however, the output file contains a lot of junk (lots of '?' characters), even though it should match the input file ..

can anyone help me out ??

thanks :)
muskad202
LVL 2
muskad202Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sudhakar_koundinyaCommented:
I have asked similar question  long back

Discussion from below link might help you to decide your IDE
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21048347.html

Personally I suggest Net Beans which is free


Best Regards
Sudhakar
0
sudhakar_koundinyaCommented:
Ignore previous comment. That was for another thread
0
CEHJCommented:
>>.e., from a 20 byte SHA-1 value, i need to get a string of length 60

How?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

RMaruszewskiCommented:
Simple. In Java chars have the size of 16 bits and are unsigned, and bytes have size of 8 bits and are signed. If your input contains chars with numeric value bigger than 127, they won't be properly converted to bytes. And you'll get junk, trying to convert such bytes back to chars.
0
CEHJCommented:
>>.e., from a 20 byte SHA-1 value, i need to get a string of length 60

I see - i think you mean literally %hh. If so, pass the digested bytes to the following:

            public static String toHexString(byte[] rawBytes) {
                  String charIndex = "0123456789abcdef";
                  StringBuffer sb = new StringBuffer(rawBytes.length * 3);
                  for (int i = 0; i < rawBytes.length; i++) {
                        sb.append('%');
                        int ix = (rawBytes[i] >> 4) & 0xF;
                        sb.append(charIndex.charAt(ix));
                        ix = rawBytes[i] & 0xF;
                        sb.append(charIndex.charAt(ix));
                  }
                  return sb.toString();
            }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
muskad202Author Commented:
hi !!

@RMaruszewski : then how do i do the conversion ?? i need to convert the chars to bytes, so that i can calculate its SHA1 hash .. and the MessageDigest functions only accept byte[] as parameter

muskad202
0
CEHJCommented:
Read the file and put it into a ByteArrayOutputStream
0
muskad202Author Commented:
hi !!

(1) ByteArrayOutputStream doesn't have any method that accepts char[] (at least i didn;t find any)
(2) as mentioned in my original question .. this reading into char[], converting to byte[], and then reconverting back to char[] was only for testing. my main aim is to store it in a byte[] so that i can calculate the SHA-1 hash ... and here lies the problem - i don't know how to convert from char[] to byte[]

thnx :)
muskad202
0
CEHJCommented:
You don't read a source file as chars to get a digest, you read it as bytes:


                  BufferedInputStream in = new BufferedInputStream(new FileInputStream(args[0]));
                  final int BUF_SIZE = 4096;
                  byte[] buffer = new byte[BUF_SIZE];
                  int bytesRead = -1;
                  MessageDigest md = MessageDigest.getInstance("SHA1");
                  while ((bytesRead = in.read(buffer, 0, BUF_SIZE)) > -1) {
                        md.update(buffer, 0, bytesRead);
                  }
                  in.close();
                  //DEBUG
                  final byte[] digested = md.digest();
0
muskad202Author Commented:
ok .. that's wht i was looking for .. i was using FileReader,BufferedReader , and they wre reading it as chars, so i assumed that all classes for reading files would be using chars ..
thanks :)
0
CEHJCommented:
No problem
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.