1

I am interested in the question of how to most efficiently split a number (Long.MAX_VALUE or higher) into its digits and place it in a byte[]. I used converting a number to a string, and then turned the string into an array with a symbol and wrote it into an array (Pre-subtracting '0'). But I would like to know if there are more effective ways to solve this problem.

Example:

long number = 4644864;
byte[] array = toArray(number);

And in byte[] array values will be stored in the form [4,6,4,4,8,6,4]

Nanodesy
  • 21
  • 2
  • 2
    Does this answer your question? [How do I convert Long to byte\[\] and back in java](https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java) – deadshot Sep 15 '20 at 09:28
  • @deadshot: this is about "long" numbers (as in the english words) with values bigger than `Long.MAX_VALUE`). – Joachim Sauer Sep 15 '20 at 09:29
  • Not sure which is most efficient, but you could use the iterative division/modulo 10 approach to split a number into its digits. – Robby Cornelissen Sep 15 '20 at 09:29
  • What format do you have that number in? A `BigInteger`? – Joachim Sauer Sep 15 '20 at 09:29
  • @deadshot I looked at this solution, and as far as I understood, the number is converted exactly into bytes, and I want to see exactly the digits in the array. – Nanodesy Sep 15 '20 at 09:33
  • @JoachimSauer I would like to get the values up to Long.MAX_VALUE for the beginning, but it is possible to get values and higher. – Nanodesy Sep 15 '20 at 09:35
  • @RobbyCornelissen I tried this, but as far as I remember it was less effective – Nanodesy Sep 15 '20 at 09:35

0 Answers0