Last active
April 20, 2022 17:34
-
-
Save superllama/df8473fb87b5ed14b4227d06c5263713 to your computer and use it in GitHub Desktop.
Some XSLT templates that can convert Hex to Base64, for some reason.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
<xsl:output method="xml" encoding="ascii" indent="yes" omit-xml-declaration="yes"/> | |
<xsl:template match="Hex"> | |
<xsl:call-template name="bin2b64"> | |
<xsl:with-param name="x"> | |
<xsl:call-template name="hex2bin"> | |
<xsl:with-param name="x" select="text()"/> | |
</xsl:call-template> | |
</xsl:with-param> | |
</xsl:call-template> | |
</xsl:template> | |
<xsl:template name="bin2b64"> | |
<xsl:param name="x" select="."/> | |
<xsl:variable name="b64" select="substring($x,1,1)*32+substring($x,2,1)*16+substring($x,3,1)*8+substring($x,4,1)*4+substring($x,5,1)*2+substring($x,6,1)"/> | |
<xsl:value-of select="substring('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',$b64+1,1)"/> | |
<xsl:choose> | |
<xsl:when test="string-length($x) > 6"> | |
<xsl:call-template name="bin2b64"> | |
<xsl:with-param name="x" select="substring($x,7)"/> | |
</xsl:call-template> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of select="substring('==',1,(6-string-length($x)) div 2)"/> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:template> | |
<xsl:template name="hex2bin"> | |
<Binary> | |
<xsl:call-template name="hex2bin_text"> | |
<xsl:with-param name="x" select="text()"/> | |
</xsl:call-template> | |
</Binary> | |
</xsl:template> | |
<xsl:template name="hex2bin_text"> | |
<xsl:param name="x" select="."/> | |
<xsl:variable name="hex" select="substring($x,1,1)"/> | |
<xsl:variable name="dec" select="string-length(substring-before('0123456789ABCDEF',translate($hex,'abcdef','ABCDEF')))"/> | |
<xsl:value-of select="floor($dec div 8) mod 2"/> | |
<xsl:value-of select="floor($dec div 4) mod 2"/> | |
<xsl:value-of select="floor($dec div 2) mod 2"/> | |
<xsl:value-of select="$dec mod 2"/> | |
<xsl:if test="string-length($x) > 1"> | |
<xsl:call-template name="hex2bin_text"> | |
<xsl:with-param name="x" select="substring($x,2)"/> | |
</xsl:call-template> | |
</xsl:if> | |
</xsl:template> | |
</xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment