这个题目有点变态,不过有时确实会有这种需求,起码我就碰到过。同样变态的需求还有“如何用VB6在日文系统下把Unicode编码的中文字符转成GB2312编码”。这种需求有个比较时髦的名字,叫做“国际对应”。本文将提供几种解决方法。
一:繁琐的方法
如果之前看过我贴的那篇 VB6中EBCDIC码和Unicode码之间的相互转换方法,可能马上就想到答案了。没错,我们可以用WideCharToMultiByte,把里面的代码页改成日文932就行了。WideCharToMultiByte接受的源数据是Unicode编码的数组,那么问题自然就转变为如何把这个字符串转成数组。下面有两种做法:
1 转成Byte数组
2 转成Integer数组
其实,大可不必自己大费周折地手动把一个个16位的Unicode字符转成8位的字节数组,直接转成16位的Integer数组(VB6的Integer是16位的哦)就行了,剩下的事情交给机器做--从物理上说,存数据的这块连续内存的大小和内容完全和上面是一样的。所不同的只在逻辑上。
二:最简捷的方法
StrConv(string, conversion, LCID)
还记得StrConv函数的最后一个参数吗?MSDN上是这么说的:
LCID Optional. The LocaleID, if different than the system LocaleID. (The system LocaleID is the default.)
大部分的时候都是用VB6提供的默认值。现在这个参数就派上用场了,下面是例子:
一:繁琐的方法
如果之前看过我贴的那篇 VB6中EBCDIC码和Unicode码之间的相互转换方法,可能马上就想到答案了。没错,我们可以用WideCharToMultiByte,把里面的代码页改成日文932就行了。WideCharToMultiByte接受的源数据是Unicode编码的数组,那么问题自然就转变为如何把这个字符串转成数组。下面有两种做法:
1 转成Byte数组
1
Private
Function
Uni2WChar(str
As
String
)
As
Byte
()
2
Dim
i
As
Long
3
Dim
idx
As
Long
4
Dim
iC
As
Integer
5
Dim
iH
As
Integer
6
Dim
iL
As
Integer
7
Dim
byt()
As
Byte
8
9
idx
=
0
10
'
多定义一字节,作为结尾'/0'(就像C里面一样)
11
ReDim
byt(
Len
(str)
*
2
+
1
)
12
13
For
i
=
1
To
Len
(str)
14
iC
=
AscW(
Mid
(str, i,
1
))
15
iH
=
(iC
And
&
HFF00)
/
&
H100
16
If
iC
<
0
Then
iH
=
iH
+
&
H100
17
iL
=
iC
And
&
HFF
18
'
高低字节是反着存的
19
byt(idx)
=
iL
20
byt(idx
+
1
)
=
iH
21
idx
=
idx
+
2
22
Next
i
23
24
Uni2WChar
=
byt
25
End Function
Private
Function
Uni2WChar(str
As
String
)
As
Byte
()2
Dim
i
As
Long
3
Dim
idx
As
Long
4
Dim
iC
As
Integer
5
Dim
iH
As
Integer
6
Dim
iL
As
Integer
7
Dim
byt()
As
Byte
8

9
idx
=
0
10
'
多定义一字节,作为结尾'/0'(就像C里面一样)
11
ReDim
byt(
Len
(str)
*
2
+
1
)12

13
For
i
=
1
To
Len
(str)14
iC
=
AscW(
Mid
(str, i,
1
))15
iH
=
(iC
And
&
HFF00)
/
&
H10016
If
iC
<
0
Then
iH
=
iH
+
&
H10017
iL
=
iC
And
&
HFF18
'
高低字节是反着存的
19
byt(idx)
=
iL20
byt(idx
+
1
)
=
iH21
idx
=
idx
+
2
22
Next
i23

24
Uni2WChar
=
byt25
End Function
2 转成Integer数组
其实,大可不必自己大费周折地手动把一个个16位的Unicode字符转成8位的字节数组,直接转成16位的Integer数组(VB6的Integer是16位的哦)就行了,剩下的事情交给机器做--从物理上说,存数据的这块连续内存的大小和内容完全和上面是一样的。所不同的只在逻辑上。
1
Private
Function
Uni2WChar(str
As
String
)
As
Integer
()
2
Dim
i
As
Long
3
Dim
ary()
As
Integer
4
5
ReDim
ary(
Len
(str))
6
7
For
i
=
1
To
Len
(str)
8
ary(i
-
1
)
=
AscW(
Mid
(str, i,
1
))
9
Next
i
10
11
Uni2WChar
=
ary
12
End Function
That's all!记得要把那个WChar2XXX函数的参数类型改成Integer数组。
Private
Function
Uni2WChar(str
As
String
)
As
Integer
()2
Dim
i
As
Long
3
Dim
ary()
As
Integer
4

5
ReDim
ary(
Len
(str))6

7
For
i
=
1
To
Len
(str)8
ary(i
-
1
)
=
AscW(
Mid
(str, i,
1
))9
Next
i10

11
Uni2WChar
=
ary12
End Function
二:最简捷的方法
StrConv(string, conversion, LCID)
还记得StrConv函数的最后一个参数吗?MSDN上是这么说的:
LCID Optional. The LocaleID, if different than the system LocaleID. (The system LocaleID is the default.)
大部分的时候都是用VB6提供的默认值。现在这个参数就派上用场了,下面是例子:
1
Dim
byt()
As
Byte
2
'
把"一二三"转成日文Shift-JIS编码,LCID=0x0411
3
byt
=
StrConv(
"
一二三
"
, vbFromUnicode,
&
H411)
4
'
把"一二三"转成中文GB2312编码,LCID=0x0804
5
byt
=
StrConv(
"
一二三
"
, vbFromUnicode,
&
H804)
Dim
byt()
As
Byte
2
'
把"一二三"转成日文Shift-JIS编码,LCID=0x0411
3
byt
=
StrConv(
"
一二三
"
, vbFromUnicode,
&
H411)4
'
把"一二三"转成中文GB2312编码,LCID=0x0804
5
byt
=
StrConv(
"
一二三
"
, vbFromUnicode,
&
H804)
一行就搞定!LCID还有很多值,具体参照MSDN,搜关键字“CultureInfo 类”。同样,你应该先确认系统安装了对应LCID的代码页。
本文介绍使用VB6进行字符编码转换的方法,包括Unicode到GB2312及Shift-JIS编码的转换技巧。通过详细步骤展示如何利用VB6内置函数实现高效编码转换。
2285

被折叠的 条评论
为什么被折叠?



