CFLib.org – Common Function Library Project

floatToHex(floatValue[, usePadding])

Last updated June 22, 2010

author

Leigh

Version: 0 | Requires: CF6 | Library: MathLib

Description:
Converts a java float to a 32-bit hexadecimal representation (IEEE 754 floating-point number)

Return Values:
Returns a string.

Example:

<cfset hexValue = FloatToHex( "9.937777E-32" ) />
<cfoutput>hexValue = #hexValue#</cfoutput>

Parameters:

Name Description Required
floatValue Numeric value. Yes
usePadding Boolean to determine if padding is used on the result. Defaults to true. No

Full UDF Source:

<!---
 Converts a java float to a 32-bit hexadecimal representation
 
 @param floatValue      Numeric value. (Required)
 @param usePadding      Boolean to determine if padding is used on the result. Defaults to true. (Optional)
 @return Returns a string. 
 @author Leigh (cfsearching@yahoo.com) 
 @version 0, June 22, 2010 
--->
<cffunction name="floatToHex" returntype="string" access="public" output="false"
        hint="Converts a java float value to a 32-bit hexadecimal representation (IEEE 754 floating-point number)">
    <cfargument name="floatValue" type="numeric" required="true" />
    <cfargument name="usePadding" type="boolean" default="true" />

    <cfset var floatRef = javacast("float", 0) />
    <cfset var intValue = floatRef.floatToIntBits( javacast("float", arguments.floatValue) ) />
    <cfset var result   = formatBaseN(intValue, 16) />

    <!--- pad hex value with leading zeroes --->
    <cfif arguments.usePadding>
        <cfset result =  right("00000000"& result, 8) />
    </cfif>
    
    <cfreturn result />
</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson