Trying to encode in base 64 a byte array from a BigInteger in javaScript -


i trying implement exact same java algorythm in javascript :

string s = "6332878812086272"; // example long id = long.valueof(s); final byte[] idbytes = biginteger.valueof(id).tobytearray(); final string idbase64 = base64.getencoder().encodetostring(idbytes); 

is possible javascript not handle big number java biginteger/long? libraries recommendations?

you can try that:

// number -> number in base 2 string var nbrasbits = (6332878812086272).tostring(2); //"10110 01111111 10111000 01000000 00000000 00000000 00000000"  // number in base 2 string -> array of bytes (8 bits) numbers var nbrasintarray = convertiontool(nbrasbits); //[22, 127, 184, 64, 0, 0, 0]  // array of bytes numbers -> bytes var nbrasbytearray= new uint8array(bytes);  // concat in string var str = ""; for(var i=0; i<bytearray.length; i++) {     str += string.fromcharcode(bytearray[i]); }  // base64 btoa(str); //"fn+4qaaaaa==" 

you have implement convertiontool() function convert every byte numerical value. use power of 2 in example:

01111111 = 2^6 + 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 2^0 = 127

note

the java long data type has maximum value of 2^63 - 1 (primitive data type) while javascript number has maximum value of 2^53 - 1 (number.max_safe_integer).

so in javascript won't able process number between 9007199254740991 2^63 - 1 , 9223372036854775807 2^53 - 1, when in java can.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -