[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

convert char[] to byte[]

Posted on 2004-11-26
12
Medium Priority
?
681 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:muskad202
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12682104
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12682124
Ignore previous comment. That was for another thread
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12683054
>>.e., from a 20 byte SHA-1 value, i need to get a string of length 60

How?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Assisted Solution

by:RMaruszewski
RMaruszewski earned 80 total points
ID: 12683621
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 160 total points
ID: 12684305
>>.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
 
LVL 2

Author Comment

by:muskad202
ID: 12684497
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12684509
Read the file and put it into a ByteArrayOutputStream
0
 
LVL 2

Author Comment

by:muskad202
ID: 12684880
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12685829
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
 
LVL 2

Author Comment

by:muskad202
ID: 12685926
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12685930
No problem
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12685936
8-)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month18 days, 19 hours left to enroll

834 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