-1

I have an encoded Java integer:

1ABCDEFGH
ex1: 123561287
ex2: 184236230

I wish to extract 2-digit pairs after first digit. In this case, it would be:

ex1: 23, 56, 12, 87
ex2: 84, 23, 62, 30

The naive way I came up was to repeatedly divide by 100 and take mod by 100 to find those 2 digit numbers.

List<Integer> find2DigitGroups(int number) {
// handle scenario when number is not of the format 1ABCDEFGH
...

List<Integer> result = new ArrayList<>();
result.add(number%100);
number/=100;
result.add(number%100);
number/=100;
result.add(number%100);
number/=100;
result.add(number%100);
number/=100;
return result;
} 

Is there any better way for doing this? for binary, I know I could use bitmasking but here I am not sure. Any idea or suggestion would be really helpful.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
akram
  • 157
  • 1
  • 15
  • 1
    The code you've written does nothing except bog standard binary to ASCII conversion. It doesn't decode anything. Your encoding method is unclear. Your question is unclear. – user207421 Aug 28 '20 at 00:06
  • You will get the results in reverse order. –  Aug 28 '20 at 00:32

1 Answers1

-1

I am going to bet that converting to a String is the fastest way to do this (since the conversion is done in one step by the JVM... and it avoids all of the math you are doing). Then you can pick of the characters (no multiplications needed).

I found these benchmarks -- https://www.happycoders.eu/java/how-to-convert-int-to-string-fastest/ (I assume they are correct).

Of course, you should run your own benchmarks to confirm which gives the best performance.