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.

Similar Messages

  • How can i convert object to byte array very*100 fast?

    i need to transfer a object by datagram packet in embeded system.
    i make a code fallowing sequence.
    1) convert object to byte array ( i append object attribute to byte[] sequencailly )
    2) send the byte array by datagram packet ( by JNI )
    but, it's not satisfied my requirement.
    it must be finished in 1ms.
    but, converting is spending 2ms.
    network speed is not bottleneck. ( transfer time is 0.3ms and packet size is 4096 bytes )
    Using ObjectOutputStream is very slow, so i'm using this way.
    is there antoher way? or how can i improve?
    Edited by: JongpilKim on May 17, 2009 10:48 PM
    Edited by: JongpilKim on May 17, 2009 10:51 PM
    Edited by: JongpilKim on May 17, 2009 10:53 PM

    thanks a lot for your reply.
    now, i use udp socket for communication, but, i must use hardware pci communication later.
    so, i wrap the communication logic to use jni.
    for convert a object to byte array,
    i used ObjectInputStream before, but it was so slow.
    so, i change the implementation to use byte array directly, like ByteBuffer.
    ex)
    public class ByteArrayHelper {
    private byte[] buf = new byte[1024];
    int idx = 0;
    public void putInt(int val){
    buf[idx++] = (byte)(val & 0xff);
    buf[idx++] = (byte)((val>>8) & 0xff);
    buf[idx++] = (byte)((val>>16) & 0xff);
    buf[idx++] = (byte)((val>>24) & 0xff);
    public void putDouble(double val){ .... }
    public void putFloat(float val){ ... }
    public byte[] toByteArray(){ return this.buf; }
    public class PacketData {
    priavte int a;
    private int b;
    public byte[] getByteArray(){
    ByteArrayHelper helper = new ByteArrayHelper();
    helper.putInt(a);
    helper.putInt(b);
    return helper.toByteArray();
    but, it's not enough.
    is there another way to send a object data?
    in java language, i can't access memory directly.
    in c language, if i use struct, i can send struct data to copy memory by socket and it's very fast.
    Edited by: JongpilKim on May 18, 2009 5:26 PM

  • Converting object wrapper type array into equivalent primary type array

    Hi All!
    My question is how to convert object wrapper type array into equivalent prime type array, e.g. Integer[] -> int[] or Float[] -> float[] etc.
    Is sound like a trivial task however the problem is that I do not know the type I work with. To understand what I mean, please read the following code -
    //Method signature
    Object createArray( Class clazz, String value ) throws Exception;
    //and usage should be as follows:
    Object arr = createArray( Integer.class, "2%%3%%4" );
    //"arr" will be passed as a parameter of a method again via reflection
    public void compute( Object... args ) {
        a = (int[])args[0];
    //or
    Object arr = createArray( Double.class, "2%%3%%4" );
    public void compute( Object... args ) {
        b = (double[])args[0];
    //and the method implementation -
    Object createArray( Class clazz, String value ) throws Exception {
         String[] split = value.split( "%%" );
         //create array, e.g. Integer[] or Double[] etc.
         Object[] o = (Object[])Array.newInstance( clazz, split.length );
         //fill the array with parsed values, on parse error exception will be thrown
         for (int i = 0; i < split.length; i++) {
              Method meth = clazz.getMethod( "valueOf", new Class[]{ String.class });
              o[i] = meth.invoke( null, new Object[]{ split[i] });
         //here convert Object[] to Object of type int[] or double[] etc...
         /* and return that object*/
         //NB!!! I want to avoid the following code:
         if( o instanceof Integer[] ) {
              int[] ar = new int[o.length];
              for (int i = 0; i < o.length; i++) {
                   ar[i] = (Integer)o;
              return ar;
         } else if( o instanceof Double[] ) {
         //...repeat "else if" for all primary types... :(
         return null;
    Unfortunately I was unable to find any useful method in Java API (I work with 1.5).
    Did I make myself clear? :)
    Thanks in advance,
    Pavel Grigorenko

    I think I've found the answer myself ;-)
    Never thought I could use something like int.class or double.class,
    so the next statement holds int[] q = (int[])Array.newInstance( int.class, 2 );
    and the easy solution is the following -
    Object primeArray = Array.newInstance( token.getPrimeClass(), split.length );
    for (int j = 0; j < split.length; j++) {
         Method meth = clazz.getMethod( "valueOf", new Class[]{ String.class });
         Object val = meth.invoke( null, new Object[]{ split[j] });
         Array.set( primeArray, j, val );
    }where "token.getPrimeClass()" return appropriate Class, i.e. int.class, float.class etc.

  • Converting Object to a Date

    Hey guys some one help me out..
    1) How to convert Object to Date and
    2) Date to a String in a specified format like "MM : DDD : YYYY".
    Thanks in Advance :-)

    Thanks guys.. I got it.

  • 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

  • Converting object libraries vi command line

    Forms, ifbld60.exe, allows you to create Forms, Libraies, Menus, and Object Libraries. It alsow allows you to convert each type from binary-to-text and back. I have been successful in using ifcmp60.exe to convert FORMS, LIBRARIES, and MENUS from binary-to-text and back.
    How can I also convert Object Libries using ifcmp60.exe?

    I should add that the documented types for ifcmp60.exe are FORM, LIBRARY, MENU and PECS. Don't understand PECS, but I get errors when tring to convert using any of these module_type(s).

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

  • 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

  • What is the problem with converting objects with gradients to gradient mesh?

    What is the problem with converting objects with gradients to gradient mesh?

    slange,
    There may arise some smaller corruption states that may be cured by what you did, as you can see in the list. Sometimes, restarting thrice is needed.
    The following is a general list of things you may try when the issue is not in a specific file (you may have tried/done some of them already); 1) and 2) are the easy ones for temporary strangenesses, and 3) and 4) are specifically aimed at possibly corrupt preferences); 5) is a list in itself, and 6) is the last resort.
    If possible/applicable, you should save curent artwork first, of course.
    1) Close down Illy and open again;
    2) Restart the computer (you may do that up to 3 times);
    3) Close down Illy and press Ctrl+Alt+Shift/Cmd+Option+Shift during startup (easy but irreversible);
    4) Move the folder (follow the link with that name) with Illy closed (more tedious but also more thorough and reversible);
    5) Look through and try out the relevant among the Other options (follow the link with that name, Item 7) is a list of usual suspects among other applications that may disturb and confuse Illy, Item 15) applies to CC, CS6, and maybe CS5);
    Even more seriously, you may:
    6) Uninstall, run the Cleaner Tool (if you have CS3/CS4/CS5/CS6/CC), and reinstall.
    http://www.adobe.com/support/contact/cscleanertool.html

  • 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 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;
        }

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

Maybe you are looking for