Exploring ColdFusion InputBaseN() And FormatBaseN() Functions

Posted August 8, 2007 at 2:33 PM by Ben Nadel

Tags: ColdFusion

As part of my ColdFusion calendar system, I got into fooling around with some colors. I didn't end up going in that particular direction, but it got me messing around with both the ColdFusion functions, InputBaseN() and FormatBaseN(), neither of which I had ever used before. They do very simple but very powerful calculations for you.

InputBaseN( strValue, intRadix ) - This takes a numeric value and the radix that defines the numeric value and converts it to our standard base 10 format.

FormatBaseN( strValue, intRadix ) - This takes a base 10 value (what we use all the time) and converts it to a number with the given radix.

Using the ColdFusion InputBaseN() function, we can easily do things like convert HEX (hexadecimal) color values to RGB (red-green-blue) decimal values:

  • <!--- Start out with a base HEX web color. --->
  • <cfset strHEX = "FFCC00" />
  •  
  • <!---
  • Get the red, green, and blue parts (each two
  • characters of the given HEX value).
  • --->
  • <cfset strRed = Mid( strHEX, 1, 2 ) />
  • <cfset strGreen = Mid( strHEX, 3, 2 ) />
  • <cfset strBlue = Mid( strHEX, 5, 2 ) />
  •  
  • <!---
  • Convert the HEX colors to RGB decimal colors
  • where 0x00 = 00 and 0xFF = 255. HEX is base
  • 16 and we want to convert to base 10 (our
  • standard number system).
  • --->
  • Red : #strRed# : #InputBaseN( strRed, 16 )#<br />
  • Green : #strGreen# : #InputBaseN( strGreen, 16 )#<br />
  • Blue : #strBlue# : #InputBaseN( strBlue, 16 )#<br />

Running the above code, we get the following output:

Red : FF : 255
Green : CC : 204
Blue : 00 : 0

We can also easily convert bit/binary values to decimal:

  • <!--- Loop over BIT values. --->
  • <cfloop
  • index="strBits"
  • list="0:1:10:11:100:110:111:1000:1100:1110:1111"
  • delimiters=":">
  •  
  • <!---
  • Convert the bits to integers. Bits are in
  • base 2 and we are converting to base 10 (our
  • standard number system).
  • --->
  • #strBits# : #InputBaseN( strBits, 2 )#<br />
  •  
  • </cfloop>

Running the above code, we get the following output:

0 : 0
1 : 1
10 : 2
11 : 3
100 : 4
110 : 6
111 : 7
1000 : 8
1100 : 12
1110 : 14
1111 : 15

Pretty easy stuff! And, as easy as that is, ColdFusion's FormatBaseN() function makes it just as easy to go the other way, such as converting RGB values to HEX values. The only thing that complicates this conversion is that we need a 6-digit HEX value (at least if you are cool, you do), and there is nothing about FormatBaseN() that enforces this. So, in addition to actually changing the base radix, we also need to check the value lengths:

  • <!--- Set up our basic Red/Green/Blue color values. --->
  • <cfset intRed = 255 />
  • <cfset intGreen = 14 />
  • <cfset intBlue = 128 />
  •  
  • <!---
  • Now, we can create HEX numbers using RGB values. When
  • creating the colors, things are little more complicated
  • because we need a 6 digit value, but simply converting
  • base 10 to hex might not give us two digit values for
  • each color.
  • --->
  • <cfset strRed = FormatBaseN( intRed, 16 ) />
  • <cfset strGreen = FormatBaseN( intGreen, 16 ) />
  • <cfset strBlue = FormatBaseN( intBlue, 16 ) />
  •  
  • <!--- Now, make sure they have two digits. --->
  • <cfif (Len( strRed ) EQ 1)>
  • <cfset strRed = ("0" & strRed) />
  • </cfif>
  • <cfif (Len( strGreen ) EQ 1)>
  • <cfset strGreen = ("0" & strGreen) />
  • </cfif>
  • <cfif (Len( strBlue ) EQ 1)>
  • <cfset strBlue = ("0" & strBlue) />
  • </cfif>
  •  
  • <!--- Combine the RGB HEX values to get the color HEX. --->
  • <cfset strHEX = UCase(
  • strRed &
  • strGreen &
  • strBlue
  • ) />
  •  
  • <!--- Output HEX value. --->
  • #strHEX#

