Is there is a faster alternative for Integer.toString(myInt).getBytes(US_ASCII)?

This solution quite straightforward.

import java.util.Arrays; // Needed only for demo purposes

public class LongToBytes
{
  private static final byte ZERO = '0';
  private static final byte MINUS = '-';

  public static byte[] convert(long value)
  {
    // -------------------------------------
    // Manage some particular value directly
    // abs(Long.MIN_VALUE) remains negative
    // -------------------------------------
    if ((value >= 0) && (value < 10))
      return (new byte[]{(byte)(ZERO + value)});
    else if ((value > -10) && (value < 0))
      return (new byte[] {MINUS, (byte)(ZERO - value)});
    else if (value == Long.MIN_VALUE)
      return (Long.toString(value).getBytes());

    // -----------------------------------------------------------------
    // Initialize result
    // The longest value (Long.MIN_VALUE+1) is composed of 20 characters
    // -----------------------------------------------------------------
    byte[] array;
    array = new byte[20];

    // ---------------------------
    // Keep track of eventual sign
    // ---------------------------
    boolean negative;
    negative = (value < 0);
    if (negative)
      value = -value;

    // ----------------------
    // Fill array (backwards)
    // ----------------------
    int size;
    size = 0;
    while (value > 0)
    {
      array[size] = (byte)((value % 10) + ZERO);
      size++;
      value /= 10;
    }

    // -------------------
    // Add sign eventually
    // -------------------
    if (negative)
    {
      array[size] = MINUS;
      size++;
    }

    // -------------------------------------------------------------
    // Compose result, giving it the correct length and reversing it
    // -------------------------------------------------------------
    byte[] result;
    int    counter;
    result = new byte[size];
    for (counter = 0; counter < size; counter++)
      result[size - counter - 1] = array[counter];

    // ----
    // Done
    // ----
    return (result);

  } // convert

  public static void main(String[] args)
  {
    try
    {
      long value;
      value = Long.parseLong(args[0]);
      System.out.println(value);
      System.out.println(Arrays.toString(convert(value)));
    }
    catch (Exception exception)
    {
      exception.printStackTrace();
    }
  }

} // class LongToBytes

UPDATE
I checked the performance calling both ways (the method here above and Long.toString().getBytes()) separately in a loop over 100.000.000, using System.nanoTime() as a stopwatch.

Passing 0, the method above is about 500 times faster.
Relatively small values (between -10.000 and 10.000) have a gain of about 60%.
Huge values (near Long.MIN_VALUE and Long.MAX_VALUE) have a gain of about 40%.

UPDATE 2
Managing specific values (between -9 and 9 and the value Long.MIN_VALUE) separately, things get slightly better.
I updated the method's implementation.

Tags:

Java