-3

How in node.js/JS convert big array with 8-bit values like [28, 27, ... 250] to bignumber representation in string?

Is there a lib for that? I cant find it.

Ultra
  • 57
  • 8
  • 1
    Give an example input and output. – Brigand Mar 27 '17 at 22:44
  • 2
    Define what you mean by bignumber please. It's integer, float, which length ? 32, 64 bits ? Also, the answer I gave in this question may be useful to you: http://stackoverflow.com/questions/42985471/converting-array-of-binary-doubles-in-protocol-buffer-to-javascript-numbers/42985700#42985700 – Nelson Teixeira Mar 27 '17 at 22:47
  • While this question has good intent, in its current form, it is off-topic since it is requesting a reference to a tool. In addition to specifying example input and output, you should also show any code you've written that attempts to do this. It shows us you've put in the effort, and aren't just expecting us to do your work for you. – Patrick Roberts Mar 27 '17 at 22:47
  • I believe NodeJS core global object `Buffer` can do this for you. https://nodejs.org/dist/latest-v7.x/docs/api/buffer.html#buffer_class_buffer – Luis Estevez Mar 27 '17 at 22:54
  • @NelsonTeixeira By biginteger I mean arbitrary representation of big integer in string – Ultra Mar 29 '17 at 13:58
  • @FakeRainBrigand here `[28, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 250]` I want to know what integer (can be represented as a string) in base 10 it represents – Ultra Mar 29 '17 at 14:00
  • @PatrickRoberts so you will show me how to work on arbitrary numbers in javascript instead of focusing on my problem? – Ultra Mar 29 '17 at 14:01
  • @LuisEstevez `Buffer` cant handle 128 byte numbers, max one is 64 https://nodejs.org/dist/latest-v7.x/docs/api/buffer.html#buffer_buf_swap64 – Ultra Mar 29 '17 at 14:02
  • Welcome to StackOverflow. Please take some time to complete the [tour](http://stackoverflow.com/tour) and read the help topic [How to ask a good question](http://stackoverflow.com/help/how-to-ask) before posting. StackOverflow also has an [help center](http://stackoverflow.com/help) with lots of information. Thanks. – Nelson Teixeira Mar 31 '17 at 15:47

1 Answers1

1

128 BYTES number ? what ? are you trying to count the number of atoms in the universe or something ?

AFAIU javascript can't handle this. And not any other language I now of. Not any lib I know of.Also Buffer can handle only 64 BIT numbers, not bytes. Which is the usual limit to most languages.

You can't do math in such a large value. If this is just a case of comparing values in a faster way than array loops, what you can do is get a string representation in HEX (cause you can treat bytes separately). Here are a way to get the hex representation:

var arrn = [28, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 250];

function bytetohex(byte){
return (byte & 0xFF).toString(16);
}
var n = arrn.reduce((last,curr,ix)=>(ix==1?bytetohex(arrn[0]):last)+bytetohex(curr));
console.log(n);

Edit

If wall you want is RSA decrypt, you could have asked that in the first place. You would have received a response much more quickly. There are several libraries available for that.

This question has some links: Javascript RSA decryption using private key

There is more here: RSA encryption/decryption compatible with Javascript and PHP

And there are plenty of resources on the web for that.

Community
  • 1
  • 1
Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73
  • AFAIU javascript can handle this https://www.npmjs.com/search?q=arbitrary+number – Ultra Mar 30 '17 at 09:02
  • yes 128 BYTES number which is 1024 BITS – Ultra Mar 30 '17 at 09:02
  • About your code please state if Im right; every number converted to hex joined to a string gives hex representation of a whole number? – Ultra Mar 30 '17 at 09:04
  • @ultra yes if each number is a sigle byte length. If each number is more than one byte length then you have to worry about [https://en.wikipedia.org/wiki/Endianness](https://en.wikipedia.org/wiki/Endianness) – Nelson Teixeira Mar 30 '17 at 13:46
  • @Ultra javascript itself can't handle it.So there isn't any library that can handle it.But it's not only javascript.It's all languages I know of including but not limited to PHP,C,C++,Python and others.In the link you mentioned the first libray (bignumber) says in it's constructor docs: n "number: integer, magnitude 1 to 1e+9 inclusive". The second (big.js) says in it's constructor docs: "There is no limit to the number of digits [...]but the largest recommended exponent magnitude is 1e+6.". I think that shows what I'm saying clearly. – Nelson Teixeira Mar 30 '17 at 13:54
  • Is this somekind of crypto key ? what are you trying to do with it ? – Nelson Teixeira Mar 30 '17 at 14:07
  • plain JS RSA decryption – Ultra Mar 31 '17 at 13:51