Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert hex to base64

Tags:

java

base64

how to convert hex string into base64 ? I found this page http://home2.paulschou.net/tools/xlate/ but I need some function in java: String base64 = ...decoder(String hex); I found something in the Internet but they are to difficult and use byte array. I am looking for something simplier thx a lot

like image 793
hudi Avatar asked Sep 10 '11 14:09

hudi


People also ask

Is hex same as Base64?

The difference between Base64 and hex is really just how bytes are represented. Hex is another way of saying "Base16". Hex will take two characters for each byte - Base64 takes 4 characters for every 3 bytes, so it's more efficient than hex.

How is Base64 conversion done?

How it works. Base64 encoding takes the original binary data and operates on it by dividing it into tokens of three bytes. A byte consists of eight bits, so Base64 takes 24bits in total. These 3 bytes are then converted into four printable characters from the ASCII standard.

Can we convert file to Base64?

Convert Files to Base64Just select your file or drag & drop it below, press the Convert to Base64 button, and you'll get a base64 string. Press a button – get base64. No ads, nonsense, or garbage. The input file can also be an mp3 or mp4.


2 Answers

Have a look at Commons Codec :

 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.codec.binary.Hex;

byte[] decodedHex = Hex.decodeHex(hex);
byte[] encodedHexB64 = Base64.encodeBase64(decodedHex);
like image 107
Victor Martinez Avatar answered Sep 20 '22 16:09

Victor Martinez


You are not likely to find something that converts directly from hex to base-64. You'll need to find or write a hex decoder and a base-64 encoder, and use a byte[] as an intermediate form.

Compare reality with what you are asking for:

String b64 = encoder.encode(decoder.decode(hex)); /* Too difficult :( !!! */

versus

String b64 = transcoder.transcode(hex);           /* So much simpler! */
like image 21
erickson Avatar answered Sep 20 '22 16:09

erickson