4

I've followed this: https://dev.twitter.com/docs/auth/creating-signature

to the end but I can't find how to encode a "binary string" to base64(at the end). I wanted to try online converters first but they don't give the string the show "tnnArxj06cWHq44gCs1OSKk/jLY="

Tried this: http://www.motobit.com/util/base64-decoder-encoder.asp

and

http://www.hash-cracker.com/base64.php#anchor

and

http://www.opinionatedgeek.com/dotnet/tools/Base64Encode/

and none give that string.

I'm going to be using java. But I think all those java tools I search for will give the same result as the online converters. What has to be done to encode a "binary string" to base64?

3
  • Web-based tools really aren't good at binary i/o. Does it have to be web-based? Commented Dec 4, 2011 at 6:32
  • 1
    using commons.apache.org/codec/api-release/org/apache/commons/codec/… ? Commented Dec 4, 2011 at 6:32
  • It doesn't have to be web-based. I'm just trying to figure each step out and what I'd have to do. Commented Dec 4, 2011 at 7:08

3 Answers 3

6

The problem of using those online tools isn't going to be in the base64 conversion - it's going to be parsing the hex string into a byte array to start with. In your real code that won't be a problem, as it'll be the output of another stage. Just to prove that, here's some sample Java code, using a public domain base64 encoder:

public class Test {
    public static void main(String[] args) throws Exception {
        byte[] data = { (byte) 0xB6, (byte) 0x79, (byte) 0xC0, (byte) 0xAF, 
                (byte) 0x18, (byte) 0xF4, (byte) 0xE9, (byte) 0xC5, 
                (byte) 0x87, (byte) 0xAB, (byte) 0x8E, (byte) 0x20, 
                (byte) 0x0A, (byte) 0xCD, (byte) 0x4E, (byte) 0x48, 
                (byte) 0xA9, (byte) 0x3F, (byte) 0x8C, (byte) 0xB6 };

        String text = Base64.encodeBytes(data);
        System.out.println(text);
    }
}

Output: tnnArxj06cWHq44gCs1OSKk/jLY=

Sign up to request clarification or add additional context in comments.

3 Comments

Okay. I get it. Thanks. So here: ostermiller.org/calc/encode.html I go from "b679c0af18f4e9c587ab8e200acd4e48a93f8cb6" to decode hex. Then Encode base64 and it came out right "tnnArxj06cWHq44gCs1OSKk/jLY=". One thing I wasn't seeing was the hex thing.
Are binary string and Base64 String are different or same thing? If they are different, Are binary string and byte array are same thing? I am so much confused about binary string definition.
@RaviKumar: You'll note I don't use the phrase "binary string" anywhere in my answer, as it's basically a nonsense. I don't know exactly what the OP meant, and they didn't provide any code...
1

Java 8 has come up with java.util.Base64 package to help encode bytes to Base64 and vice-versa

import java.util.Base64;


byte[] bytes = string.getBytes();

// returns byte[]
Base64.getEncoder().encode(bytes);

// returns String
Base64.getEncoder().encodeToString(bytes);

Comments

0

You can use this link to convert Binary String to Base64 https://cryptii.com/pipes/binary-to-text

Here is an example of converting Binary String to Base64 String: enter image description here

Here is another example of converting Normal String to Base64 String

enter image description here

1 Comment

I do not see how this answers the question at the top of this page, but it should. Please edit according to How to Answer or delete the answer. Otherwise it risks being flagged as "not an answer" and being deleted.

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.