CFLib.org – Common Function Library Project

GuidToString(guidByteArray)

Last updated September 06, 2002

author

Samuel Neff

Version: 1 | Requires: CF6 | Library: DataManipulationLib

Description:
Useful when returning GUIDs (16-byte globally unique identifier, aka Replication ID's) from a database in CFMX. CF5 returned the GUIDs as a string when retrieved from a database and as such could easily be copied into SQL for another database call. However, CFMX returns byte arrays and they need to be converted before they can be inserted into SQL.

Return Values:
Returns a string.

Example:

<!--- Non Working Example
<cfquery name="source" datasource="#ds#">
   SELECT ID
   FROM RepID_Source
</cfquery>

<cfoutput>#guidToString(source.ID)#</cfoutput>
--->

Parameters:

Name Description Required
guidByteArray GUID Byte array returned from a query. Yes

Full UDF Source:

/**
 * Accepts a numeric GUID stored in a Byte Array and converts it to a string in the normal convention.
 * 
 * @param guidByteArray      GUID Byte array returned from a query. (Required)
 * @return Returns a string. 
 * @author Samuel Neff (sam@blinex.com) 
 * @version 1, September 6, 2002 
 */
function guidToString(guidByteArray) {
   var hexString='';
   
   if (IsArray(guidByteArray) AND ArrayLen(guidByteArray) GTE 16) {
     hexString=hexString & guidByteToHex(guidByteArray[4]);
     hexString=hexString & guidByteToHex(guidByteArray[3]);
     hexString=hexString & guidByteToHex(guidByteArray[2]);
     hexString=hexString & guidByteToHex(guidByteArray[1]);
     hexString=hexString & "-";
     hexString=hexString & guidByteToHex(guidByteArray[6]);
     hexString=hexString & guidByteToHex(guidByteArray[5]);
     hexString=hexString & "-";
     hexString=hexString & guidByteToHex(guidByteArray[8]);
     hexString=hexString & guidByteToHex(guidByteArray[7]);
     hexString=hexString & "-";
     hexString=hexString & guidByteToHex(guidByteArray[9]);
     hexString=hexString & guidByteToHex(guidByteArray[10]);
     hexString=hexString & "-";
     hexString=hexString & guidByteToHex(guidByteArray[11]);
     hexString=hexString & guidByteToHex(guidByteArray[12]);
     hexString=hexString & guidByteToHex(guidByteArray[13]);
     hexString=hexString & guidByteToHex(guidByteArray[14]);
     hexString=hexString & guidByteToHex(guidByteArray[15]);
     hexString=hexString & guidByteToHex(guidByteArray[16]);
   }
   
   return hexString;
}

function guidByteToHex(guidByte) {
   // Accepts a single byte and converts it to a two digit Hex number.
   
   var hexByte=Ucase(Right(FormatBaseN(guidByte, 16),2));
   if (Len(hexByte) IS 0) {
      hexByte='00';
   } else if (Len(hexByte) IS 1) {
      hexByte='0' & hexByte;
   }
   
   return hexByte;
}

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