Running the above code, we get the following output:

FF0E80

So anyway, I didn't end up using these yet, but it's great to know that this functionality exists. If it didn't, I would probably have to end up doing manual multiplication and division and stuff - certainly not something we should be wasting our times on.




Reader Comments

Aug 9, 2007 at 7:44 AM // reply »
1 Comments

Thank you for the code, I will see what it can bring to me


Aug 13, 2007 at 1:05 PM // reply »
4 Comments

Very nice! This will help me when converting colors in some server-side font and image manipulation tools I use that require decimal color values. I know a lot of hex color values by heart, but not many equivalent decimal values.


Aug 13, 2007 at 1:23 PM // reply »
10,640 Comments

Glad to help in some way :)


Nov 16, 2007 at 11:05 AM // reply »
7 Comments

Thank you for sharing.


Aug 28, 2008 at 3:50 PM // reply »
2 Comments

I am using InputBaseN to convert IP addresses to decimal (dotted-decimal to binary to decimal); it works for lower-numbered addresses (such as 10.0.0.0), but returns a negative number for higher-numbered addresses (such as 192.0.0.0). It seems its treating the variable like a signed int? Any ideas?


Aug 28, 2008 at 3:57 PM // reply »
10,640 Comments

@Tom,

How are you going from dotted-decimal to binary. I am not even sure what that would do?


Aug 28, 2008 at 4:28 PM // reply »
2 Comments

It's for an application that maps a specific user login to one or a range of network addresses. This permits assignment of a "default account" to workstations per the logical/physical network on which they reside (e.g. different campus locations). I convert to decimal to make the comparison to configured network address ranges mathematically simple. All dotted-decimal IP addresses are fundamentally decimal numbers; dotted decimal is a rather arbitrary notation, but it makes network classification, routing, subnet and supernet masking possible.

The process is convert each portion of the dotted-decimal IP address (from CGI variable) to binary, pad the binary to 8 chars where needed (the second, third and forth values need to be 8 chars), concatenate the result (creating the binary equivalent of the dotted-decimal) then convert to decimal.

When the first decimal is small e.g. 10.x.x.x (usually used for internal private networks), InputBaseN works fine - when the first decimal number is large e.g. 192.x.x.x (also used for internal private networks) or 169.x.x.x (Microsoft private assigned in absence of DHCP) the decimal value returned is negative.


Aug 28, 2008 at 4:38 PM // reply »
10,640 Comments

@Tom,

Ok, I see what you are trying to do. I would check with Michael Dinowitz over on www.blogoffusion.com. He often converts IP addresses to integers for database storage (I believe). I think he will be able to give you better advice than I can.


Mar 27, 2009 at 12:57 PM // reply »
7 Comments

Ben,

I have the same requirement as Tom where I would like to take a IP address from a Nigeria spammer 212.100.69.11 and convert it to binary so I can look it up in a database table I got from http://www.maxmind.com/app/geolitecountry

I could not locate anything on Michael Dinowitz site, i think there is something wrong with it.

Dave


Jan 14, 2010 at 4:02 PM // reply »
2 Comments

Dave, I know this is now 2010, but in case you or anyone else is still wondering, the MaxMind database you're talking about stores its IP address in decimal format to support faster lookups.

The way they do it is this:

Take an IP address - e.g. 1.2.3.4

The numerical representation for the IP is:

(1 * 256 * 256 * 256) + (2 * 256 * 256) + (3 * 256) + 4

This will give you a maximum of a 10-digit number.

