1

How can I convert double to byte array in Java? I looked at many other posts, but couldn't figure out the right way.

Input = 65.43 
byte[] size = 6
precision = 2   (this might change based on input)

expected output (byte[]) = 006543

Can I do it without using functions like doubleToLongBits()?

2
  • Are you trying to create a binary-encoded decimal? That's what your output is, but it isn't very clear from the question. Commented Oct 25, 2012 at 15:20
  • Not exactly, I want the decimal point to be removed. If I give input 9999.1234, my output should be 999912 Commented Oct 25, 2012 at 15:30

5 Answers 5

18

Real double to byte[] Conversion

double d = 65.43;
byte[] output = new byte[8];
long lng = Double.doubleToLongBits(d);
for(int i = 0; i < 8; i++) output[i] = (byte)((lng >> ((7 - i) * 8)) & 0xff);
//output in hex would be 40,50,5b,85,1e,b8,51,ec

double to BCD Conversion

double d = 65.43;
byte[b] output = new byte[OUTPUT_LENGTH];
String inputString = Double.toString(d);
inputString = inputString.substring(0, inputString.indexOf(".") + PRECISION);
inputString = inputString.replaceAll(".", "");
if(inputString.length() > OUTPUT_LENGTH) throw new DoubleValueTooLongException();
for(int i = inputString.length() - 1; i >= 0; i--) output[i] = (byte)inputString.charAt(i)
//output in decimal would be 0,0,0,0,6,5,4,3 for PRECISION=2, OUTPUT_LENGTH=8
Sign up to request clarification or add additional context in comments.

2 Comments

The second one is what you asked for, the first is what I started typing before your comment, thought I'd include it anyway.
Good job, I'd imagine the first thing is what the large majority of the ~13,602 viewers came to see.
9
ByteBuffer.allocate(8).putDouble(yourDouble).array()

Comments

1
public static byte[] encode(double input, int size, int precision) {
    double tempInput = input;

    for (int i = 0; i < precision; i++) tempInput *= 10;

    int output = (int) tempInput;

    String strOut = String.format("%0"+size+"d", output);

    return strOut.getBytes();
}

2 Comments

it's perfect, but I would add also a parameter to indicate the locale to avoid problem with , or . in the decimal's part of the input
Yeah but don't forget to do that job in a different function. The function I gave you takes a double as input, not a string... use NumberFormat format = NumberFormat.getInstance(Locale.FRANCE)) for example if you are getting a comma instead of a period as input. (and then use format.parse(inputString).doubleValue(); ) Don't forget to check this answer :)
0
double doubleValue = 10.42123;
DecimalFormat df = new DecimalFormat("#.##");
String newDouble = df.format(doubleValue);
byte[] byteArray = (newDouble.replace(",", "")).getBytes();

for (byte b : byteArray) {
    System.out.println((char)b+"");
}

1 Comment

italian locale... so , as decimal sign
0

This is what I got based on your inputs and it serves my purpose. Thanks for helping out!

static int formatDoubleToAscii(double d, int bytesToUse, int minPrecision, byte in[], int startPos) {

        int d1 = (int)(d * Math.pow(10, minPrecision));

        String t = String.format("%0"+bytesToUse+"d", d1).toString();
        System.out.println("d1 = "+ d1 + " t="+ t + " t.length=" + t.length());

        for(int i=0 ; i<t.length() ; i++, startPos++) {
            System.out.println(t.charAt(i));
            in[startPos] = (byte) t.charAt(i);
        }           

        return startPos;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.