【IT168 技术】 各类解决汉字等非ASCII字符乱码问题的方法,归根结底都是一种数字化的表现方式。使用ASCII字符集中的字符对汉字编码处理进而可以被正常接收并处理。我们给出的前后台方式,是根据通常使用的方法进行总结。
最简单的是前台利用JS中自身的escape与unescape进行相应的编码,后台需要进行相应代码处理。这样开发工作量主要是后台,如果使用Java中自带的方法这些后台工作也可简化。比较复杂一些的是将字符串中每个字符的码值用分隔符拼接成串再进行传送,后台通过处理再还原成正常的字符串。这需要前台与后台都要做相应的开发处理。
通过对码值进行十进制编码或十六进制编码,我们可以得到不同的字符串。通常建议使用十六进制,处理比较大的字符串可以缩减不少长度。
方式一:后台开发与JS中escape\unescape匹配的方法
前台JS:escape\unescape
后台Lotus:Function HTMLDecode(Byval encodedText As String) As String
‘Javascript unescape 采用的Unicode解码
Dim index,length As Integer
Dim StringPiece As String ’字符串中每个字符
Dim StringPieceUnicode As Long ‘字符串中每个字符对应编码
Dim Decode As String
’Dim ReserveString As String
length=Len(encodedText)
‘ReserveString=“*+-./@_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”
Decode=“”
’Msgbox encodeText
For index=1 To length
StringPiece=Mid(encodedText,index,1)
‘StringPieceUnicode=Uni(StringPiece)
’Msgbox StringPiece
If StringPiece=“%” Then
StringPiece=Mid(encodedText,index,2)
If StringPiece=“%u” Or StringPiece=“%U” Then
StringPiece=Mid(encodedText,index+2,4)
index=index+5
‘%u____
Else
StringPiece=Mid(encodedText,index+1,2)
index=index+2
’%__
End If
‘Msgbox StringPiece
Decode=Decode+Ustring(1,getHex(StringPiece))
Else
Decode=Decode+StringPiece
End If
Next
HTMLDecode=Decode
’Msgbox “HTMLDecode:“+encodeText+” “+Decode
End Function
Function HTMLEncode(Byval plainText As String)As String
‘Javascript escape 采用的Unicode编码
Dim index,length As Integer
Dim StringPiece As String ’字符串中每个字符
Dim StringPieceUnicode As Long ‘字符串中每个字符对应编码
Dim Encode As String
Dim ReserveString As String
length=Len(plainText)
ReserveString=”*+-./@_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ“
Encode=”“
For index=1 To length
StringPiece=Mid(plainText,index,1)
StringPieceUnicode=Uni(StringPiece)
If StringPieceUnicode<256 Then
If Instr(1,ReserveString,StringPiece)>=1 Then
Encode=Encode+StringPiece
Else
Encode=Encode+”%“+Hex(StringPieceUnicode)
End If
Else
Encode=Encode+”%u“+Hex(StringPieceUnicode)
End If
Next
HTMLEncode=Encode
End Function
方式二:前后台对汉字码值编码以及解码
十进制方式(字符串比较长)
前台JS
function getUnicodeEncode(ChineseWord,separator)
{
var Encode=”“;
if(separator=”“) separator=”;“;
for(var index=0;index<ChineseWord.length;index++)
{
Encode=Encode+ChineseWord.charCodeAt(index).toString()+separator;
}
return Encode;
}
function getUnicodeDecode(EncryptWord,separator)
{
var Unicodes=null;
var ChineseWord=”“;
if(separator=”“) separator=”;“;
Unicodes=EncryptWord.split(separator);
for(var index=0;index<Unicodes.length;index++)
{
var unicode=parseInt(Unicodes[index]);
if(!isNaN(unicode))
ChineseWord=ChineseWord+String.fromCharCode(unicode);
}
return ChineseWord;
}
后台Lotus
Function UnicodeEncode(ByVal ChineseWord As String,ByVal separator As String) As String
Dim Encode As String
if separator=”“ Then separator=”;“
For Index=1 to Len(ChineseWord)
Encode=Encode+Cstr(Uni(Mid(ChineseWord,Index,1)))+separator
Next
UnicodeEncode=Encode
End Function
Function UnicodeDecode(Byval EncryptWord As String,Byval separator As String)As String
Dim Unicodes As Variant
Dim ChineseWord As String
Dim Index As Integer
If separator=”“ Then separator=”;“
Unicodes=Split(EncryptWord,separator)
ChineseWord=”“
For Index=Lbound(Unicodes) To Ubound(Unicodes)
If Isnumeric(Unicodes(Index)) Then ChineseWord=ChineseWord+Ustring(1,CLng(Unicodes(Index)))
Next
UnicodeDecode=ChineseWord
End Function
十六进制方式(字符串略短)
function getUnicodeEncode(ChineseWord,separator)
{
var Encode=”“;
if(separator=”“) separator=”;“;
for(var index=0;index<ChineseWord.length;index++)
{
Encode=Encode+ChineseWord.charCodeAt(index).toString(16)+separator;
}
return Encode;
}
function getUnicodeDecode(EncryptWord,separator)
{
var Unicodes=null;
var ChineseWord=”“;
if(separator=”“) separator=”;“;
Unicodes=EncryptWord.split(separator);
for(var index=0;index<Unicodes.length;index++)
{
var unicode=parseInt(Unicodes[index],16);
if(!isNaN(unicode))
ChineseWord=ChineseWord+String.fromCharCode(unicode);
}
return ChineseWord;
}
后台Lotus
Function UnicodeEncode(ByVal ChineseWord As String,ByVal separator As String) As String
Dim Encode As String
if separator=”“ Then separator=”;“
For Index=1 to Len(ChineseWord)
Encode=Encode+Cstr(Hex(Uni(Mid(ChineseWord,Index,1))))+separator
Next
UnicodeEncode=Encode
End Function
Function UnicodeDecode(Byval EncryptWord As String,Byval separator As String)As String
Dim Unicodes As Variant
Dim ChineseWord As String
Dim Index As Integer
If separator=”“ Then separator=”;“
Unicodes=Split(EncryptWord,separator)
ChineseWord=”“
For Index=Lbound(Unicodes) To Ubound(Unicodes)
ChineseWord=ChineseWord+Ustring(1,getHex(Unicodes(Index)))
Next
UnicodeDecode=ChineseWord
End Function
Function getHex(Byval hexText As String)As Long
Dim index,length As Integer
Dim getValue As Long
Dim HexPiece As String
Dim ReserveHex As String
Dim Pos As Integer
hexText=Trim(hexText)
hexText=Ucase(hexText)
getValue=0
length=Len(hexText)
ReserveHex=”0123456789ABCDEF“
For index=1 To length
HexPiece=Mid(hexText,index,1)
Pos=Instr(1,ReserveHex,HexPiece) ’1~16
If Pos<1 Or Pos>16 Then
getHex=getValue
Exit Function
End If
If getValue>0 Then getValue=getValue*16
getValue=getValue+(Pos-1)
Next
getHex=getValue
End Function