Next Page

1

Previous Page

Thread: Base64

Created on: 09/04/08 12:03 PM

Replies: 2

jbliss


New Member


Joined: 09/04/08

Posts: 3

Base64
09/04/08 12:03 PM

So, Ray helped me solve this via email and I thought I'd post the solution here for the good of the order:

The point of this exercise is that I have a table with a primary key that's a standard Microsoft GUID (36 hex characters long). This is fine but there's one remote "place" I need to store references to this primary key (via webservice) that'll only accept a max of 25 characters. So by base64'ing the GUID, I'll be able to use Microsoft's GUID and not break the webservice.

Problem is: CF's ToBase64 chokes on the decimal representation of a GUID (it's too big). So, luckily, I found this http://www.fusebox.org/forums/messageview.cfm?catid=21&threadid=5075 and used it to create this:

view plain print about
1
2<cffunction access="public" name="HexToBase64" output="yes" returntype="string">
3    <cfargument name="Hex" type="string" required="yes">
4    <cfset outStream = CreateObject("java", "java.io.ByteArrayOutputStream").init()>
5 <cfset inputLength = Len(Hex)>
6 <cfset outputString = "">
7 <cfset i = 0>
8 <cfset ch = "">
9 <cfif inputLength mod 2 neq 0>
10 <cfset Hex = "0" & Hex>
11 </cfif>
12 <cfloop from="1" to="#inputLength#" index="i" step="2">
13 <cfset ch = Mid(Hex, i, 2)>
14 <cfset outStream.write(javacast("int", InputBaseN(ch, 16)))>
15 </cfloop>
16 <cfset outStream.flush()>
17 <cfset outStream.close()>
18 <cfreturn ToBase64(outStream.toByteArray())>
19</cffunction>

Sweet. Then Ray helped me noodle the return voyage. (Unfortunately, CF does not have "FromBase64" or equivalent.)

view plain print about
1
2<cffunction access="public" name="Base64ToHex" output="yes" returntype="string">
3    <cfargument name="Base64" type="string" required="yes">
4    <cfset aBinary = ToBinary(Base64)>
5 <cfset Hex = "">
6 <cfloop index="i" from="1" to="#ArrayLen(aBinary)#">
7 <cfset Hex = Hex & FormatBaseN(BitAnd(aBinary[i], 255), 16)>
8 </cfloop>
9 <cfreturn Hex>
10</cffunction>

Hope this saves someone some time someday.

Link | Top | Bottom

jbliss


New Member


Joined: 09/04/08

Posts: 3

RE: Base64
09/04/08 12:19 PM

Addendum: Microsoft GUID likes this format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx so, near the top of HexToBase64, I added:

view plain print about
1
2<cfset Hex = Replace(Hex, "-", "", "ALL")>

...and near the bottom of Base64ToHex, I added:

view plain print about
1
2<cfset Hex = Insert("-", Hex, 21)>
3<cfset Hex = Insert("-", Hex, 17)>
4<cfset Hex = Insert("-", Hex, 13)>
5<cfset Hex = Insert("-", Hex, 9)>

Link | Top | Bottom

jbliss


New Member


Joined: 09/04/08

Posts: 3

RE: Base64
04/27/11 11:59 AM

Update fixes an issue or two:

view plain print about
1<cffunction access="public" name="HexToBase64" output="yes" returntype="string">
2    <cfargument name="Hex" type="string" required="yes">
3    <cfset Hex = Replace(Hex, "-", "", "ALL")>
4    <cfset outStream = CreateObject("java", "java.io.ByteArrayOutputStream").init()>
5    <cfset inputLength = Len(Hex)>
6    <cfset outputString = "">
7    <cfset i = 0>
8    <cfset ch = "">
9    <cfif inputLength mod 2 neq 0>
10        <cfset Hex = "0" & Hex>
11    </cfif>
12    <cfloop from="1" to="#inputLength#" index="i" step="2">
13        <cfset ch = Mid(Hex, i, 2)>
14        <cfset outStream.write(javacast("int", InputBaseN(ch, 16)))>
15    </cfloop>
16    <cfset outStream.flush()>
17    <cfset outStream.close()>
18    <cfreturn ToBase64(outStream.toByteArray())>
19</cffunction>
20
21<cffunction access="public" name="Base64ToHex" output="yes" returntype="string">
22    <cfargument name="Base64" type="string" required="yes">
23    <cfset aBinary = ToBinary(Base64)>
24    <cfset Hex = "">
25    <cfloop index="i" from="1" to="#ArrayLen(aBinary)#">
26        <cfset Hex = Hex & Replace(RJustify(FormatBaseN(BitAnd(aBinary[i], 255), 16), 2), " ", "0", "ALL")>
27    </cfloop>
28    <cfset Hex = Insert("-", Hex, 20)>
29    <cfset Hex = Insert("-", Hex, 16)>
30    <cfset Hex = Insert("-", Hex, 12)>
31    <cfset Hex = Insert("-", Hex, 8)>
32    <cfreturn Hex>
33</cffunction>

Link | Top | Bottom

Next Page

1

Previous Page

New Post

Please login to post a response.