Welcome!

Adobe Flex Authors: Liz McMillan, RealWire News Distribution, Maureen O'Gara, Yakov Fain, Keith Swenson

Related Topics: Adobe Flex, ColdFusion

Adobe Flex: Article

Converting Back and Forth Between Hexidecimal Strings and Numbers

It's really convenient for anyone coming from an HTML background that we can now provide colors to Flex

Jeff Tapper's Blog

It's really convenient for anyone coming from an HTML background that we can now provide colors to Flex using html standard syntax for hexidecimal number (#000000 = black, #ffffff= white, etc).  I've recently been building some tools to convert drawings in the flash player to and from SVG format. SVG, like HTML, likes to specify hexidecimal numbers with a "#" prefix, but the flash player drawing API needs hexidecimal numbers to be prefixed with "0x"  To help migrate back and forth between hexStrings and AS numbers, I wrote a few static methods, which are working well for me:

package utils {
 public class Colors {
  public static function hexStringToNumber(hexStr:String):Number{
   if(hexStr.length != 7){
    return -1;
   }
   if(hexStr.charAt(0) != "#"){
    return -1;
   }
   var newStr:String = hexStr.substr(1,6);
   var numStr:String = "0x"+newStr;
   var num:Number = Number(numStr);
   return num;
  }
  public static function numToHexString(num:Number):String{
   var hexStr:String = num.toString(16);
   while(hexStr.length < 6){
    hexStr = "0"+hexStr;
   }
   hexStr = "#"+hexStr;
   return hexStr;
  }
 }
}

As you can see, there are 2 methods hexStringToNumber and numToHexString.  These can be used simply like this:

var whiteNum:Number = Colors.hexStringToNumber ("#ffffff");
var whiteStr:String = Colors.numToHexString(16777215);

More Stories By Jeff Tapper

Jeff Tapper, co-founder of Tapper, Nimer and Associates, is an Editorial Board member of Web Developer's & Designer's Journal. He has been developing Internet-based applications since 1995, for a myriad of clients including Toys R Us, IBM, Allaire, Dow Jones, American Express, M&T Bank, Verizon, Allied Office Supplies, and many others. As an Instructor, he is certified to teach all of Adobe's courses on Flex, ColdFusion and Flash development. He has worked as author and technical editor for several books on technologies including Flex, Flash and ColdFusion, such as "Object Oriented Programming with ActionScript 2.0", and "Flex 2 Training from the Source."

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Web Developer's & Designer's Journal 10/31/06 11:01:42 AM EST

It's really convenient for anyone coming from an HTML background that we can now provide colors to Flex using html standard syntax for hexidecimal number (#000000 = black, #ffffff= white, etc). I've recently been building some tools to convert drawings in the flash player to and from SVG format. SVG, like HTML, likes to specify hexidecimal numbers with a '#' prefix, but the flash player drawing API needs hexidecimal numbers to be prefixed with '0x' To help migrate back and forth between hexStrings and AS numbers, I wrote a few static methods, which are working well for me: