4 Replies Latest reply on Oct 12, 2010 9:41 AM by Flex harUI

    Convert HEX string to double (using ByteArray?)

    FrankDelporte

      Hello,

       

      I have a HEX string I whish to convert back to a double number.

       

      The hex code comes from a C# program which processes data like this:

      byte[] bytes = BitConverter.GetBytes(val);
      where val is a double.

       

      So for example the number 3985856 is converted to 00 00 00 00 E0 68 4E 41.

       

      Starting from the hex value as a string, how could this be converted back to a number with actionscript ?

       

      This is my code but gives an error #2030: end of file was encountered.

       


      var str:String = "00000000E0684E41";

       

      public static function hex2double(str:String):Number
      {
        var rt:Number = 0;

       

        var b:ByteArray = new ByteArray();

        for (var i:Number = 0; i < str.length / 2; i++)
        {
           var thisNumber:int = parseInt(str.substr(i * 2, 2), 16);
           b.writeByte(thisNumber);
        }

       

        try
        {
           rt = b.readDouble();
        }
        catch(e:EOFError)
        {
           trace(e);
        }
                 
        return rt;
      }

       

       

      Thanks,

      Frank