flex-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Frédéric THOMAS <webdoubl...@hotmail.com>
Subject RE: Decimal to Hex conversion.
Date Mon, 29 Sep 2014 21:13:25 GMT
Actually, it is too late here to optimize it, any, the correct function is:

function dec2hex(dec:Number):String {
    const carryArr:Array = [];
    const hexIntPart:String = dec.toString(16).toUpperCase();
    const integerPart:int = parseInt(dec.toString());
    const s:String = dec.toString();
    const i2:int = s.indexOf(".");
    const iterationMax:int = (i2 > -1) ? s.substr(i2 + 1).length : 0;
    var fractionalPart:Number = parseFloat((dec - integerPart).toFixed(iterationMax));
    var product:Number;

    var hex:String = "0x" + hexIntPart.toString();

    if (iterationMax > 0) {
        for (var i:uint = 0; i < iterationMax; i++) {
            product = fractionalPart * 16;
            carryArr[carryArr.length] = product.toString(16).toUpperCase();
            const carry:uint = parseInt(product.toString());
            fractionalPart = parseFloat((product - carry).toFixed(iterationMax));
            if (fractionalPart == 0)
                break;
        }
        hex += "." + carryArr.join("");
    }

    return hex;
}


and should be use in the as per the previous example: 
dec2hex(parseFloat(scaledVal.text));
Frédéric THOMAS

