编码系列--Base24编码

348 篇文章 0 订阅
75 篇文章 5 订阅

编码系列--Base24编码                    

标签: nullbyteexceptioncharacterbufferstring
3107人阅读 评论(11) 收藏 举报
本文章已收录于:


分类:


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;
 
   if (abData == null || m_acBaseMap == null || m_acBaseMap.Length < m_dwMapLength)
    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;
 
   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);
   }
 
   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;
 
   if (sData == null || sData == String.Empty)
    return null;
 
   acInput = sData.ToCharArray();
   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 = 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;
 
     default:
      dwCharIndex[0] = GetCharIndex(acInput[dwLoop]) - dwRem;
      dwCharIndex[1] = GetCharIndex(acInput[dwLoop + 1]) - dwRem;
      abOutput[dwLength] = (Byte) ((dwCharIndex[0] << 4) + dwCharIndex[1]);
      break;
 
    }
    dwLength++;
   }
 
   return abOutput;
  }
 



0
0
 
 


我的同类文章



参考知识库

更多资料请参考:
猜你在找
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&ltu=http%3A%2F%2Fblog.csdn.net%2Fmittermeyer%2Farticle%2Fdetails%2F114124&ltr=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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值