My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package org.as3lib.math
{
/**
* Converts a uint into a string in the format “0x123456789ABCDEF”.
* This function is quite useful for displaying hex colors as text.
*
* @author Mims H. Wright (modified by Pimm Hogeling)
*
* @example
* <listing version="3.0">
* getNumberAsHexString(255); // 0xFF
* getNumberAsHexString(0xABCDEF); // 0xABCDEF
* getNumberAsHexString(0x00FFCC); // 0xFFCC
* getNumberAsHexString(0x00FFCC, 6); // 0x00FFCC
* getNumberAsHexString(0x00FFCC, 6, false); // 00FFCC
* </listing>
*
*
* @param number The number to convert to hex. Note, numbers larger than 0xFFFFFFFF may produce unexpected results.
* @param minimumLength The smallest number of hexits to include in the output.
* Missing places will be filled in with 0’s.
* e.g. getNumberAsHexString(0xFF33, 6); // results in "0x00FF33"
* @param showHexDenotation If true, will append "0x" to the front of the string.
* @return String representation of the number as a string starting with "0x"
*/
public function getNumberAsHexString(number:uint, minimumLength:uint = 1, showHexDenotation:Boolean = true):String {
// The string that will be output at the end of the function.
var string:String = number.toString(16).toUpperCase();

// While the minimumLength argument is higher than the length of the string, add a leading zero.
while (minimumLength > string.length) {
string = "0" + string;
}

// Return the result with a "0x" in front of the result.
if (showHexDenotation) { string = "0x" + string; }

return string;
}
}

Change log

r30 by mimshwright on Oct 27, 2009   Diff
Replaced @use with @example in ASDocs and
made sure all examples were rendered in
the documentation.
Go to: 
Project members, sign in to write a code review

Older revisions

r20 by mimshwright on Aug 16, 2009   Diff
Added tests for number as hex string.
also, slightly refactored that
function.
r13 by mimshwright on Jan 29, 2009   Diff
Finalized getNumberAsHexString
r12 by mimshwright on Jan 24, 2009   Diff
tweaks to color and number as hex
All revisions of this file

File info

Size: 1568 bytes, 40 lines
Powered by Google Project Hosting