23 个解决方案
#1
将汉字的ASCII码存入integer变量中,用二进制文件的方式的PUT语句
将这个变量写入文件时,只需要2字节,而不是6字节。
如:
Dim intHanzi As Integer
intHanzi = Asc("汉")
Open "Test.Dat" For Binary As#1
Put #1,,intHanzi
Close #1
将这个变量写入文件时,只需要2字节,而不是6字节。
如:
Dim intHanzi As Integer
intHanzi = Asc("汉")
Open "Test.Dat" For Binary As#1
Put #1,,intHanzi
Close #1
#2
FreshAir(FreshAir):
我不是将汉字存入文件中,我所操作的汉字包含在大字符串中,字符串可能需要存入数据库中,另外我需要将字符串中的汉字提取出来,以字母或数字的形式表示汉字,因为我的程序中用到的一个字符串函数不能识别汉字,所以我需要以字母或数字的形式,以最少的字节数来表示汉字。
我不是将汉字存入文件中,我所操作的汉字包含在大字符串中,字符串可能需要存入数据库中,另外我需要将字符串中的汉字提取出来,以字母或数字的形式表示汉字,因为我的程序中用到的一个字符串函数不能识别汉字,所以我需要以字母或数字的形式,以最少的字节数来表示汉字。
#3
正如你说的要用字节表示汉字
以下是字符串转换字节的代码
Sub ChangeStrAryToByte(StrAry()
As String, ByteAry() As Byte)
'将字符串数组转换成字节数组
Dim LowBound, UpBound As Integer
Dim i, count, StartPos, MaxLen As Integer
Dim TmpByte() As Byte
LowBound = LBound(StrAry)
UpBound = UBound(StrAry)
count = 0
ReDim ByteAry(0)
For i = LowBound To UpBound
MaxLen = LenB(StrAry(i))
ReDim TmpByte(MaxLen + 1)
ReDim Preserve ByteAry(count + MaxLen + 1)
Call StrToByte(StrAry(i), TmpByte) '转换一个字符串
StartPos = count
Do
ByteAry(count) = TmpByte(count - StartPos)
count = count + 1
If ByteAry(count - 1) = 0 Then Exit Do
Loop '将每一个字符串对应
的字节数组按顺序填入结果数组中
ReDim Preserve ByteAry(count - 1)
Next i
End Sub
以下是字符串转换字节的代码
Sub ChangeStrAryToByte(StrAry()
As String, ByteAry() As Byte)
'将字符串数组转换成字节数组
Dim LowBound, UpBound As Integer
Dim i, count, StartPos, MaxLen As Integer
Dim TmpByte() As Byte
LowBound = LBound(StrAry)
UpBound = UBound(StrAry)
count = 0
ReDim ByteAry(0)
For i = LowBound To UpBound
MaxLen = LenB(StrAry(i))
ReDim TmpByte(MaxLen + 1)
ReDim Preserve ByteAry(count + MaxLen + 1)
Call StrToByte(StrAry(i), TmpByte) '转换一个字符串
StartPos = count
Do
ByteAry(count) = TmpByte(count - StartPos)
count = count + 1
If ByteAry(count - 1) = 0 Then Exit Do
Loop '将每一个字符串对应
的字节数组按顺序填入结果数组中
ReDim Preserve ByteAry(count - 1)
Next i
End Sub
#4
Function GetCharByte(ByVal OneChar As Integer, ByVal IsHighByte As Boolean) As Byte ' 该函数获得一个字符的高字节或低字节
If IsHighByte Then
If OneChar >= 0 Then
GetCharByte = CByte(OneChar \ 256)
'右移8位,得到高字节
Else
GetCharByte = CByte((OneChar
And &H7FFF) \ 256) Or &H80
End If
Exit Function
Else
GetCharByte = CByte(OneChar And &HFF)
'屏蔽掉高字节,得到低字节
Exit Function
End If
End Function
If IsHighByte Then
If OneChar >= 0 Then
GetCharByte = CByte(OneChar \ 256)
'右移8位,得到高字节
Else
GetCharByte = CByte((OneChar
And &H7FFF) \ 256) Or &H80
End If
Exit Function
Else
GetCharByte = CByte(OneChar And &HFF)
'屏蔽掉高字节,得到低字节
Exit Function
End If
End Function
#5
Sub StrToByte(StrToChange As String, ByteArray() As Byte)
'该函数将一个字符串转换成字节数组
Dim LowBound, UpBound As Integer
Dim i, count, length As Integer
Dim OneChar As Integer
count = 0
length = Len(StrToChange)
LowBound = LBound(ByteArray)
UpBound = UBound(ByteArray)
For i = LowBound To UpBound
ByteArray(i) = 0 '初始化字节数组
Next
For i = LowBound To UpBound
count = count + 1
If count <= length Then
OneChar = Asc(Mid(StrToChange, count, 1))
If (OneChar > 255) Or (OneChar < 0) Then
'该字符是非ASCII字符
ByteArray(i) = GetCharByte(OneChar, True) '得到高字节
i = i + 1
If i <= UpBound Then ByteArray(i)
= GetCharByte(OneChar, False)
'得到低字节
Else
'该字符是ASCII字符
ByteArray(i) = OneChar
End If
Else
Exit For
End If
Next
End Sub
'该函数将一个字符串转换成字节数组
Dim LowBound, UpBound As Integer
Dim i, count, length As Integer
Dim OneChar As Integer
count = 0
length = Len(StrToChange)
LowBound = LBound(ByteArray)
UpBound = UBound(ByteArray)
For i = LowBound To UpBound
ByteArray(i) = 0 '初始化字节数组
Next
For i = LowBound To UpBound
count = count + 1
If count <= length Then
OneChar = Asc(Mid(StrToChange, count, 1))
If (OneChar > 255) Or (OneChar < 0) Then
'该字符是非ASCII字符
ByteArray(i) = GetCharByte(OneChar, True) '得到高字节
i = i + 1
If i <= UpBound Then ByteArray(i)
= GetCharByte(OneChar, False)
'得到低字节
Else
'该字符是ASCII字符
ByteArray(i) = OneChar
End If
Else
Exit For
End If
Next
End Sub
#6
这样阿,我看看
#7
其实我是想达到如下的目的:
将包含有中文的字符串转化成条码,同时又可将条码转化成含有中文的字符串,Code39条码只能由A-Z,0-9,小数点和减号组成,例如我想将书名《SQLServer数据库大全》转化成条码,应该如何进行呢,如何才能使条码的长度最少?
将包含有中文的字符串转化成条码,同时又可将条码转化成含有中文的字符串,Code39条码只能由A-Z,0-9,小数点和减号组成,例如我想将书名《SQLServer数据库大全》转化成条码,应该如何进行呢,如何才能使条码的长度最少?
#8
先对书籍进行编目。然后用一一对应的方式去对应数据库中的真正的汉字书名。ISBN考虑了书籍重目的问题,不是将所有的信息都转化为汉字,而是将信息进行编码,然后由程序解码。其实就是数据库的问题。没什么麻烦的啊
#9
我是想实现自动编码,例如
《SQLServer数据库大全》可编码成如下:
SQLServer-13571-16675-16414-19213-14165 这样就可通过条码打印出来,
在以上编码中,我是用汉字的内码表示汉字的,
同时我又可编写一个函数将编码SQLServer-13571-16675-16414-19213-14165还原成《SQLServer数据库大全》,但以上编码太长了,我希望以上编码能够缩短一些。
《SQLServer数据库大全》可编码成如下:
SQLServer-13571-16675-16414-19213-14165 这样就可通过条码打印出来,
在以上编码中,我是用汉字的内码表示汉字的,
同时我又可编写一个函数将编码SQLServer-13571-16675-16414-19213-14165还原成《SQLServer数据库大全》,但以上编码太长了,我希望以上编码能够缩短一些。
#10
Hex(Asc("数"))
这样一个汉字用4个字母和数字来表示
这样一个汉字用4个字母和数字来表示
#11
压根就不用你自己编码,采用ISBN的编码就可以了
#12
使用Arcan(Arcan) 的方法(专为16进制)是可行的,我实际上也是这么做的,由于需要识别码,因此每个汉字用4个字母和一个识别码来表示,但真个编码长度仍然很长,希望能有更好的办法。
另外,是否可考虑使用压缩技术,对编码进行压缩后生成新的编码。
programart_life(理论派) ,能否介绍一下ISBN?
另外,是否可考虑使用压缩技术,对编码进行压缩后生成新的编码。
programart_life(理论派) ,能否介绍一下ISBN?
#13
去看看国际标准,以及图书编目的理论方面的书籍。ISBN那么多,我怎么介绍啊?
#14
大多数条码识别器都支持Code128标准了,如果不用Code39,
而用Code128的话,就可以利用现成的编码方法了。
如:Base64编码法、7Bit编码法等。
而用Code128的话,就可以利用现成的编码方法了。
如:Base64编码法、7Bit编码法等。
#15
我在考虑能否将“SQLServer-13571-16675-16414-19213-14165”使用压缩技术进行压缩后生成一种比较短的编码,不知各位大侠有没有什么好的建议。
#16
以SQLServer-13571-16675-16414-19213-14165Programming为例,
介绍一种简单的压缩方法。
1.将整数用16进制表示后,得到:
SQLServer-3503-4123-401E-4B0D-3755Programming
一个汉字由6字节变成5字节,压缩率为83.3%
2.将“-”作为汉字开始与结束符,得到:
SQLServer-35034123401E4B0D3755-Programming
n个连续汉字的情况下,n*6字节变成n*4+2字节,
n越大压缩率越明显,最大极限压缩率是66.7%。
n 原字节 压缩后字节 压缩率
1 6 6 100.0%
2 12 10 83.3%
3 18 14 77.8%
4 24 18 75.0%
5 30 22 73.3%
10 60 42 70.0%
20 120 82 68.3%
100 600 402 67.0%
介绍一种简单的压缩方法。
1.将整数用16进制表示后,得到:
SQLServer-3503-4123-401E-4B0D-3755Programming
一个汉字由6字节变成5字节,压缩率为83.3%
2.将“-”作为汉字开始与结束符,得到:
SQLServer-35034123401E4B0D3755-Programming
n个连续汉字的情况下,n*6字节变成n*4+2字节,
n越大压缩率越明显,最大极限压缩率是66.7%。
n 原字节 压缩后字节 压缩率
1 6 6 100.0%
2 12 10 83.3%
3 18 14 77.8%
4 24 18 75.0%
5 30 22 73.3%
10 60 42 70.0%
20 120 82 68.3%
100 600 402 67.0%
#17
谢谢FreshAir(FreshAir)和其它同仁的指教,我准备采取FreshAir(FreshAir)所说的方式,我3天后再结贴。
#18
如果是汉字,一个汉字我用3个字符表示。不过为了方便我把一个英文字母叶做成了3个字符表示(如果为了更节约,可以将一个英文字符用2个字符表示,或者采用FreshAir(FreshAir)的办法,不过我觉得没什么必要)。
SQLServer数据库大全 <=> 2CT2CR2CM2CT2DB2DO2DS2DB2DOQTJOFBOMKMGTQC2
日子 <=> QD8TCT
*宪法 <=> S6MNSKQDXPFFNEKNMFNGKRR7MZ3
下面是具体程序
Private Const MaCode As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"
Private Const MaLength As Long = 36
Private Sub Command1_Click()
Text2.Text = Chinese2Char(Text1.Text)
Text3.Text = Char2Chinese(Text2.Text)
End Sub
Private Function C2E(WideChar As String) As String
Dim CharVal As Long
Dim K As Long
C2E = ""
CharVal = Asc(WideChar) + 35000
For i = 2 To 1 Step -1
K = Int(CharVal / MaLength ^ i)
C2E = C2E + Mid(MaCode, K + 1, 1)
CharVal = CharVal - K * MaLength ^ i
Next i
C2E = C2E + Mid(MaCode, CharVal + 1, 1)
End Function
Private Function E2C(CodeStr As String) As String
Dim CharVal As Long
Dim K As Long
CharVal = 0
For i = 1 To 3
K = InStr(1, MaCode, Mid(CodeStr, i, 1)) - 1
CharVal = CharVal + K * MaLength ^ (3 - i)
Next i
E2C = Chr(CharVal - 35000)
End Function
Private Function Chinese2Char(ChineseStr As String) As String
Chinese2Char = ""
For i = 1 To Len(ChineseStr)
Chinese2Char = Chinese2Char + C2E(Mid(ChineseStr, i, 1))
Next i
End Function
Private Function Char2Chinese(CharStr As String) As String
Char2Chinese = ""
For i = 1 To Len(CharStr) Step 3
Char2Chinese = Char2Chinese + E2C(Mid(CharStr, i, 3))
Next i
End Function
SQLServer数据库大全 <=> 2CT2CR2CM2CT2DB2DO2DS2DB2DOQTJOFBOMKMGTQC2
日子 <=> QD8TCT
*宪法 <=> S6MNSKQDXPFFNEKNMFNGKRR7MZ3
下面是具体程序
Private Const MaCode As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"
Private Const MaLength As Long = 36
Private Sub Command1_Click()
Text2.Text = Chinese2Char(Text1.Text)
Text3.Text = Char2Chinese(Text2.Text)
End Sub
Private Function C2E(WideChar As String) As String
Dim CharVal As Long
Dim K As Long
C2E = ""
CharVal = Asc(WideChar) + 35000
For i = 2 To 1 Step -1
K = Int(CharVal / MaLength ^ i)
C2E = C2E + Mid(MaCode, K + 1, 1)
CharVal = CharVal - K * MaLength ^ i
Next i
C2E = C2E + Mid(MaCode, CharVal + 1, 1)
End Function
Private Function E2C(CodeStr As String) As String
Dim CharVal As Long
Dim K As Long
CharVal = 0
For i = 1 To 3
K = InStr(1, MaCode, Mid(CodeStr, i, 1)) - 1
CharVal = CharVal + K * MaLength ^ (3 - i)
Next i
E2C = Chr(CharVal - 35000)
End Function
Private Function Chinese2Char(ChineseStr As String) As String
Chinese2Char = ""
For i = 1 To Len(ChineseStr)
Chinese2Char = Chinese2Char + C2E(Mid(ChineseStr, i, 1))
Next i
End Function
Private Function Char2Chinese(CharStr As String) As String
Char2Chinese = ""
For i = 1 To Len(CharStr) Step 3
Char2Chinese = Char2Chinese + E2C(Mid(CharStr, i, 3))
Next i
End Function
#19
还有,如果采用FreshAir(FreshAir) 和楼主自己的办法,有一个问题你想过没有,假如书名为《程序设计1-2-3》这样的名字怎么办?你如何区分这个“-”号到底是表示后面是汉字还是就是真正的负号?
#20
Arcan(Arcan)的方法的确非常有巧妙。如果能Arcan(Arcan)和FreshAir的方法结合起来就更好了,我的设想如下:
汉字用3个字母表示,将“-”作为汉字开始与结束符(注:分隔符必须成对出现)。非汉字字符仍然保留原样。
以“SQLServer数据库大全”:为例:
FreshAir编码长度31:SQLServer-35034123401E4B0D3755-
Arcan编码长度42:2CT2CR2CM2CT2DB2DO2DS2DB2DOQTJOFBOMKMGTQC2
结合FreshAir和Arcan的方法:
编码长度26:SQLServer-QTJOFBOMKMGTQC2-
当然,最后一种办法的确存在一些问题,正如Arcan所说,如何区分这个“-”,我想从以下几个方面进行区分:
1。分隔符必须成对出现
2。两个分隔符之间的字符个数必须是3的倍数。
3。两个分隔符之间的字符必须是码表
"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"内的字符。
经过以上判断后,仍然存在这误码率,请大侠们再想想办法,找出一个万全之策。
汉字用3个字母表示,将“-”作为汉字开始与结束符(注:分隔符必须成对出现)。非汉字字符仍然保留原样。
以“SQLServer数据库大全”:为例:
FreshAir编码长度31:SQLServer-35034123401E4B0D3755-
Arcan编码长度42:2CT2CR2CM2CT2DB2DO2DS2DB2DOQTJOFBOMKMGTQC2
结合FreshAir和Arcan的方法:
编码长度26:SQLServer-QTJOFBOMKMGTQC2-
当然,最后一种办法的确存在一些问题,正如Arcan所说,如何区分这个“-”,我想从以下几个方面进行区分:
1。分隔符必须成对出现
2。两个分隔符之间的字符个数必须是3的倍数。
3。两个分隔符之间的字符必须是码表
"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"内的字符。
经过以上判断后,仍然存在这误码率,请大侠们再想想办法,找出一个万全之策。
#21
我做的时候留了点余地,没有使用-号这个字符,就是为了作修改的。
#22
完美的解答
SQLServer数据库大全 <=> SQLServer-QTJOFBOMKMGTQC2
SQL-Server数据库大全 <=> SQL-2BR-Server-QTJOFBOMKMGTQC2
数据库大全1-2-3 <=> -QTJOFBOMKMGTQC2-1-2BR-2-2BR-3
多来几个------2-3 <=> -MUIOSENZ3M0C2BR2BR2BR2BR2BR2BR-2-2BR-3
下面是完整程序:
Private Const MaCode As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"
Private Const MaLength As Long = 36
Private Function C2E(WideChar As String) As String
Dim CharVal As Long
Dim K As Long
C2E = ""
CharVal = Asc(WideChar) + 35000
For i = 2 To 1 Step -1
K = Int(CharVal / MaLength ^ i)
C2E = C2E + Mid(MaCode, K + 1, 1)
CharVal = CharVal - K * MaLength ^ i
Next i
C2E = C2E + Mid(MaCode, CharVal + 1, 1)
End Function
Private Function E2C(CodeStr As String) As String
Dim CharVal As Long
Dim K As Long
CharVal = 0
For i = 1 To 3
K = InStr(1, MaCode, Mid(CodeStr, i, 1)) - 1
CharVal = CharVal + K * MaLength ^ (3 - i)
Next i
E2C = Chr(CharVal - 35000)
End Function
Private Function Chinese2Char(ChineseStr As String) As String
Dim AtE As Boolean
Dim LinChar As String
AtE = True
Chinese2Char = ""
For i = 1 To Len(ChineseStr)
LinChar = Mid(ChineseStr, i, 1)
If Asc(LinChar) > 0 And LinChar <> "-" Then
If AtE = True Then
Chinese2Char = Chinese2Char + LinChar
Else
AtE = True
Chinese2Char = Chinese2Char + "-" + LinChar
End If
Else
If AtE = True Then
AtE = False
Chinese2Char = Chinese2Char + "-" + C2E(Mid(ChineseStr, i, 1))
Else
Chinese2Char = Chinese2Char + C2E(Mid(ChineseStr, i, 1))
End If
End If
Next i
End Function
Private Function Char2Chinese(CharStr As String) As String
Dim i As Long
Dim StrLen As Long
Dim LinChar As String
Dim AtE As Boolean
Char2Chinese = ""
StrLen = Len(CharStr)
AtE = True
i = 1
While i <= StrLen
LinChar = Mid(CharStr, i, 1)
If LinChar = "-" Then
AtE = Not AtE
i = i + 1
Else
If AtE Then
Char2Chinese = Char2Chinese + LinChar
i = i + 1
Else
Char2Chinese = Char2Chinese + E2C(Mid(CharStr, i, 3))
i = i + 3
End If
End If
Wend
End Function
Private Sub Command1_Click()
Text2.Text = Chinese2Char(Text1.Text)
Text3.Text = Char2Chinese(Text2.Text)
End Sub
SQLServer数据库大全 <=> SQLServer-QTJOFBOMKMGTQC2
SQL-Server数据库大全 <=> SQL-2BR-Server-QTJOFBOMKMGTQC2
数据库大全1-2-3 <=> -QTJOFBOMKMGTQC2-1-2BR-2-2BR-3
多来几个------2-3 <=> -MUIOSENZ3M0C2BR2BR2BR2BR2BR2BR-2-2BR-3
下面是完整程序:
Private Const MaCode As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"
Private Const MaLength As Long = 36
Private Function C2E(WideChar As String) As String
Dim CharVal As Long
Dim K As Long
C2E = ""
CharVal = Asc(WideChar) + 35000
For i = 2 To 1 Step -1
K = Int(CharVal / MaLength ^ i)
C2E = C2E + Mid(MaCode, K + 1, 1)
CharVal = CharVal - K * MaLength ^ i
Next i
C2E = C2E + Mid(MaCode, CharVal + 1, 1)
End Function
Private Function E2C(CodeStr As String) As String
Dim CharVal As Long
Dim K As Long
CharVal = 0
For i = 1 To 3
K = InStr(1, MaCode, Mid(CodeStr, i, 1)) - 1
CharVal = CharVal + K * MaLength ^ (3 - i)
Next i
E2C = Chr(CharVal - 35000)
End Function
Private Function Chinese2Char(ChineseStr As String) As String
Dim AtE As Boolean
Dim LinChar As String
AtE = True
Chinese2Char = ""
For i = 1 To Len(ChineseStr)
LinChar = Mid(ChineseStr, i, 1)
If Asc(LinChar) > 0 And LinChar <> "-" Then
If AtE = True Then
Chinese2Char = Chinese2Char + LinChar
Else
AtE = True
Chinese2Char = Chinese2Char + "-" + LinChar
End If
Else
If AtE = True Then
AtE = False
Chinese2Char = Chinese2Char + "-" + C2E(Mid(ChineseStr, i, 1))
Else
Chinese2Char = Chinese2Char + C2E(Mid(ChineseStr, i, 1))
End If
End If
Next i
End Function
Private Function Char2Chinese(CharStr As String) As String
Dim i As Long
Dim StrLen As Long
Dim LinChar As String
Dim AtE As Boolean
Char2Chinese = ""
StrLen = Len(CharStr)
AtE = True
i = 1
While i <= StrLen
LinChar = Mid(CharStr, i, 1)
If LinChar = "-" Then
AtE = Not AtE
i = i + 1
Else
If AtE Then
Char2Chinese = Char2Chinese + LinChar
i = i + 1
Else
Char2Chinese = Char2Chinese + E2C(Mid(CharStr, i, 3))
i = i + 3
End If
End If
Wend
End Function
Private Sub Command1_Click()
Text2.Text = Chinese2Char(Text1.Text)
Text3.Text = Char2Chinese(Text2.Text)
End Sub
#23
Arcan(Arcan) :
非常感谢,你的解答是最完美的,我以决定采用你的办法。
此贴保留2天后,我再结贴。
非常感谢,你的解答是最完美的,我以决定采用你的办法。
此贴保留2天后,我再结贴。
#1
将汉字的ASCII码存入integer变量中,用二进制文件的方式的PUT语句
将这个变量写入文件时,只需要2字节,而不是6字节。
如:
Dim intHanzi As Integer
intHanzi = Asc("汉")
Open "Test.Dat" For Binary As#1
Put #1,,intHanzi
Close #1
将这个变量写入文件时,只需要2字节,而不是6字节。
如:
Dim intHanzi As Integer
intHanzi = Asc("汉")
Open "Test.Dat" For Binary As#1
Put #1,,intHanzi
Close #1
#2
FreshAir(FreshAir):
我不是将汉字存入文件中,我所操作的汉字包含在大字符串中,字符串可能需要存入数据库中,另外我需要将字符串中的汉字提取出来,以字母或数字的形式表示汉字,因为我的程序中用到的一个字符串函数不能识别汉字,所以我需要以字母或数字的形式,以最少的字节数来表示汉字。
我不是将汉字存入文件中,我所操作的汉字包含在大字符串中,字符串可能需要存入数据库中,另外我需要将字符串中的汉字提取出来,以字母或数字的形式表示汉字,因为我的程序中用到的一个字符串函数不能识别汉字,所以我需要以字母或数字的形式,以最少的字节数来表示汉字。
#3
正如你说的要用字节表示汉字
以下是字符串转换字节的代码
Sub ChangeStrAryToByte(StrAry()
As String, ByteAry() As Byte)
'将字符串数组转换成字节数组
Dim LowBound, UpBound As Integer
Dim i, count, StartPos, MaxLen As Integer
Dim TmpByte() As Byte
LowBound = LBound(StrAry)
UpBound = UBound(StrAry)
count = 0
ReDim ByteAry(0)
For i = LowBound To UpBound
MaxLen = LenB(StrAry(i))
ReDim TmpByte(MaxLen + 1)
ReDim Preserve ByteAry(count + MaxLen + 1)
Call StrToByte(StrAry(i), TmpByte) '转换一个字符串
StartPos = count
Do
ByteAry(count) = TmpByte(count - StartPos)
count = count + 1
If ByteAry(count - 1) = 0 Then Exit Do
Loop '将每一个字符串对应
的字节数组按顺序填入结果数组中
ReDim Preserve ByteAry(count - 1)
Next i
End Sub
以下是字符串转换字节的代码
Sub ChangeStrAryToByte(StrAry()
As String, ByteAry() As Byte)
'将字符串数组转换成字节数组
Dim LowBound, UpBound As Integer
Dim i, count, StartPos, MaxLen As Integer
Dim TmpByte() As Byte
LowBound = LBound(StrAry)
UpBound = UBound(StrAry)
count = 0
ReDim ByteAry(0)
For i = LowBound To UpBound
MaxLen = LenB(StrAry(i))
ReDim TmpByte(MaxLen + 1)
ReDim Preserve ByteAry(count + MaxLen + 1)
Call StrToByte(StrAry(i), TmpByte) '转换一个字符串
StartPos = count
Do
ByteAry(count) = TmpByte(count - StartPos)
count = count + 1
If ByteAry(count - 1) = 0 Then Exit Do
Loop '将每一个字符串对应
的字节数组按顺序填入结果数组中
ReDim Preserve ByteAry(count - 1)
Next i
End Sub
#4
Function GetCharByte(ByVal OneChar As Integer, ByVal IsHighByte As Boolean) As Byte ' 该函数获得一个字符的高字节或低字节
If IsHighByte Then
If OneChar >= 0 Then
GetCharByte = CByte(OneChar \ 256)
'右移8位,得到高字节
Else
GetCharByte = CByte((OneChar
And &H7FFF) \ 256) Or &H80
End If
Exit Function
Else
GetCharByte = CByte(OneChar And &HFF)
'屏蔽掉高字节,得到低字节
Exit Function
End If
End Function
If IsHighByte Then
If OneChar >= 0 Then
GetCharByte = CByte(OneChar \ 256)
'右移8位,得到高字节
Else
GetCharByte = CByte((OneChar
And &H7FFF) \ 256) Or &H80
End If
Exit Function
Else
GetCharByte = CByte(OneChar And &HFF)
'屏蔽掉高字节,得到低字节
Exit Function
End If
End Function
#5
Sub StrToByte(StrToChange As String, ByteArray() As Byte)
'该函数将一个字符串转换成字节数组
Dim LowBound, UpBound As Integer
Dim i, count, length As Integer
Dim OneChar As Integer
count = 0
length = Len(StrToChange)
LowBound = LBound(ByteArray)
UpBound = UBound(ByteArray)
For i = LowBound To UpBound
ByteArray(i) = 0 '初始化字节数组
Next
For i = LowBound To UpBound
count = count + 1
If count <= length Then
OneChar = Asc(Mid(StrToChange, count, 1))
If (OneChar > 255) Or (OneChar < 0) Then
'该字符是非ASCII字符
ByteArray(i) = GetCharByte(OneChar, True) '得到高字节
i = i + 1
If i <= UpBound Then ByteArray(i)
= GetCharByte(OneChar, False)
'得到低字节
Else
'该字符是ASCII字符
ByteArray(i) = OneChar
End If
Else
Exit For
End If
Next
End Sub
'该函数将一个字符串转换成字节数组
Dim LowBound, UpBound As Integer
Dim i, count, length As Integer
Dim OneChar As Integer
count = 0
length = Len(StrToChange)
LowBound = LBound(ByteArray)
UpBound = UBound(ByteArray)
For i = LowBound To UpBound
ByteArray(i) = 0 '初始化字节数组
Next
For i = LowBound To UpBound
count = count + 1
If count <= length Then
OneChar = Asc(Mid(StrToChange, count, 1))
If (OneChar > 255) Or (OneChar < 0) Then
'该字符是非ASCII字符
ByteArray(i) = GetCharByte(OneChar, True) '得到高字节
i = i + 1
If i <= UpBound Then ByteArray(i)
= GetCharByte(OneChar, False)
'得到低字节
Else
'该字符是ASCII字符
ByteArray(i) = OneChar
End If
Else
Exit For
End If
Next
End Sub
#6
这样阿,我看看
#7
其实我是想达到如下的目的:
将包含有中文的字符串转化成条码,同时又可将条码转化成含有中文的字符串,Code39条码只能由A-Z,0-9,小数点和减号组成,例如我想将书名《SQLServer数据库大全》转化成条码,应该如何进行呢,如何才能使条码的长度最少?
将包含有中文的字符串转化成条码,同时又可将条码转化成含有中文的字符串,Code39条码只能由A-Z,0-9,小数点和减号组成,例如我想将书名《SQLServer数据库大全》转化成条码,应该如何进行呢,如何才能使条码的长度最少?
#8
先对书籍进行编目。然后用一一对应的方式去对应数据库中的真正的汉字书名。ISBN考虑了书籍重目的问题,不是将所有的信息都转化为汉字,而是将信息进行编码,然后由程序解码。其实就是数据库的问题。没什么麻烦的啊
#9
我是想实现自动编码,例如
《SQLServer数据库大全》可编码成如下:
SQLServer-13571-16675-16414-19213-14165 这样就可通过条码打印出来,
在以上编码中,我是用汉字的内码表示汉字的,
同时我又可编写一个函数将编码SQLServer-13571-16675-16414-19213-14165还原成《SQLServer数据库大全》,但以上编码太长了,我希望以上编码能够缩短一些。
《SQLServer数据库大全》可编码成如下:
SQLServer-13571-16675-16414-19213-14165 这样就可通过条码打印出来,
在以上编码中,我是用汉字的内码表示汉字的,
同时我又可编写一个函数将编码SQLServer-13571-16675-16414-19213-14165还原成《SQLServer数据库大全》,但以上编码太长了,我希望以上编码能够缩短一些。
#10
Hex(Asc("数"))
这样一个汉字用4个字母和数字来表示
这样一个汉字用4个字母和数字来表示
#11
压根就不用你自己编码,采用ISBN的编码就可以了
#12
使用Arcan(Arcan) 的方法(专为16进制)是可行的,我实际上也是这么做的,由于需要识别码,因此每个汉字用4个字母和一个识别码来表示,但真个编码长度仍然很长,希望能有更好的办法。
另外,是否可考虑使用压缩技术,对编码进行压缩后生成新的编码。
programart_life(理论派) ,能否介绍一下ISBN?
另外,是否可考虑使用压缩技术,对编码进行压缩后生成新的编码。
programart_life(理论派) ,能否介绍一下ISBN?
#13
去看看国际标准,以及图书编目的理论方面的书籍。ISBN那么多,我怎么介绍啊?
#14
大多数条码识别器都支持Code128标准了,如果不用Code39,
而用Code128的话,就可以利用现成的编码方法了。
如:Base64编码法、7Bit编码法等。
而用Code128的话,就可以利用现成的编码方法了。
如:Base64编码法、7Bit编码法等。
#15
我在考虑能否将“SQLServer-13571-16675-16414-19213-14165”使用压缩技术进行压缩后生成一种比较短的编码,不知各位大侠有没有什么好的建议。
#16
以SQLServer-13571-16675-16414-19213-14165Programming为例,
介绍一种简单的压缩方法。
1.将整数用16进制表示后,得到:
SQLServer-3503-4123-401E-4B0D-3755Programming
一个汉字由6字节变成5字节,压缩率为83.3%
2.将“-”作为汉字开始与结束符,得到:
SQLServer-35034123401E4B0D3755-Programming
n个连续汉字的情况下,n*6字节变成n*4+2字节,
n越大压缩率越明显,最大极限压缩率是66.7%。
n 原字节 压缩后字节 压缩率
1 6 6 100.0%
2 12 10 83.3%
3 18 14 77.8%
4 24 18 75.0%
5 30 22 73.3%
10 60 42 70.0%
20 120 82 68.3%
100 600 402 67.0%
介绍一种简单的压缩方法。
1.将整数用16进制表示后,得到:
SQLServer-3503-4123-401E-4B0D-3755Programming
一个汉字由6字节变成5字节,压缩率为83.3%
2.将“-”作为汉字开始与结束符,得到:
SQLServer-35034123401E4B0D3755-Programming
n个连续汉字的情况下,n*6字节变成n*4+2字节,
n越大压缩率越明显,最大极限压缩率是66.7%。
n 原字节 压缩后字节 压缩率
1 6 6 100.0%
2 12 10 83.3%
3 18 14 77.8%
4 24 18 75.0%
5 30 22 73.3%
10 60 42 70.0%
20 120 82 68.3%
100 600 402 67.0%
#17
谢谢FreshAir(FreshAir)和其它同仁的指教,我准备采取FreshAir(FreshAir)所说的方式,我3天后再结贴。
#18
如果是汉字,一个汉字我用3个字符表示。不过为了方便我把一个英文字母叶做成了3个字符表示(如果为了更节约,可以将一个英文字符用2个字符表示,或者采用FreshAir(FreshAir)的办法,不过我觉得没什么必要)。
SQLServer数据库大全 <=> 2CT2CR2CM2CT2DB2DO2DS2DB2DOQTJOFBOMKMGTQC2
日子 <=> QD8TCT
*宪法 <=> S6MNSKQDXPFFNEKNMFNGKRR7MZ3
下面是具体程序
Private Const MaCode As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"
Private Const MaLength As Long = 36
Private Sub Command1_Click()
Text2.Text = Chinese2Char(Text1.Text)
Text3.Text = Char2Chinese(Text2.Text)
End Sub
Private Function C2E(WideChar As String) As String
Dim CharVal As Long
Dim K As Long
C2E = ""
CharVal = Asc(WideChar) + 35000
For i = 2 To 1 Step -1
K = Int(CharVal / MaLength ^ i)
C2E = C2E + Mid(MaCode, K + 1, 1)
CharVal = CharVal - K * MaLength ^ i
Next i
C2E = C2E + Mid(MaCode, CharVal + 1, 1)
End Function
Private Function E2C(CodeStr As String) As String
Dim CharVal As Long
Dim K As Long
CharVal = 0
For i = 1 To 3
K = InStr(1, MaCode, Mid(CodeStr, i, 1)) - 1
CharVal = CharVal + K * MaLength ^ (3 - i)
Next i
E2C = Chr(CharVal - 35000)
End Function
Private Function Chinese2Char(ChineseStr As String) As String
Chinese2Char = ""
For i = 1 To Len(ChineseStr)
Chinese2Char = Chinese2Char + C2E(Mid(ChineseStr, i, 1))
Next i
End Function
Private Function Char2Chinese(CharStr As String) As String
Char2Chinese = ""
For i = 1 To Len(CharStr) Step 3
Char2Chinese = Char2Chinese + E2C(Mid(CharStr, i, 3))
Next i
End Function
SQLServer数据库大全 <=> 2CT2CR2CM2CT2DB2DO2DS2DB2DOQTJOFBOMKMGTQC2
日子 <=> QD8TCT
*宪法 <=> S6MNSKQDXPFFNEKNMFNGKRR7MZ3
下面是具体程序
Private Const MaCode As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"
Private Const MaLength As Long = 36
Private Sub Command1_Click()
Text2.Text = Chinese2Char(Text1.Text)
Text3.Text = Char2Chinese(Text2.Text)
End Sub
Private Function C2E(WideChar As String) As String
Dim CharVal As Long
Dim K As Long
C2E = ""
CharVal = Asc(WideChar) + 35000
For i = 2 To 1 Step -1
K = Int(CharVal / MaLength ^ i)
C2E = C2E + Mid(MaCode, K + 1, 1)
CharVal = CharVal - K * MaLength ^ i
Next i
C2E = C2E + Mid(MaCode, CharVal + 1, 1)
End Function
Private Function E2C(CodeStr As String) As String
Dim CharVal As Long
Dim K As Long
CharVal = 0
For i = 1 To 3
K = InStr(1, MaCode, Mid(CodeStr, i, 1)) - 1
CharVal = CharVal + K * MaLength ^ (3 - i)
Next i
E2C = Chr(CharVal - 35000)
End Function
Private Function Chinese2Char(ChineseStr As String) As String
Chinese2Char = ""
For i = 1 To Len(ChineseStr)
Chinese2Char = Chinese2Char + C2E(Mid(ChineseStr, i, 1))
Next i
End Function
Private Function Char2Chinese(CharStr As String) As String
Char2Chinese = ""
For i = 1 To Len(CharStr) Step 3
Char2Chinese = Char2Chinese + E2C(Mid(CharStr, i, 3))
Next i
End Function
#19
还有,如果采用FreshAir(FreshAir) 和楼主自己的办法,有一个问题你想过没有,假如书名为《程序设计1-2-3》这样的名字怎么办?你如何区分这个“-”号到底是表示后面是汉字还是就是真正的负号?
#20
Arcan(Arcan)的方法的确非常有巧妙。如果能Arcan(Arcan)和FreshAir的方法结合起来就更好了,我的设想如下:
汉字用3个字母表示,将“-”作为汉字开始与结束符(注:分隔符必须成对出现)。非汉字字符仍然保留原样。
以“SQLServer数据库大全”:为例:
FreshAir编码长度31:SQLServer-35034123401E4B0D3755-
Arcan编码长度42:2CT2CR2CM2CT2DB2DO2DS2DB2DOQTJOFBOMKMGTQC2
结合FreshAir和Arcan的方法:
编码长度26:SQLServer-QTJOFBOMKMGTQC2-
当然,最后一种办法的确存在一些问题,正如Arcan所说,如何区分这个“-”,我想从以下几个方面进行区分:
1。分隔符必须成对出现
2。两个分隔符之间的字符个数必须是3的倍数。
3。两个分隔符之间的字符必须是码表
"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"内的字符。
经过以上判断后,仍然存在这误码率,请大侠们再想想办法,找出一个万全之策。
汉字用3个字母表示,将“-”作为汉字开始与结束符(注:分隔符必须成对出现)。非汉字字符仍然保留原样。
以“SQLServer数据库大全”:为例:
FreshAir编码长度31:SQLServer-35034123401E4B0D3755-
Arcan编码长度42:2CT2CR2CM2CT2DB2DO2DS2DB2DOQTJOFBOMKMGTQC2
结合FreshAir和Arcan的方法:
编码长度26:SQLServer-QTJOFBOMKMGTQC2-
当然,最后一种办法的确存在一些问题,正如Arcan所说,如何区分这个“-”,我想从以下几个方面进行区分:
1。分隔符必须成对出现
2。两个分隔符之间的字符个数必须是3的倍数。
3。两个分隔符之间的字符必须是码表
"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"内的字符。
经过以上判断后,仍然存在这误码率,请大侠们再想想办法,找出一个万全之策。
#21
我做的时候留了点余地,没有使用-号这个字符,就是为了作修改的。
#22
完美的解答
SQLServer数据库大全 <=> SQLServer-QTJOFBOMKMGTQC2
SQL-Server数据库大全 <=> SQL-2BR-Server-QTJOFBOMKMGTQC2
数据库大全1-2-3 <=> -QTJOFBOMKMGTQC2-1-2BR-2-2BR-3
多来几个------2-3 <=> -MUIOSENZ3M0C2BR2BR2BR2BR2BR2BR-2-2BR-3
下面是完整程序:
Private Const MaCode As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"
Private Const MaLength As Long = 36
Private Function C2E(WideChar As String) As String
Dim CharVal As Long
Dim K As Long
C2E = ""
CharVal = Asc(WideChar) + 35000
For i = 2 To 1 Step -1
K = Int(CharVal / MaLength ^ i)
C2E = C2E + Mid(MaCode, K + 1, 1)
CharVal = CharVal - K * MaLength ^ i
Next i
C2E = C2E + Mid(MaCode, CharVal + 1, 1)
End Function
Private Function E2C(CodeStr As String) As String
Dim CharVal As Long
Dim K As Long
CharVal = 0
For i = 1 To 3
K = InStr(1, MaCode, Mid(CodeStr, i, 1)) - 1
CharVal = CharVal + K * MaLength ^ (3 - i)
Next i
E2C = Chr(CharVal - 35000)
End Function
Private Function Chinese2Char(ChineseStr As String) As String
Dim AtE As Boolean
Dim LinChar As String
AtE = True
Chinese2Char = ""
For i = 1 To Len(ChineseStr)
LinChar = Mid(ChineseStr, i, 1)
If Asc(LinChar) > 0 And LinChar <> "-" Then
If AtE = True Then
Chinese2Char = Chinese2Char + LinChar
Else
AtE = True
Chinese2Char = Chinese2Char + "-" + LinChar
End If
Else
If AtE = True Then
AtE = False
Chinese2Char = Chinese2Char + "-" + C2E(Mid(ChineseStr, i, 1))
Else
Chinese2Char = Chinese2Char + C2E(Mid(ChineseStr, i, 1))
End If
End If
Next i
End Function
Private Function Char2Chinese(CharStr As String) As String
Dim i As Long
Dim StrLen As Long
Dim LinChar As String
Dim AtE As Boolean
Char2Chinese = ""
StrLen = Len(CharStr)
AtE = True
i = 1
While i <= StrLen
LinChar = Mid(CharStr, i, 1)
If LinChar = "-" Then
AtE = Not AtE
i = i + 1
Else
If AtE Then
Char2Chinese = Char2Chinese + LinChar
i = i + 1
Else
Char2Chinese = Char2Chinese + E2C(Mid(CharStr, i, 3))
i = i + 3
End If
End If
Wend
End Function
Private Sub Command1_Click()
Text2.Text = Chinese2Char(Text1.Text)
Text3.Text = Char2Chinese(Text2.Text)
End Sub
SQLServer数据库大全 <=> SQLServer-QTJOFBOMKMGTQC2
SQL-Server数据库大全 <=> SQL-2BR-Server-QTJOFBOMKMGTQC2
数据库大全1-2-3 <=> -QTJOFBOMKMGTQC2-1-2BR-2-2BR-3
多来几个------2-3 <=> -MUIOSENZ3M0C2BR2BR2BR2BR2BR2BR-2-2BR-3
下面是完整程序:
Private Const MaCode As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"
Private Const MaLength As Long = 36
Private Function C2E(WideChar As String) As String
Dim CharVal As Long
Dim K As Long
C2E = ""
CharVal = Asc(WideChar) + 35000
For i = 2 To 1 Step -1
K = Int(CharVal / MaLength ^ i)
C2E = C2E + Mid(MaCode, K + 1, 1)
CharVal = CharVal - K * MaLength ^ i
Next i
C2E = C2E + Mid(MaCode, CharVal + 1, 1)
End Function
Private Function E2C(CodeStr As String) As String
Dim CharVal As Long
Dim K As Long
CharVal = 0
For i = 1 To 3
K = InStr(1, MaCode, Mid(CodeStr, i, 1)) - 1
CharVal = CharVal + K * MaLength ^ (3 - i)
Next i
E2C = Chr(CharVal - 35000)
End Function
Private Function Chinese2Char(ChineseStr As String) As String
Dim AtE As Boolean
Dim LinChar As String
AtE = True
Chinese2Char = ""
For i = 1 To Len(ChineseStr)
LinChar = Mid(ChineseStr, i, 1)
If Asc(LinChar) > 0 And LinChar <> "-" Then
If AtE = True Then
Chinese2Char = Chinese2Char + LinChar
Else
AtE = True
Chinese2Char = Chinese2Char + "-" + LinChar
End If
Else
If AtE = True Then
AtE = False
Chinese2Char = Chinese2Char + "-" + C2E(Mid(ChineseStr, i, 1))
Else
Chinese2Char = Chinese2Char + C2E(Mid(ChineseStr, i, 1))
End If
End If
Next i
End Function
Private Function Char2Chinese(CharStr As String) As String
Dim i As Long
Dim StrLen As Long
Dim LinChar As String
Dim AtE As Boolean
Char2Chinese = ""
StrLen = Len(CharStr)
AtE = True
i = 1
While i <= StrLen
LinChar = Mid(CharStr, i, 1)
If LinChar = "-" Then
AtE = Not AtE
i = i + 1
Else
If AtE Then
Char2Chinese = Char2Chinese + LinChar
i = i + 1
Else
Char2Chinese = Char2Chinese + E2C(Mid(CharStr, i, 3))
i = i + 3
End If
End If
Wend
End Function
Private Sub Command1_Click()
Text2.Text = Chinese2Char(Text1.Text)
Text3.Text = Char2Chinese(Text2.Text)
End Sub
#23
Arcan(Arcan) :
非常感谢,你的解答是最完美的,我以决定采用你的办法。
此贴保留2天后,我再结贴。
非常感谢,你的解答是最完美的,我以决定采用你的办法。
此贴保留2天后,我再结贴。