Convert Double to a ByteArray

I need to convert several values to Byte Arrays so that they can be transmitted in a DataGramPacket. One of these values is a Double. What is the best way to do this? The data contained in the double needs not have a scale greater than 4.
With other values such as Shorts or Longs, I can represent them using a BigInteger and use BigInteger.toByteArray, but am unable to do this with Doubles as I would lose everything after the decimal point.
Thanks in advance

Knowing that a double is a 64 bits value, which means it has the same number of bytes than a "long", you can produce a long value that represents your double
long l = Double.doubleToRawLongBits(d)where d is the double you want to represent as a byte array. And then you can use this new long as an ordinary long number.
To get your double back, just use
double Double.longBitsToDouble(long l)

Similar Messages

  • Converting double to String

    I am looking for a way to convert double to String.
    I think that I have to use wrapper like Double class and use toString(double d), but I am not sure how to do it.
    Since toString() is static method, I do not have to have an instance of the Double class, so I was wondering what's the correct way to do it.
    I need something like this:
    String MyString = new String (String1.concat ( My_doubleNumber.Double.toString() ) );
    Any Suggestions?
    Thanks!!!

    WOW thanks a lot. These are definately better than what I thought of doing originally.
    In case I need to use the wrapper class somewhere else, I am just wondering what would be a proper use of Double and String?

  • Converting Double to String without exponent

    Hi all,
    I am reading some values from Excelsheet using apache POI library. I have large numbers that need to stored into database as string and not numbers. hence, while reading the excelsheet values, i convert these number.. and the are received as double. While converting double into string I noticed that some values are represented in exponential form.. I would like to avoid this and have pure number values in the form of string,,..
    Can anybody tell me how to do it?
    Thanks in advance,
    Abdel Olakara

    java.text.DecimalFormat

  • FM to convert double byte chars

    Hi All,
    Is there anyone know what are the function module to convert double-byte chars? Thanks.

    Seems like Blue Sky is not clear
    You want to convert what into what?
    Whats the purpose of this requirement, kindly give more details.
    Regards
    Karthik D

  • Convert Double to double

    How can I convert Double to double???

    It's an extremely important skill to learn to read the API and become familiar with the tools you will use to program Java. Java has an extensive set of documentation that you can even download for your convenience. These "javadocs" are indexed and categorized so you can quickly look up any class or method. Take the time to consult this resource whenever you have a question - you'll find they typically contain very detailed descriptions and possibly some code examples.
    Java� API Specifications
    Java� 1.5 JDK Javadocs

  • Convert double precision float string to a number

    I am a newbie with Labview.
    I have a GPIB link with a vector voltmeter and I receive the data as a double precision float string (IEEE standard 754-1985). I would like it to be recognized as a number, but I can't find the proper conversion function. The string to convert is 8 byte long. Does anyone have an idea how to do it? Thank you!

    Asumming your data is big-endian, you should be able to simply typecast it to a DBL (see attache LV 7.0 example).
    (It is possible that your DBL string is little-endian, in this case it will be slightly more complicated. Let us know )Message Edited by altenbach on 05-27-2005 04:49 PM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    StringToDBL.vi ‏23 KB

  • Converting double to float

    This problem only occurs when dealing with large numbers or numbers with a lot of fraction digits.
    Here is an example:
    double d = 7.1E9;
    float f1 = (float)d;
    float f2 = new Float(d).floatValue();
    System.out.println( d + " " + f1 + " " + f2);
    The result is:
    7.1E9 7.1000003E9 7.1000003E9
    I tried other ways of converting to float (using DecimalFormat) but unfortunately nothing works. The resulting float is not the same as the original double.
    Any ideas?
    Thanks.
    Igor.

    float is only 32 bit while double is 64 bit, so you can easily get differences like that.
    Is it a problem? If you results gets inaccurate you should stay with double or use BigDecimal if you really care about the precision. If it's because you want to round the result of before you display it, you can use DecimalFormat.

  • Converting Double to Specific Format.!!!!!!

    Hi,
    I have a requirement to convert the double to a speicfic pattern.
    Example:
    Double: 1222.23 ----> if i input Mantissa=13 decimal=2
    then i want to append zeros before the Mantissa and after decimal
    a) input : Double: 1222.23 if i input Mantissa=13 decimal=2
    output: 000000000122223 -----> total 15 digits
    b) input : Double:1234.1 if i input Mantissa=7 decimal=4
    output: 000123410000 ---> total is 11 digits
    c) input : Double:123.2268 if i input Mantissa=6 decimal=2
    output: 00012322 ---> total is 8 digits
    Insted of writing a logic to get Mantissa and decimal numbers is there any method in api to get the same.
    Insted of saying to see a specific api, anyone can atleast say which method is available to proceed.
    Awaiting
    DARMA

    You really need to learn to use the javadocs...
    Here is a hint to get you started.
    setMaximumIntegerDigits()
    setMaximumFractionDigits()
    Oh, and just one point about DecimalFormat, it uses ROUND_HALF_EVEN (you can also find details of this in the javadocs)

  • Converting double to strings

    I'm having a problem in getting my double to convert to a string, here's my code so far.
         public void calculatePayments()
              double paymentAmount;
              double monthlyInterestRate = (Double.parseDouble(interestBox.getText())/12)/100;
              double amountOfLoan = Double.parseDouble(balanceBox.getText());
              double numberOfPayments = Double.parseDouble(yearsBox.getText()) * 12;
              paymentAmount = (amountOfLoan * monthlyInterestRate) / (1 - Math.pow(1/(1 + monthlyInterestRate),numberOfPayments));
              Double.toString(paymentAmount);
              tableBox.append(paymentAmount);
    And here's the error message I get:
    ProgramInterface.java:134: append(java.lang.String) in javax.swing.JTextArea cannot be applied to (double)
              tableBox.append(paymentAmount);
    As always, help is appreciated.

    This line returns a String but has no affect on paymentAmount.
    Double.toString(paymentAmount);To covert the double to String you can save the return in a variable
    String s = Double.toString(paymentAmount);
    tableBox.append(s);or combine them
    tableBox.append(Double.toString(paymentAmount));

  • Newbie Converting double to string

    I am very new to Java Programming, but I am trying to convert a double to a string
    Here is some code of what I am trying to do:
          String output = "";
          Word words[] = new Word[ 7 ];
                        Then I add the "words" to the array
          for ( int i = 0; i < words.length; i++ ) {
             output += words[ i ].toString();
                      output +="\nListed :" + Double.toString(words[ i ].listings()) + "\n";
         return output; the listings method is type double. I think I can use toString to convert a double to a string but am not sure how to go about doing it.
    Any help is very appreciated.
    Thanks.

    I am very new to Java Programming, but I am trying to
    convert a double to a string
    Here is some code of what I am trying to do:
    String output = "";
    Word words[] = new Word[ 7 ];
    Then I add the "words" to the array
    for ( int i = 0; i < words.length; i++ ) {
    output += words[ i ].toString();
    output +="\nListed :" + Double.toString(words[ i
    ].listings()) + "\n";
    return output;the listings method is type double. I think I can use
    toString to convert a double to a string but am not
    sure how to go about doing it.
    Any help is very appreciated.
    Thanks.Wrong:
    Double.toString(words[ i ].listings())
    Right:
    words[ i ].listings().toString()

  • Convert Double to String and Specify Precision

    Hi.....
    Can anyone tell me what class/method I would use if I want to convert a double to a string and need the ability to specify the precision?
    Thanks,
    Christine

    DecimalFormat

  • How to convert double to unsigned long integer for cRIO analog output?

    All,
    Having issues sending out my arbitrary waveform to my cRIO analog output. My values are doubles and by the time I send them out, they have been converted to U32 values and are all turning out as zeroes. This led me to assume two things: 1) That cRIO analog output can only output integers since the values need to be deployed in the memory first and 2) that I'm missing a step in the conversion process. My values range from 0-8, therefore I don't expect that the simple conversion tool in labview should make everything into zeroes.
    Any help?

    Since we are using the cRIO's FPGA interface, you really should be doing most of this inside of the FPGA.  Use DMA FIFOs to pass your data between your RT and the FPGA (and visa versa).
    On your FPGA, you can have a loop that just reads the analog inputs at whatever loop rate you want.  You just send the data to the RT using a DMA.
    Similarly, use a DMA to send your analog output values to the FPGA.  The FPGA can have another loop that reads the DMA and writes the value to the analog output.  This should be done in the FPGA since you can have the FPGA send out the values at a given (and deterministic) loop rate.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • Convert double quotes to single quotes

    I'm using a regEx to convert all the double quotes in an xml string before saving to an mysql database (any doublequotes get escaped by the PHPcode).
    buildXML = buildXML.replace(/"/g,"'");
    however after that line all the following turns red (I assume because of the unbalanced double quotes).
    what other way can I express the regEx?

    Your syntax looks fine (that compiles, right?)--FlexBuilder is just not being as smart as it should. The easiest way to fix this is to use a RegExp object instead of a RegExp literal:
    buildXML = buildXML.replace(new RegExp('"', 'g'),"'");
    Maciek Sakrejda
    Truviso, Inc.
    www.truviso.com
    Truviso is hiring: http://www.truviso.com/company-careers.php?i=87

  • Trouble converting "double like" string to integer

    Hi everyone,
    I have a trouble here. Let's say I have a string which its value is "314.12345667". the value is "like a double". How do i convert it into an integer where the integer only takes the value of da string before the decimal point (as in the value of string = 314 only)? Thanks for helping.

    hi xamule,
    tey this..
    String s="314.123876"
    double d=Double.parseDouble(s);
    int x=d.intValue();
    hope it may help u..
    bye bye,
    Pramod

  • Converting doubles to strings

    I'm sorry if this has already been posted, but i searched the database for a while and i couldn't find anything that helped. I was just wondering what the code was to convert a double into a string, so it can be displayed on a label or button, etc. Again, i am sorry if this was already posted and i missed it.

    double d=100.0;
    String doubleStr=Double.toString(d);

Maybe you are looking for

  • Inventory Opening and Closing Stock

    Hello Experts, How can i get the inventory opening stock  and inventory closing stock in SAP business One so as to reflect the same in the Profit and Loss Statement and  Balance sheet.. Divya Chaudhary

  • How do I control my iPod remote speaker with iTunes 10.5 upgrade?

    I can no longer control playing my iPod Classic(next/previous/play) using the remote control on my Geneva S remotespeaker.  It worked fine until the iTunes10.5 software upgrade.  I checked the"Allow iTunes audio control from remote speakers" box in P

  • How to use usb flash drive

    So i tried to use a usb flash drive to save a word document on my macbook, but i couldnt find the flash drive, or even where to find the usb port. can somebody tell me how to use a usb flash drive on macbooks?

  • Created extra line to be deleted

    Data in temp.ps after running my program is: % p +newText.map p +pfr.map p +pfu.map p +poa.map From my program, after each execution of the program, a line gets inserted into temp.ps after the line % p +newText.map. From my code the necessary is happ

  • Migration LMS 3.1 - 3.2, restore failed

    Hello, I'm trying to migrate from LMS 3.1 to 3.2, via backup/restore. When doing restore of LMS 3.1 backup on clean install of LMS 3.2 I got following error - (copy/paste) from restorebackup.log: [Mon Apr 11 13:39:48 2011] Copied directory.... [Mon A