编码系列--Base24编码
本文章已收录于:
分类:
作者同类文章
X
版权声明:本文为博主原创文章,未经博主允许不得转载。
0、写在前面
Windows和Office的序列号就是在最终换算之后,通过Base24编码转换成为可显示字符串的。写这个系列的目的就是做类似的东东。
1、编码原理
在Base32的那篇文章中已经比较详细的说明了,这里只指出与原理相比较,Base24是一个非常奇怪的方案,因为用4个bits作为分码段,只能用到16个,另外8个编码字符用不到,而采用5个bits作为分码段,又少8个字符。这就是之前写了个Base32的原因。
后来想明白了这个问题,还是4个bits一组,然后在通过另外一种方式把24个字符都用上不就行了,我采用的是比较愚蠢的方式,按照位置取8的余数,再加上编码值换算即可。总之样子上已经没啥区别了。
先申明,我并不知道MS采用的Base24的具体的方式是什么,因为和我描述的不同,因为据说它是将114bit编码成为200个bits(25个Bytes)
,我说的方法显然做不到。目前就到这个地步吧,有空再想想。
2、源代码
只把Encoding和Decoding的两个函数贴出来,其他的都雷同。
public new static String Encode(Byte[] abData)
{
Int32 dwLoop = 0,dwCharIndex = 0,dwCharCount = 0,dwRem = 0;
Char[] acPart = null;
StringBuilder sbOutput = null;
{
Int32 dwLoop = 0,dwCharIndex = 0,dwCharCount = 0,dwRem = 0;
Char[] acPart = null;
StringBuilder sbOutput = null;
if (abData == null || m_acBaseMap == null || m_acBaseMap.Length < m_dwMapLength)
return null;
return null;
try
{
dwCharCount = abData.Length * 2;
sbOutput = new StringBuilder(dwCharCount);
acPart = new Char[2];
}
catch (Exception e)
{
Trace.WriteLine("CLsBase24.Encode: Initialize buffer failed! " + e.Message);
}
if (acPart == null || sbOutput == null)
return null;
{
dwCharCount = abData.Length * 2;
sbOutput = new StringBuilder(dwCharCount);
acPart = new Char[2];
}
catch (Exception e)
{
Trace.WriteLine("CLsBase24.Encode: Initialize buffer failed! " + e.Message);
}
if (acPart == null || sbOutput == null)
return null;
for(dwLoop = 0;dwLoop < abData.Length;dwLoop++)
{
Array.Clear(acPart,0,acPart.Length);
// ONE byte will split to TWO characters
Math.DivRem(dwLoop,9,out dwRem);
dwCharIndex = (abData[dwLoop] >> 4) + dwRem;
acPart[0] = m_acBaseMap[dwCharIndex];
dwCharIndex = (abData[dwLoop] & 0x0F) + dwRem;
acPart[1] = m_acBaseMap[dwCharIndex];
sbOutput.Append(acPart,0,acPart.Length);
}
{
Array.Clear(acPart,0,acPart.Length);
// ONE byte will split to TWO characters
Math.DivRem(dwLoop,9,out dwRem);
dwCharIndex = (abData[dwLoop] >> 4) + dwRem;
acPart[0] = m_acBaseMap[dwCharIndex];
dwCharIndex = (abData[dwLoop] & 0x0F) + dwRem;
acPart[1] = m_acBaseMap[dwCharIndex];
sbOutput.Append(acPart,0,acPart.Length);
}
return sbOutput.ToString();
}
}
public new static Byte[] Decode(String sData)
{
Int32 dwLoop = 0,dwLength = 0,dwRem = 0;
Int32[] dwCharIndex = null;
Byte[] abOutput = null;
Char[] acInput = null;
{
Int32 dwLoop = 0,dwLength = 0,dwRem = 0;
Int32[] dwCharIndex = null;
Byte[] abOutput = null;
Char[] acInput = null;
if (sData == null || sData == String.Empty)
return null;
return null;
acInput = sData.ToCharArray();
if (acInput == null)
return null;
if (acInput == null)
return null;
try
{
dwLength = acInput.Length / 2;
abOutput = new Byte[dwLength];
dwCharIndex = new Int32[2];
}
catch (Exception e)
{
Trace.WriteLine("CLsBase24.Decode: Initialize buffer failed! " + e.Message);
}
if (acInput == null)
return null;
{
dwLength = acInput.Length / 2;
abOutput = new Byte[dwLength];
dwCharIndex = new Int32[2];
}
catch (Exception e)
{
Trace.WriteLine("CLsBase24.Decode: Initialize buffer failed! " + e.Message);
}
if (acInput == null)
return null;
dwLength = 0;
for (dwLoop = 0;dwLoop < acInput.Length;dwLoop += 2)
{
Array.Clear(dwCharIndex,0,dwCharIndex.Length);
// TWO character can merage ONE byte
Math.DivRem(dwLoop / 2,9,out dwRem);
switch (acInput.Length - dwLoop)
{
case 1:
dwCharIndex[0] = GetCharIndex(acInput[dwLoop]) - dwRem;
abOutput[dwLength] = (Byte) (dwCharIndex[0] << 4);
break;
for (dwLoop = 0;dwLoop < acInput.Length;dwLoop += 2)
{
Array.Clear(dwCharIndex,0,dwCharIndex.Length);
// TWO character can merage ONE byte
Math.DivRem(dwLoop / 2,9,out dwRem);
switch (acInput.Length - dwLoop)
{
case 1:
dwCharIndex[0] = GetCharIndex(acInput[dwLoop]) - dwRem;
abOutput[dwLength] = (Byte) (dwCharIndex[0] << 4);
break;
default:
dwCharIndex[0] = GetCharIndex(acInput[dwLoop]) - dwRem;
dwCharIndex[1] = GetCharIndex(acInput[dwLoop + 1]) - dwRem;
abOutput[dwLength] = (Byte) ((dwCharIndex[0] << 4) + dwCharIndex[1]);
break;
dwCharIndex[0] = GetCharIndex(acInput[dwLoop]) - dwRem;
dwCharIndex[1] = GetCharIndex(acInput[dwLoop + 1]) - dwRem;
abOutput[dwLength] = (Byte) ((dwCharIndex[0] << 4) + dwCharIndex[1]);
break;
}
dwLength++;
}
dwLength++;
}
return abOutput;
}
}
-
顶
- 0
-
踩
- 0
我的同类文章
http://blog.csdn.net
- •谈谈Javascript中的异步调用的上下文2016-01-26
- •CSharp Tips:Delegate成员变量和Event的区别2010-11-19
- •CSharp Tips:调用Win32 API如何处理指针类型的参数(补)2009-06-27
- •编码系列--Base32编码的完整示例2007-10-02
- •CSharp Tips:模拟MSN Message发送文字效果的实现2007-06-15
- •DotNET 1.1迁移至DotNET 2.0的注意事项2006-12-30
- •谈谈jQuery和跨域访问2014-11-25
- •CSharp Tips:Drag & Drop的高级应用2010-11-18
- •CSharp Tips:使用OleDb操纵Excel文件(1)2008-04-05
- •CSharp Tips:以整数的形式传递对象引用2007-08-01
- •CSharp Tips:调用Win32 API如何处理指针类型的参数2007-04-27
参考知识库
- Office 365 用户管理
- 利用Office 365 管理合同文档,实现定期提醒
- windows命令行教程
- windows批处理教程
-
Windows Server 2012 Hyper-v 管理
- ARM汇编学习笔记
- ARM汇编学习笔记
- ARM汇编学习笔记
- 各种音视频编解码学习详解 h264 mpeg4 aac 等所有音视频格式
- 使用 WSDL 部署 Web 服务
更多资料请参考:
猜你在找
width="728" height="90" align="center,center" id="iframeu1607657_0" src="http://pos.baidu.com/mclm?rdid=1607657&dc=2&di=u1607657&dri=0&dis=0&dai=1&ps=3586x442&dcb=BAIDU_SSP_define&dtm=HTML_POST&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1473673120393&ti=%E7%BC%96%E7%A0%81%E7%B3%BB%E5%88%97--Base24%E7%BC%96%E7%A0%81%20-%20%E7%96%BE%E9%A3%8E%E4%B9%8B%E7%8B%BC%20-%20%E5%8D%9A%E5%AE%A2%E9%A2%91%E9%81%93%20-%20CSDN.NET&ari=2&dbv=0&drs=1&pcs=1423x675&pss=1423x3605&cfv=20&cpl=2&chi=1&cce=true&cec=utf-8&tlm=1473673120&rw=675<u=http%3A%2F%2Fblog.csdn.net%2Fmittermeyer%2Farticle%2Fdetails%2F114124<r=http%3A%2F%2Fblog.csdn.net%2Fmittermeyer%2Farticle%2Fdetails%2F96950&ecd=1&psr=1440x900&par=1440x776&pis=-1x-1&ccd=24&cja=true&cmi=4&col=zh-CN&cdo=-1&tcn=1473673120&qn=9b24a8228b0bb43f&tt=1473673120367.34.35.35" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" vspace="0" hspace="0" style="margin: 0px; border: 0px currentColor; border-image: none; vertical-align: bottom;" allowtransparency="true">
10楼 Mittermeyer 2006-08-24 11:42发表 [回复] [引用] [举报]-
-
好久没来照料Blog,前面一些要代码的回复都没有看到。只能在这里表示一下迟到的歉意了。 <br>
9楼 yj_yulin 2005-02-05 17:12发表 [回复] [引用] [举报]-
-
不知道base24是干什么的,我只知道有base16,base32,base64< br>定义在rfc 3548里面 <br>http://www.faqs.org/rfcs/rfc3548.html