2

I have to convert a string into binary using UTF-8, but i am unable to do that. I have tried

String encodedName=URLEncoder.encode(name, "UTF-8");

but it doesn't gave me the binary format, it just returned me the same string with slight changes, please help.

EDIT:

what i have to do: I have a string suppose "HelloWorld" I have to convert it to binary using "UTF-8" and then use SHA1 hashing algorithm and from this i will get the hashkey and this is what i need "a hashkey".

I found this one for SHA1 hashing.

   public class AeSimpleSHA1 { 

    private  String convertToHex(byte[] data) { 
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) { 
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do { 
                if ((0 <= halfbyte) && (halfbyte <= 9)) 
                    buf.append((char) ('0' + halfbyte));
                else 
                    buf.append((char) ('a' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while(two_halfs++ < 1);
        } 
        return buf.toString();
    } 

    public  String SHA1(String text) 
    throws NoSuchAlgorithmException, UnsupportedEncodingException  { 
    MessageDigest md;
    md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[40];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();
    return convertToHex(sha1hash);

    } 

} 

2 Answers 2

2

It's not really clear what you mean, but you'd usually use:

byte[] data = name.getBytes("UTF-8");

I'm not sure why you've used URLEncoder in your attempt - or what your "desired result" is.

EDIT: Converting the binary data back to text so you can then call getBytes() on it in your SHA1 method makes no sense. Just change your hashing method do:

public String hashSha1(byte[] data) throws NoSuchAlgorithmException, 
    UnsupportedEncodingException { 
  MessageDigest md = MessageDigest.getInstance("SHA-1");
  md.update(data, 0, data.length);
  return convertToHex(md.digest());
}
Sign up to request clarification or add additional context in comments.

5 Comments

I was not getting that in binary format, that is what was my desired result.Your code is really helpful, I got the encoded format in data but i have to store it as a string, can you please help me on that.
@user1211188: It's entirely unclear what you're trying to achieve. byte[] is binary data, String is text data. Please clarify what you're after - read tinyurl.com/so-hints and edit your question.
OK, lets make it clear i converted the string to binary using UTF-8, now what i got is stored in data.Now, i have to use SHA1 Hashing algorithm and hence for that i need this byte[] data to be a string, so that i can give it as a input for another encoding.
@user1211188: No, the input for SHA-1 should be a byte[], not a String. You should really edit your question to give us the whole picture about what you're trying to do.
@user1211188: You're converting the input text into binary to hash it - don't do that. Just pass in the binary data. Will edit my answer...
0

why dont you simply use string's getBytes() method to convert into UTF-8 bytes array? see below:

String s = "this is my string";
byte[] bytes = s.getBytes();   // default:  UTF-8

the default encoding is UTF-8 in android, though you may further define your own encoding in the same method

3 Comments

Even though I believe UTF-8 is the default encoding on Android, I still think it makes sense to be explicit about it.
@Jon Skeet: If so, then being explicit (as if you had to care for it all the time) acts against the meaning of default UTF-8 and the whole Android programming principles ("Simplify My Life").
@TomeeNS: I think we'll have to agree to disagree on this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.