Search Unity

Converting hexadecimal Strings to Int, JavaScript

Discussion in 'Scripting' started by attilam, Jul 5, 2008.

  1. attilam

    attilam

    Joined:
    Jun 2, 2007
    Posts:
    86
    Hi,

    I'm trying to convert a string with a hexadecimal number in it to an int.

    I tried using parseInt(mystring, 16), but apparently Unity's implementation of parseInt() cannot take a radix...

    Is there another function to do this, or do I have to write my own parser to do this?
     
  2. ProtonZero

    ProtonZero

    Joined:
    Mar 8, 2008
    Posts:
    406
    I've used:

    Code (csharp):
    1. System.Int32.Parse( myString, NumberStyles.AllowHexSpecifier );
     
  3. attilam

    attilam

    Joined:
    Jun 2, 2007
    Posts:
    86
    Works perfectly, thanks Proton!
     
  4. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Does anyone have an idea how I could convert a string which has the hexadecimal equivalent of a float into a real float?

    Like I have a string containing "C567643F" and I would like to convert this to the correct 32bits single precision float.


    I tried

    Code (csharp):
    1. myFloat=System.Single.Parse("C567643F",NumberStyles.AllowHexSpecifier);
    But that tells me NumberStyles does nog exist in the current context.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,201
    System.Globalization.NumberStyles

    --Eric
     
  6. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Damn, I tested this and it does now recognise the NumberStyle, but now I get


    Strangely enough I work with "floats" and it gives me a "double" error and it looks like this technique is not the way to convert a hexadecimal string to a real single precision float :(