Convert short[] to float[]

Hi, I'm trying to convert a short[] to a float[]. The following code works fine, but when I try to convert between arrays I get "cannot resolve symbol" error. Can anyone tell me the cause of this error? Thanks.
short s=2;
float f;
f = new Float(s).floatValue(); //works fine
short[] shortArray;
float[] floatArray;
floatArray = new Float(shortArray).floatValue(); //gets error

Firstly don't use new AnyClass() to do conversions between primatives, it creates object unneccassaily. You can cast primative types from one value to another.
Don't use float unless you really need to. double is just as fast and more accurate. The amount of memory you will save will be worth less than 1 cent. (most of the time)
    public static double[] convertToDoubleArray(short[] shortArray) {
        double[] ret = new double[shortArray.length];
        for (int j = 0; j < shortArray.length; j++)
            ret[j] = shortArray[j];
        return ret;
    }

Similar Messages

  • Convert string to floating-point

    Hi all,
    ..very basic question, but I tryed it for hours and only received short-dumps
    <b>How can I convert a string into a floating-point number?</b>
    Kind regards,
    Stefan

    hi
    try this
    to convert  string to float.
    data : a type f,
    s type string value '1.023'.
    a = s.
    write :/ a.
    to convert float to string.
    data : a type f value '1.023',
    s type string.
    s = a.
    write : s.

  • Converting Byte [] into float []

    Hi, I read the contents of a file into a byte array and am now trying to convert it into a float array.
    here is the code I am using
    public static float [] bytetofloat(byte [] convert){
        float [] data = new float [convert.length/2];
        for(int i = 0;i<convert.length;i+=2){
            for(int j = 0; j <data.length;j++){
            short valueAsShort = (short)( (convert[i] << 8)  | (convert[i+1] & 0xff));
            float valueAsFloat = (float)valueAsShort;
            System.out.println(valueAsFloat);
            valueAsFloat = data[j];
            System.out.println(data[j]);
        }can anyone see anythign wrong with the way I am doing this? I cant see anythign wrong but need to make sure its fine before I can continue.
    any advice on this or a better way to do it would be much appreciated.

    ultiron wrote:
    I'm pretty sure they do. The way im doing it is by taking 2 byte values and changing them to a 16 bit float.
    the way the bytes are shift they should only take up 16 bits.It's not that simple.
    First, a float in Java is always 4 bytes. The fact that it has an integer value that can fit into two bytes is irrelevant
    Second, floating point representation is not the same 2s complement that's used for byte, short, int, and long, so you're not just shifting the bits.
    For eample:
    1,  int: 00000000 00000000 00000000 00000001
    1, float: 00111111 10000000 00000000 00000000
    -1,  int: 11111111 11111111 11111111 11111111
    -1, float: 10111111 10000000 00000000 00000000Having said that, you're casting, so your step of going from short to float is correct. It doesn't just shift; it does conversions like the above. The caveats are:
    1) Is your conversion from bytes to short correct? That depends on what those bytes are supposed to be. If they're big-endian representations of 2-byte, 2s-complement numbers, then your code looks correct. You'll still have to test it of course. You'll want to focus on corner cases.
    00 00 --> 0
    00 01 --> 1
    10 00 --> 4096
    7f ff --> 32767
    80 00 --> -32768
    ff ff --> -1
    2) Can float hold all of short's values? I think it can. It obviously can't hold all of int's values, but I think it's precision is sufficient to cover short.
    By the way, is there a reason you're using float instead of double? Unless you're in an extremely memory-constrained environment, there's no reason to use double.

  • Converting string to float

    I have a decimal string "122339994" which i am trying to convert to float using Float.parseFloat. This results in incorrect value of 1.22339992E8.
    I thought for floats precsision comes into effect after decimal places.
    public static void main(String[] args) {
    String floatString = "122339994";
    float floatNumber = Float.parseFloat(floatString);
    System.out.println("Float is "+floatNumber+". Now double "+Double.valueOf(floatString));
    }

    See this API
    [Java2SE Float|http://download-llnw.oracle.com/javase/6/docs/api/java/lang/Float.html#valueOf%28java.lang.String%29]
    Note that trailing format specifiers, specifiers that determine the type of a floating-point literal (1.0f is a float value; 1.0d is a double value), do not influence the results of this method. In other words, the numerical value of the input string is converted directly to the target floating-point type. In general, the two-step sequence of conversions, string to double followed by double to float, is not equivalent to converting a string directly to float. For example, if first converted to an intermediate double and then to float, the string
    "1.00000017881393421514957253748434595763683319091796875001d"
    results in the float value 1.0000002f; if the string is converted directly to float, 1.0000001f results.
    Its better to see the Java APIs first for any information, we will get almost all the information we need from APIs
    Regards,
    Venkatesh

  • Should I save memory by using byte, short and float?

    Our teacher tells us always to use int and double. But what if I don't need such precision or large values?
    Shouldn't I use byte, short and float instead?

    You don't save any memory if you use floats instead of doubles in a method. These variables only exist on the stack when the method is executing. Also remember that Java may do type conversions from float to double in expressions. This may make your code slower.
    If you have a class with large quantities of doubles, for example an array of 100000 doubles, then you may consider using floats, because the memory saving is substantial. If you access the array from outside the class via double methods you can easily do this change local to the class late in the development process.
    I'd say generally it's better to do as your teacher says and standardize on doubles and integers. If you write your program well you can always optimize later if you have to.

  • Looking for VI where i can convert covert a float number to Q 11.5 representation

    looking for VI where i can convert covert a float number to Q 11.5 representation and what exactly  Q 11.5 representation means.
    Kindly help me on the same.
    Solved!
    Go to Solution.

    Appears to be right, but don't forget to round to the nearest integer.
    http://zone.ni.com/reference/en-XX/help/371361H-01/glang/round_to_nearest/
    Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
    If you don't hate time zones, you're not a real programmer.
    "You are what you don't automate"
    Inplaceness is synonymous with insidiousness

  • Converting Vector with Floats to float[] typecast error?

    Java Developers,
    I am able to convert my String vector to a
    String[] but just can't get the Vector with Floats to be converted to a float[] array. I have looked into google and java.sun.com Forums but still cant seem to find the answer. I would really appreciate your help!
    This is how I am making the conversion from Vector(String) to String[]:
    legendLabelsArray = new String[columnHeads.size()];
    int k=0;
    Iterator e = columnHeads.iterator();
    while(e.hasNext()){
    System.out.println("inside the enumerator");
    legendLabelsArray[k] = e.next().toString();
    System.out.println("Array elements at " + k + " are " + legendLabelsArray[k]);
    System.out.println(k++);
    How can I make something similar to work with a Vector with Floats to float[] instead of Strings?
    Thanks,
    Musaddiq
    [email protected]

    Something like this ...
    float[] floatArray = new float[vectorOfFloats.size()];
    int k=0;
    Iterator e = vectorOfFloats.iterator();
    while(e.hasNext()){
    floatArray[k] = ((Float) e.next()).floatValue();
    k++;

  • Convert hex to float

    I have to convert a bit Hex string into a float. I built a vi from the following C code, but it only works on positive numbers. Any suggestions?
    sample16 = (short) ((MSB << 8) | LSB);
    sample = (float)sample16/32768.0;
    sample*=10.0;
    Attachments:
    Parse Response.vi ‏13 KB

    [email protected] wrote:
    Hi altenbach
    The data is come from flowmeter by MODBUS. 
    Thanks
    Hong
    You are not giving us any useful information.  Look up the manual of your device.  Somewhere in there should be a definition of how the numbers are built up.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • Convert Char to float

    Hi all,
    Am wanting to convert a char value (the infoobject is defined as NUMC) into float in order to perform a calculation in the FOX formula. It would not allow the char value to be stored into a float variable...comes back wz an error message - Types of operands F and <infoobject_numc> do not agree.
    Any ways of doing this? I thought about using a function to perform the conversion from char to float format but dont knw if such a function exists on the BI system?
    Thanks!

    Hi,
    I sometimes use a local variable of type STRING to pass on values to other data types:
    DATA local type STRING.
    local = A.
    B = local.
    But I'm not sure it will work for you. If not, you can do it with an exit function.
    D

  • Convert color to float

    Hello!
    I�m using a JColorChooser to pick a color and I wanted to convert this value in a float.
    Using :
    Color color = Painel_cores.getColor();
    float[] myColor = { color.getRed(), color.getGreen(), color.getBlue() };
    I get trash that can�t be used�
    Using:
    float corRed=color.getRed();
    float corGreen=color.getGreen();
    float corBlue=color.getBlue();
    I get the 3 components separate. How ca I join them to have (255,255,255) ?
    Thanks
    Nuno

    I�m using a JColorChooser to pick a color and I
    wanted to convert this value in a float.A float array, I assume after reading the following:
    Using :
    Color color = Painel_cores.getColor();
    float[] myColor = { color.getRed(), color.getGreen(),
    color.getBlue() };
    I get trash that can�t be used�That surprises me because you simply get a float array containing
    three elements, all set to their correct values after being cast to type
    float ...
    Have you tried to print the three values and what were the results?
    kind regards,
    Jos

  • Convert int to float

    How do I properly convert an int to a float?
    I input a number such as 0x42c26163
    The final number should be 97.19021
    I thought I could just do a cast so I tried this:
    int test = 0x42c26163;
    float test2 = (float)test;
    But test2 comes up 1.120035171 E9
    How do I get to the final 97.19021?
    In another project I'm working on using C#, the conversion works when I use the BitConverter class.
    I'm sure I am just overlooking the obvious, but can someone can help me?

    I suppose that int number is really the content of a float read back from raw memory or received from an instrument or on some communication channel. Rebuilding the float number has been discussed a couple of ways in the forums: I can suggest you look at this thread or this other one which give you feasible solutions.
    This is basically a reflect of how floating point numbers are stored in memory according to IEEE754 standard, which you may want to take a look at to understand a bit how all the matter is treated.
    Proud to use LW/CVI from 3.1 on.
    My contributions to the Developer Zone Community
    If I have helped you, why not giving me a kudos?

  • Convert Object to Float

    Hello,
    I'm trying to convert an Object to a Float and can't seem to get it to work properly. I'm using a HashMap to store object key-value pairs. The values inserted into the HashMap were originally of type float. I can extract the value found in the map, but I cannot then convert that to a float to be assigned to another variable. For example:
    Object holdValue;
    holdValue = someHashMap.get(someKey); // so far so good
    float aFloatNumber = holdValue; // can't do this because
    // holdValue stores an object
    How do I convert "holdValue" from an Object to a float so that it can be assigned to "aFloatNumber"?
    Thank you.

    Well the keys could not have been of type float because float is a primative and HashMap will only take Objects as it's keys and data. So you have an incorrect notion of what is in your HashMap. If the key was a Float (the wrapper class of float) you can just cast it and you would be fine but there is no way to assign an Object to a float, you could convert threw some method (like fields 1 plus field 2 or something) but you can't just convert.

  • Problem in Converting string to float

    Hi,
    I am reading from textField and trying to convert that string value to float.
    Compiler is giving error cannot find symbol toFloat even though I have included java.lang in my program.
    Please help me with this one.
    Thanks.

    Use
    float f = Float.parseFloat(inputString); //where inputString is the string you want to parse

  • 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.

Maybe you are looking for

  • Download 9i R2 Fails

    I am trying to download Oracle 9i R2 (v 9.2.0.1 for Windows XP). There are three download files. Disk1 downloads but then I can not open it. It says that the compressed folder is invalid or corrupt. I've tried this several times over the last few day

  • Error 13019 when trying to sync my Ipod Touch 3rd generation

    For the past two days, I have not been able to sync music unto my Ipod. It started when I tried to sync two new albums onto my Ipod, but one album would not sync (the other album did). Every time I tried to sync it, an "unknown error 13019" popped up

  • File Adapter  sync reading error

    I am getting the following error trying to read a pdf file using file adapter <assign name="Assign2"> <copy> <from variable="receiveInput_process_InputVariable" part="filename"/> <to variable="file"/> </copy> <copy> <from variable="receiveInput_proce

  • Where to get wallet file of OMS 11g

    Hi all, I have installed 11g Grid control OMS and repository database too. now evwerything is perfect, but on the agents i want to map the OMS server at the agent i.e. EMD URL as a netscller name. my server name is : - HOST11g the server name mapped

  • Unable to insert records in PRPS and PROJ tables.

    I want   to insert entries in PROJ and PRPS  tables with new company code. I am selecting entries  with existing company code and changing old co. code to new co. code in work area. Then I am using insert PRPS from work area in the same way inserting