CFLib.org – Common Function Library Project

Bin2Hex(bin)

Last updated June 28, 2002
Download UDF

author

Axel Glorieux                                     Axel Glorieux

Version: 1 | Requires: ColdFusion 5 | Library: MathLib

Description:
Converts binary data (eg. obtained from cffile action=readbinary) to hexadecimal format (base16).

Return Values:
Returns a string. If an error occurs, returns

Example:

<cfset dafile = expandPath("images\back_topbar.gif")>
<cffile action="readbinary" file="#dafile#" variable="test">

<!--- Display portion of result... --->
<cfoutput>
#left(bin2hex(test),80)#
</cfoutput>

Parameters:

Name Description Required
bin Binary data. Yes

Full UDF Source:

<cfscript>
/**
* Converts binary data to hexadecimal format.
*
* @param bin      Binary data. (Required)
* @return Returns a string. If an error occurs, returns
* @author Axel Glorieux (axel@misterbuzz.com)
* @version 1, June 28, 2002
*/

function Bin2Hex (bin){
var str64 = tobase64(bin);
var result = "";
var a = 0;
var b = 0;
var c = "";
var x = "";
var i = 1;
if (isbinary(bin)) {
for (i;i LTE len(str64);i=i+1){
c = asc(mid(str64,i,1));
if (c LT 47)x = 62;
else if (c LT 48)x = 63;
else if (c EQ 61)x = 0;
else if (c LT 65)x = c+4;
else if (c LT 97)x = c-65;
else x = c-71;
if (i mod 2){
a = bitshln(x,6);
}
else {
b = x;
for (j=len(formatbasen(a+b,16));j LT 3;j=j+1){
result = result & 0;
}
result = result & formatbasen(a+b,16);
a = 0;
b = 0;
}
}
if (right(str64,2) IS "=="){
result = left(result,len(result)-4);
}
else if (right(str64,1) IS "="){
result = left(result,len(result)-2);
}
return result;
}
else {
result = "Invalid binary data";
}
}
</cfscript>

Search CFLib.org


Latest Additions

Raymond Compton Raymond Compton added
structBlend
20 day(s) ago

Duncan Duncan added
IsZIPUK
20 day(s) ago

Todd Sharp Todd Sharp added
getTagContentAll
26 day(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Created by Raymond Camden / Design by Justin Johnson