Feb
23
2009
2

Format/convert decimal to hexadecimal (or other numeral system)

My problem was to convert some calculations from decimal to hexadecimal to be used in a canvas background color. Specifically, convert from CMYK, from a pantone list, to RGB and apply the result to a canvas.

In a different post i will describe the convertion, might be useful for someone.

Flex has a way of converting to several numeral systems. For example, from the manual:

var myuint:uint = 9;
trace(myuint.toString(2)); // output: 1001 - binary system
trace(myuint.toString(8)); // output: 11 - octal system

And to create hexadecimal values, to be used in uint format:

var r:uint = 250;
var g:uint = 128;
var b:uint = 114;
var rgb:String = "0x" + r.toString(16) + g.toString(16) + b.toString(16);
trace(rgb); // 0xfa8072

All seems well, except when the result in hexadecimal is just one caracter long, so be sure to check and pad it with a zero.
A simple example:

var r:uint = 8
var rs:String = (r.toString(16).length == 1) ? "0"+r.toString(16) : r.toString(16);

Breaking it down:

r.toString(16) –> converts the var r to hexadecimal, which equals “8″.

I then check the length of the result and, in case it equals 1, add a “0″ to the beginning of the string to pad it.

UPDATE/FIX: As pointed out by Joe (comment below), r.toString(16) converts the decimal number to string representation of a hexadecimal number.

Written by 42 in: Flex | Tags: , , ,
Oct
06
2008
0

Number formatting/padding before and after the decimal

Before i came up with this solution, i had some trouble formatting and padding decimal numbers, specifically when i want to do it before and after the decimal mark. To top it all, i wanted the result signed (with a plus sign(+) or minus sign(-) depending on the value of the variable)

Example:

I want this “-4.3″ into this “-04.3″

Or this “6″ into this “+06.0″

PHP has a lot of number formatting options, from number_format, to sprontf and printf. Well, just search the php docs and you will see what i mean.

The thing is that, as always, i wanted to do it in as less code as possible so, to do what i mentioned above, and after a bit of testing, i came up with this:

$value = -14.6;
printf('%1$s%2$02d.%3$01s', ($value <0 ) ? '-' : '+',floor(abs($value)),(abs($value)-floor(abs($value)))*10);

Although pretty simple, i think its still a bit contrived and dirty. But it works.

A bit of background:

the first part of the printf means:

%1 will contain the first parameter (the value after the first comma), with $s as the format (in sprintf, ’s’ means string, so it will just output the result of the first parameter, no formatting)

%2 has $02d as format so it will show a pad of two digits of an integer treated number showed as decimal

%3 has $01s as format - like i mentioned for %1, shows a string with a pad of one digit

Since i want to keep these posts simple, to serve as reference and jump to other reference sites, i will just leave some links:

PHP docs for sprintf (it has all the formatting…formats):

W3C for PHP math:

Just ask if you need any more feedback or help.

Written by 42 in: PHP | Tags: , ,

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes