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: [email protected]
> To: [email protected]
> 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: [email protected]
> > Date: Mon, 29 Sep 2014 17:42:33 +0530
> > Subject: Re: Decimal to Hex conversion.
> > To: [email protected]
> >
> > Thanks Frederic. It Worked.
> >
> > --
> > Regards
> > Saju Thankathurai,
> >
> >
> > On Mon, Sep 29, 2014 at 5:27 PM, Frédéric THOMAS <[email protected]>
> > 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: [email protected]
> > > > To: [email protected]
> > > > 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: [email protected]
> > > > > Date: Mon, 29 Sep 2014 17:16:51 +0530
> > > > > Subject: Re: Decimal to Hex conversion.
> > > > > To: [email protected]
> > > > >
> > > > > 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 <
> > > > > [email protected]> wrote:
> > > > >
> > > > > > You convert initially using uint, so decimals are omitted.
> > > > > >
> > > > > > Regards,
> > > > > > Evyatar
> > > > > >
> > > > > > On Mon, Sep 29, 2014 at 2:22 PM, Saju Thankathurai <
> > > > > > [email protected]>
> > > > > > 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,
> > > > > > >
> > > > > >
> > > >
> > >
> > >
>