This makes it easier to match an IP address against a range, just by using BETWEEN in SQL.


Jan 14, 2010 at 4:07 PM // reply »
7 Comments

Erich,

Thanks for the heads up, I am using the MaxMind database and it is working great - I love that they are giving it away for free.

I have an example page here:
http://www.pickrent.com/misc/geoip.cfm

Dave


Jan 16, 2010 at 4:34 PM // reply »
10,640 Comments

@Dave, @Erich,

I'm still not great on bit-wise manipulation; but, is the multiples of 256 basically a mathematical way to perform bit-shifting?


Jan 20, 2010 at 6:13 AM // reply »
3 Comments

@Ben: yes, however coldfusion bit shifting operate on signed integer, which is not good for this operation.

@Tom: Personnally i use the following to convert from dotted decimal to decimal:

<cfset ipdec = 0>
<cfloop list="#ip_source#" delimiters="." index="i">
<cfset ipdec=ipdec*256 + i>
</cfloop>


Jan 20, 2010 at 11:01 PM // reply »
10,640 Comments

@Silmaril,

Ok, cool, I see what you're doing now.


Jan 26, 2010 at 12:04 PM // reply »
3 Comments

I found this to be very interesting and quite useful for some IP stuff I need to work on, but forgive me for asking...

In the example by Silmaril, using 216.201.6.99 as an example IP, how do I convert 3637053027 backwards to the original IP?


Jan 26, 2010 at 12:17 PM // reply »
7 Comments

Frank, I would be glad to show the code I am using but I can't post it here in the comments.


Jan 26, 2010 at 1:05 PM // reply »
3 Comments

@Dave,

I have a few old isp e-mails that I can throw out that are generally safe if you want to share the code, or you're welcome to toss me a link to a "hidden" page with the code displayed if that's your choice. Either works.

Feel free to try phobos[at]bright[dot]net

I'd be appreciative!


Jan 26, 2010 at 2:21 PM // reply »
7 Comments

I just sent you an email with my code.


Jan 26, 2010 at 9:11 PM // reply »
10,640 Comments

@Frank, @Dave,

I know you guys already covered this, but I took this as an opportunity to practice my bit-manipulation skills (thinking in Bits is still shaky for me).

http://www.bennadel.com/blog/1830-Converting-IP-Addresses-To-And-From-Integer-Values-With-ColdFusion.htm

Thanks for the inspiration!


Jan 27, 2010 at 12:14 AM // reply »
7 Comments

I just copied the code I got off the maxmind site and made it my own, Ben you have taken it much farther than that it appears.


Jan 27, 2010 at 8:58 AM // reply »
3 Comments

Fantastic, and thanks to both of you!


Feb 3, 2010 at 7:38 PM // reply »
2 Comments

Dave & Frank, I'd also like to see what you've come up with. How about CC'ing me on the same?

erichc[at]eonflex[dot]com

Thanks!


May 6, 2010 at 6:27 AM // reply »
8 Comments

I'm using formatbaseN with radix 36 to convert Social Security Numbers of our employees into badge barcodes :) Pretty neat way to generate unique nice looking badges:)


May 6, 2010 at 9:56 PM // reply »
10,640 Comments

@Dmitry,

This sounds cool, but I am not sure I understand the mechanics. Does base36 help you create bar codes (I've never worked with bar codes before).


May 7, 2010 at 7:16 AM // reply »
8 Comments

Base 36 generates 6 characters-long string from 9 digit SSN. This way badge barcodes are way easier to remember :)
Anyway, this was the only time I ever used these functions but they seem to be really useful for hardcore developers.


May 7, 2010 at 9:26 PM // reply »
10,640 Comments

@Dmitry,

Yeah, these are some sweet functions. It's not all the time you need to convert bases, but when you do - these are great! Base conversion and bit-shifting are like two things you read in documentation and think, pfff, I'll never need that; then one day, blam! they rock your world :)



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »