0

I have a requirement to convert a binary to hexadecimal. The issue is very similar to the one mentioned in below link but i need to use java instead of node.js - Converting mongodb Binary _id to LUUID using node

The column value is a binary base64 represented as a data type string in Hive. "AAGOBh3pqkWvtaK8AO2U9w==". The required hex output - '00018e06-1de9-aa45-afb5-a2bc00ed94f7'

I am creating a Hive UDF in Java to do this conversion and I am working on Hive 1.0 version. I wanted to represent (not convert) this string "AAGOBh3pqkWvtaK8AO2U9w==" as a base64 to further convert to hex.

Similar to the node.js example in the above link - var hex = new Buffer(bin, 'base64').toString('hex');

However base64 doesnt seem to be valid.

Is there a way to do it without Hive UDF ? If not, how can we represent (not convert) the string column as a base64 column.

Thanks for your help!!

Suraj
  • 575
  • 1
  • 9
  • 23
  • Let us understand this. You have AAGOBh3pqkWvtaK8AO2U9w== in some db column's cell right ? And you have decided to show it in hexa-decimal format in your HTML. Is that right ? – Surajit Biswas Mar 27 '18 at 07:07
  • Hi Surajit, That is correct (except for the HTML). There is a string column in db that has value AAGOBh3pqkWvtaK8AO2U9w== (which is binary base64) and I need to convert to its equivalent hexadecimal format - '00018e06-1de9-aa45-afb5-a2bc00ed94f7' – Suraj Mar 27 '18 at 09:03

1 Answers1

2

I will recommend you to use Apache Common Codec Your string looks like a base64 string.

In Java, Use :

String data = "AAGOBh3pqkWvtaK8AO2U9w==";
byte[] bytesOfBase64 = Base64.decodeBase64(data);
String hexString = Hex.encodeHexString(bytesOfBase64);
System.out.println(hexString);

Reference This

In JavaScript, Use:

function base64toHEX(base64) {

  var raw = atob(base64);

  var HEX = '';

  for ( i = 0; i < raw.length; i++ ) {

    var _hex = raw.charCodeAt(i).toString(16)

    HEX += (_hex.length==2?_hex:'0'+_hex);

  }
  return HEX.toUpperCase();

}

console.log(base64toHEX("AAGOBh3pqkWvtaK8AO2U9w=="));

Reference This:

Surajit Biswas
  • 779
  • 7
  • 25
  • @Suraj, if so, please choose vote for my answer and chose the solution (green tick)...it will help me. Thanks. – Surajit Biswas Mar 27 '18 at 13:44
  • Surajit..I tried to do so number of times..but the sys says the upvote is recorded but will not display publicly as I do not have enough reputations yet. (new to stack overflow). – Suraj Mar 27 '18 at 23:40