> From: webdoublefx@hotmail.com
> To: users@flex.apache.org
> Subject: RE: Decimal to Hex conversion.
> Date: Mon, 29 Sep 2014 21:24:01 +0100
> 
> Ok, I found the problem in mine for big numbers.
> 
> @harbs, your function works for big numbers but not fractional, anyway, I didn't optimize
mine, so, I will use its recursive pattern to recode mine, thanks.
> 
> Frédéric THOMAS
> 
> > Subject: Re: Decimal to Hex conversion.
> > From: harbs.lists@gmail.com
> > Date: Mon, 29 Sep 2014 23:15:35 +0300
> > To: users@flex.apache.org
> > 
> > I have TOTALLY not read this thread, but does this function do what you want?
> > 
> > function toHex(d) {
> >     var r = d % 16;
> >     var result;
> >     if (d-r == 0) 
> >         result = toChar(r);
> >     else 
> >         result = toHex( (d-r)/16 ) + toChar(r);
> >     return result;
> > }
> > function toChar(n) {
> >     const alpha = "0123456789abcdef";
> >     return alpha.charAt(n);
> > }
> > 
> > On Sep 29, 2014, at 11:03 PM, Frédéric THOMAS <webdoublefx@hotmail.com>
wrote:
> > 
> > > Also, it seems to work for fractional small numbers, it doesn't on big one
because this function still rely on the toString(radix) method with on my windows 64 bits
return a wrong result, should a bug to be filled on Adobe or some else first can confirm those
results ?
> > > 
> > > decimal (10) -> dec2hex -> toString(16) -> real
> > > 14159265359 -> 0x34.34BF53E2D -> 0x4BF53E4F -> 0x34BF53E4F
> > > 3.14159265359 -> 0x3.243F6A8885D -> 0x3.0 -> 0x3.243F6A8885A308D
> > > 
> > > In between I'll try to find a way to code the correct function.
> > > 
> > > Frédéric THOMAS
> > > 
> > >> From: webdoublefx@hotmail.com
> > >> To: users@flex.apache.org
> > >> Subject: RE: Decimal to Hex conversion.
> > >> Date: Mon, 29 Sep 2014 20:34:51 +0100
> > >> 
> > >> Actually I wasn't sure of mathematical correctness of the solution I gave
above and I end up writing my own function for it, so, use it instead of the one I gave before:
> > >> 
> > >> function dec2hex(dec:Number):String {
> > >>    const carryArr:Array = [];
> > >>    const integerPart:int = parseInt(dec.toString(16));
> > >>    const s:String = dec.toString();
> > >>    const iterationMax:int = s.substr(s.indexOf(".") + 1).length;
> > >>    var fractionalPart:Number = parseFloat((dec - integerPart).toFixed(iterationMax));
> > >>    var product:Number;
> > >> 
> > >>    var hex:String = "0x" + integerPart.toString();
> > >> 
> > >>    if (iterationMax > 0) {
> > >>        for (var i:uint = 0; i < iterationMax; i++) {
> > >>            product = fractionalPart * 16;
> > >>            carryArr[carryArr.length] = product.toString(16).toUpperCase();
> > >>            const carry:uint = parseInt(product.toString());
> > >>            fractionalPart = parseFloat((product - carry).toFixed(iterationMax));
> > >>            if (fractionalPart == 0)
> > >>                break;
> > >>        }
> > >>        hex += "." + carryArr.join("");
> > >>    }
> > >> 
> > >>    return hex;
> > >> }
> > >> 
> > >> You could probably optimize it but at least it returns the correct result.
> > >> 
> > >> Frédéric THOMAS
> > >> 
> > >>> From: webdoublefx@hotmail.com
> > >>> To: users@flex.apache.org
> > >>> Subject: RE: Decimal to Hex conversion.
> > >>> Date: Mon, 29 Sep 2014 13:24:21 +0100
> > >>> 
> > >>> And to be sure the user is allowed to type decimals only, I would add
on key down listener a check for it like this:
> > >>> 
> > >>> 
> > >>> 
> > >>>                                  if( isNaN(Number(scaledVal.text))
){
> > >>> 
> > >>>                                      scaledVal.text = scaledVal.substring(0,
str.length-1);
> > >>> 
> > >>>                                  }
> > >>> 
> > >>> Or use a preventDefault()
> > >>> 
> > >>> and to optimize a bit, on change, check the event is of type CHANGE
before computing.
> > >>> 
> > >>> HTH,
> > >>> Frédéric THOMAS
> > >>> 
> > >>>> From: sathikeshjith@gmail.com
> > >>>> Date: Mon, 29 Sep 2014 17:42:33 +0530
> > >>>> Subject: Re: Decimal to Hex conversion.
> > >>>> To: users@flex.apache.org
> > >>>> 
> > >>>> Thanks Frederic. It Worked.
> > >>>> 
> > >>>> --
> > >>>> Regards
> > >>>> Saju Thankathurai,
> > >>>> 
> > >>>> 
> > >>>> On Mon, Sep 29, 2014 at 5:27 PM, Frédéric THOMAS <webdoublefx@hotmail.com>
> > >>>> wrote:
> > >>>> 
> > >>>>> 
> > >>>>>    hexVal.text ="0x" + scaledVal.text.split(".").map(function
(item:*,
> > >>>>> index:int, array:Array):String {
> > >>>>>        return int(item).toString(16).toUpperCase();
> > >>>>>    }).join(".");
> > >>>>> 
> > >>>>> Frédéric THOMAS
> > >>>>> 
> > >>>>>> From: miguel.cd.ferreira@hotmail.com
> > >>>>>> To: users@flex.apache.org
> > >>>>>> Subject: RE: Decimal to Hex conversion.
> > >>>>>> Date: Mon, 29 Sep 2014 12:49:59 +0100
> > >>>>>> 
> > >>>>>> so int is the same!
> > >>>>>> if uint is just half of int...
> > >>>>>> lets say you should first read the different types but
for your try use
> > >>>>> number
> > >>>>>> 
> > >>>>>>> From: sathikeshjith@gmail.com
> > >>>>>>> Date: Mon, 29 Sep 2014 17:16:51 +0530
> > >>>>>>> Subject: Re: Decimal to Hex conversion.
> > >>>>>>> To: users@flex.apache.org
> > >>>>>>> 
> > >>>>>>> Hi,
> > >>>>>>> 
> > >>>>>>> I tried using int, it didnt help. Below is the demo
code,
> > >>>>>>> 
> > >>>>>>> <?xml version="1.0" encoding="utf-8"?>
> > >>>>>>> <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
> > >>>>>>>   xmlns:s="library://ns.adobe.com/flex/spark"
> > >>>>>>>   xmlns:mx="library://ns.adobe.com/flex/mx"
> > >>>>>>> initialize="windowedapplication1_initializeHandler(event)"
>
> > >>>>>>> <fx:Script>
> > >>>>>>> <![CDATA[
> > >>>>>>> import mx.events.FlexEvent;
> > >>>>>>> import spark.events.TextOperationEvent;
> > >>>>>>> protected function
> > >>>>> scaledVal_changeHandler(event:TextOperationEvent):void
> > >>>>>>> {
> > >>>>>>> hexVal.text = int(scaledVal.text).toString(16).toUpperCase();
> > >>>>>>> }
> > >>>>>>> protected function hexVal_changeHandler(event:TextOperationEvent):void
> > >>>>>>> {
> > >>>>>>> var texts:String = "0x"+hexVal.text;
> > >>>>>>> scaledVal.text = int(texts).toString(10);
> > >>>>>>> }
> > >>>>>>> protected function
> > >>>>>>> windowedapplication1_initializeHandler(event:FlexEvent):void
> > >>>>>>> {
> > >>>>>>> nativeWindow.maximize();
> > >>>>>>> }
> > >>>>>>> protected function button1_clickHandler(event:MouseEvent):void
> > >>>>>>> {
> > >>>>>>> scaledVal.text = "";
> > >>>>>>> hexVal.text = "";
> > >>>>>>> }
> > >>>>>>> ]]>
> > >>>>>>> </fx:Script>
> > >>>>>>> <fx:Declarations>
> > >>>>>>> <!-- Place non-visual elements (e.g., services,
value objects) here -->
> > >>>>>>> </fx:Declarations>
> > >>>>>>> <s:BorderContainer width="100%" height="100%" borderVisible="true">
> > >>>>>>> <s:VGroup  verticalCenter="0" horizontalCenter="0">
> > >>>>>>> <s:Label width="100%" color="blue"
> > >>>>>>> text="Demo to convert hexa to scaled value and vice-versa"/>
> > >>>>>>> <mx:Form width="100%" height="100%">
> > >>>>>>> <mx:FormHeading label="Enter values into the textbox"/>
> > >>>>>>> <mx:FormItem label="Enter Scaled value">
> > >>>>>>> <s:TextInput id="scaledVal" width="200"
> > >>>>>>> change="scaledVal_changeHandler(event)"/>
> > >>>>>>> </mx:FormItem>
> > >>>>>>> <mx:FormItem label="Enter Hexa value">
> > >>>>>>> <s:TextInput id="hexVal" width="200"
> > >>>>> change="hexVal_changeHandler(event)"/>
> > >>>>>>> </mx:FormItem>
> > >>>>>>> </mx:Form>
> > >>>>>>> <s:Button label="Reset" click="button1_clickHandler(event)"/>
> > >>>>>>> </s:VGroup>
> > >>>>>>> </s:BorderContainer>
> > >>>>>>> </s:WindowedApplication>
> > >>>>>>> 
> > >>>>>>> 
> > >>>>>>> 
> > >>>>>>> --
> > >>>>>>> Regards
> > >>>>>>> 
> > >>>>>>> 
> > >>>>>>> Saju Thankathurai,
> > >>>>>>> 
> > >>>>>>> 
> > >>>>>>> *"We **cannot do great things on this Earth, only small
things with
> > >>>>> great
> > >>>>>>> love"*
> > >>>>>>> *-Mother Teresa (1910-1997)*
> > >>>>>>> 
> > >>>>>>> 
> > >>>>>>> On Mon, Sep 29, 2014 at 5:06 PM, Evyatar Ben Halevi-Arbib
<
> > >>>>>>> evyatarbh@gmail.com> wrote:
> > >>>>>>> 
> > >>>>>>>> You convert initially using uint, so decimals are
omitted.
> > >>>>>>>> 
> > >>>>>>>> Regards,
> > >>>>>>>> Evyatar
> > >>>>>>>> 
> > >>>>>>>> On Mon, Sep 29, 2014 at 2:22 PM, Saju Thankathurai
<
> > >>>>>>>> sathikeshjith@gmail.com>
> > >>>>>>>> wrote:
> > >>>>>>>> 
> > >>>>>>>>> Hi,
> > >>>>>>>>> 
> > >>>>>>>>> How can we convert a decimal value to Hex value?
> > >>>>>>>>> 
> > >>>>>>>>> I need to convert *1345.4567 *value to HEX
value. I used the below
> > >>>>> code
> > >>>>>>>> to
> > >>>>>>>>> convert decimal values to Hex,
> > >>>>>>>>> 
> > >>>>>>>>> hexVal.text = uint(scaledVal.text).toString(16).toUpperCase();
> > >>>>>>>>> 
> > >>>>>>>>> 
> > >>>>>>>>> Below code to convert from Hex to decimal.
> > >>>>>>>>> var texts:String = "0x"+hexVal.text;
> > >>>>>>>>> scaledVal.text = uint(texts).toString(10);
> > >>>>>>>>> 
> > >>>>>>>>> 
> > >>>>>>>>> The value 1345 is converted to HEX without
any issues. But after
> > >>>>> the
> > >>>>>>>>> decimal part, it is not converting.
> > >>>>>>>>> 
> > >>>>>>>>> Could some one give inputs on this conversion.
> > >>>>>>>>> 
> > >>>>>>>>> 
> > >>>>>>>>> --
> > >>>>>>>>> Regards
> > >>>>>>>>> Saju Thankathurai,
> > >>>>>>>>> 
> > >>>>>>>> 
> > >>>>>> 
> > >>>>> 
> > >>>>> 
> > >>> 		 	   		  
> > >> 		 	   		  
> > > 		 	   		  
> > 
>  		 	   		  
 		 	   		  
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message