VB.Net实现7Bit编码与解码
来源:互联网 发布:软件卸载大师 编辑:程序博客网 时间:2024/05/17 07:32
7Bit编码主要用于对单字节字符进行编码,因为单字节字符的ASCII码在00~7F之间,最高位始终为0实际有效的只有7个Bit,因此可以通过编码的方式将无效的最高位利用起来从而达到使用7个字节存储8个单字节字符的目的。
7Bit编码的基本规则简单来说就是去掉最高位的0以后,将后一字节的低位挪到前一字节最高有效位的前面使前一字节凑够8bit,最后一个字节bit数不够8时从高位开始补0
举例说明,如果对“abc”进行7Bit编码,abc的ASCII码为0x61、0x62、0x63,其bit顺序如下:
bit7
bit6
bit5
bit4
bit3
bit2
bit1
bit0
a
0
1
1
0
0
0
0
1
b
0
1
1
0
0
0
1
0
c
0
1
1
0
0
0
1
1
编码步骤如下:
1、 将b的bit0移入a的bit7,再将b右移一位,得到如下结果
bit7
bit6
bit5
bit4
bit3
bit2
bit1
bit0
a
0
1
1
0
0
0
0
1
b
-
0
1
1
0
0
0
1
c
0
1
1
0
0
0
1
1
2、 将c的bit1移入b的bit7,c的bit0移入b的bit6,再将c右移两位,得到如下结果
bit7
bit6
bit5
bit4
bit3
bit2
bit1
bit0
a
0
1
1
0
0
0
0
1
b
1
1
1
1
0
0
0
1
c
-
-
0
1
1
0
0
0
3、 c剩余6bit不足8bit,在bit7、bit6补0,得到如下结果
bit7
bit6
bit5
bit4
bit3
bit2
bit1
bit0
a
0
1
1
0
0
0
0
1
b
1
1
1
1
0
0
0
1
c
0
0
0
1
1
0
0
0
abc原ASCII码为61 62 63 ,对应的7bit编码为:61 F1 18
VB.Net实现7Bit编解码的函数如下:
'7Bit编码 Private Function Encoder_7Bit(ByVal s As String) As Byte() Dim arrAsc() As Byte arrAsc = System.Text.Encoding.ASCII.GetBytes(s) Dim arr7Bit() As Byte '申请存储7Bit编码需要空间 If (arrAsc.Length * 7) Mod 8 = 0 Then ReDim arr7Bit((arrAsc.Length * 7) \ 8 - 1) Else ReDim arr7Bit((arrAsc.Length * 7) \ 8) End If Dim lBit As Integer '低位 Dim hBit As Integer '高位 Dim yy As Integer = 0 Dim zz As Integer = 0 For xx As Integer = 1 To arrAsc.Length - 1 yy = xx Mod 8 If yy <> 0 Then lBit = arrAsc(xx) Mod System.Math.Pow(2, yy) hBit = arrAsc(xx) \ System.Math.Pow(2, yy) arrAsc(xx) = hBit arr7Bit(zz) = lBit * System.Math.Pow(2, 8 - yy) + arrAsc(xx - 1) zz = zz + 1 End If Next If zz <> arr7Bit.Length Then arr7Bit(arr7Bit.Length - 1) = arrAsc(arrAsc.Length - 1) End If Encoder_7Bit = arr7Bit Erase arrAsc Erase arr7Bit End Function '7Bit解码 Private Function Decoder_7Bit(ByVal s As Byte()) As String Dim arrAsc() As Byte '申请解码后存储ASCII字符需要的空间 ReDim arrAsc((s.Length * 8) \ 7 - 1) Dim lBit As Integer '低位 Dim hBit As Integer '高位 Dim yy As Integer = 0 Dim zz As Integer = 0 For xx As Integer = 0 To arrAsc.Length - 1 yy = xx Mod 8 If yy <> 7 Then lBit = s(zz) Mod System.Math.Pow(2, 8 - yy - 1) arrAsc(xx) = lBit * System.Math.Pow(2, yy) + hBit hBit = s(zz) \ System.Math.Pow(2, 8 - yy - 1) zz = zz + 1 Else arrAsc(xx) = hBit hBit = 0 End If Next Decoder_7Bit = System.Text.Encoding.ASCII.GetString(arrAsc) Erase arrAsc End Function
- VB.Net实现7Bit编码与解码
- VB.Net实现PDU中的UCS2编码和解码
- .net C#实现Base64编码与解码
- GSM PDU 7bit短信编码与解码
- VB PDU mode 7 bit 解码
- VB PDU mode 7 bit 解码
- vb.net Encoding类 编码解码
- VB里Base64编码与解码算法
- VB UTF-8编码与解码
- GSM PDU 7bit编码解码函数
- 7bit编码解码---未测试
- ASP.NET编码与解码
- Base64编码解码与实现
- Base64编码解码与实现
- Base64编码与解码实现
- JS与C#(.net)编码与解码
- JScript.Encode编码的vb.net解码代码。
- .NET url 的编码与解码
- 循环赛日程表
- 对android应用进行单元测试
- stm32
- Qt - 导航栏的实现
- JVM调优总结一概念
- VB.Net实现7Bit编码与解码
- Activity传递数据的几种方式及返回结果
- iOS中 UIButton和UIImageView
- 深入Mysql字符集设置 图文版
- Zookeeper伪分布安装
- qmake 命令行参数
- No.2 (code 2.0)UIButton
- linux程序设计第2章末程序1
- poj 3260 The Fewest Coins