ASCII扩展字符集和unicode 是如何转换的

billy145533 2008-11-22 12:11:20
ASCII小于0x7F,只要补一个0字节就是unicode,但是0x80-0xff就不一样了
假设ASCII值为 0xA4的字符ñ,我查unicode 为0x00F1,而不是0x00A4
我想通过ASCW将0x00F1转换成ASCII值,转换结果是A4
但试过了ASC/ASCW,strconv都失败了,而对于小于0x7f的是没有问题的
我使用的中文xp+vb6
请问改如何解决,谢谢各位关注!

...全文
614 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
billy145533 2008-11-22
  • 打赏
  • 举报
回复
解决了,是API声明的问题
billy145533 2008-11-22
  • 打赏
  • 举报
回复
厉害,我查过msdn,就是没想到吧code page换一下
我对vb不熟悉,vc下面我可以用MultiByteToWideChar把ascii 为a4的转化成unicode为00f1的
但是vb调用老是出错
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Any, ByVal cchWideChar As Long) As Long

请你再指点一下
supergreenbean 2008-11-22
  • 打赏
  • 举报
回复
使用代码页437进行转换就可以了

Option Explicit
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, lpWideCharStr As Any, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, lpDefaultChar As Any, ByVal lpUsedDefaultChar As Long) As Long

Private Sub CommandButton1_Click()
On Error Resume Next
Dim cp As Long
Dim AnsiByte As Byte
Dim UnicodeByte As Long

cp = 437 'IBM PC代码页代码

UnicodeByte = AscW(TextBox1.Text) '赋值Unicode码


Call WideCharToMultiByte(437, 0, UnicodeByte, 1, AnsiByte, 1, ByVal 0, 0)

Debug.Print Hex(AnsiByte)
End Sub

Private Sub Form_Load()
TextBox1.Text = ChrW(241)
End Sub
of123 2008-11-22
  • 打赏
  • 举报
回复
STRCONV( ) Function

Converts character expressions between single-byte, double-byte, UNICODE, and locale-specific representations.


STRCONV(cExpression, nConversionSetting [, nRegionalIdentifier [, nRegionalIDType]])

Parameters
cExpression
Specifies the character expression that STRCONV( ) converts.

nConversionSetting
Specifies the type of conversion. The following table lists the values of nConversionSetting and the type of conversion performed.

nConversionSetting Conversion
1
Converts single-byte characters in cExpression to double-byte characters.

Supported for Locale ID only (specified with the nRegionalIdentifier or nRegionalIDType parameters).

2
Converts double-byte characters in cExpression to single-byte characters.

Supported for Locale ID only (specified with the nRegionalIdentifier or nRegionalIDType parameters).

3
Converts double-byte Katakana characters in cExpression to double-byte Hiragana characters.

Supported for Locale ID only (specified with the nRegionalIdentifier or nRegionalIDType parameters).

4
Converts double-byte Hiragana characters in cExpression to double-byte Katakana characters.

Supported for Locale ID only (specified with the nRegionalIdentifier or nRegionalIDType parameters).

5
Converts double-byte characters to UNICODE (wide characters).

6
Converts UNICODE (wide characters) to double-byte characters.

7
Converts cExpression to locale-specific lowercase.

Supported for Locale ID only (specified with the nRegionalIdentifier or nRegionalIDType parameters).

8
Converts cExpression to locale-specific uppercase.

Supported for Locale ID only (specified with the nRegionalIdentifier or nRegionalIDType parameters).

9
Converts double-byte characters in cExpression to UTF-8

10
Converts Unicode characters in cExpression to UTF-8

11
Converts UTF-8 characters in cExpression to double-byte characters.

12
Converts UTF-8 characters in cExpression to UNICODE characters.

13
Converts single-byte characters in cExpression to encoded base64 binary.

14
Converts base64 encoded data in cExpression to original unencoded data.

15
Converts single-byte characters in cExpression to encoded hexBinary.

16
Converts single-byte characters in cExpression to decoded hexBinary.


nRegionalIdentifier
Specifies the Locale ID, code page, or FontCharSet value to use for the conversion. If nRegionalIDType is omitted, the Locale ID is used for the conversion. If nRegionalIdentifier is omitted, the system locale ID is used by default.

If nRegionalIdentifier is invalid or not supported on the machine, the error "Invalid locale ID" is generated.

nRegionalIdentifier Language
1029
Czech

1031
German

1033
English (Default)

1034
Spanish

1036
French

1040
Italian

1045
Polish

1046
Portuguese (Brazil)

2070
Portuguese (Portugal)


nRegionalIDType
Specifies if a Locale ID, code page, or FontCharSet is used for the conversion.

The nRegionalIdentifier parameter, described above, is used to specify the actual Locale ID, code page, or FontCharSet used for the conversion.

nRegionalIDType Description
0
(Default) Specifies that the nRegionalIdentifier parameter is a Locale ID value.

1
Specifies that nRegionalIdentifier is a code page value.

2
Specifies that nRegionalIdentifier is a FontCharSet value.



Return Value
Character data type. STRCONV( ) returns the converted character expression.


STRCONV(yourstring, vbUnicode, 1036)
supergreenbean 2008-11-22
  • 打赏
  • 举报
回复
人家在Unicode码表中本来就是F1,你怎么能让它成A4呢?

而且,你如果要实现ascii到unicode的话,只要使用ascw就可以了

如果你想要得到utf-8 utf-16之类的话,则需要另写程序
billy145533 2008-11-22
  • 打赏
  • 举报
回复
在控件工具盒中引用 Microsoft Forms 2.0 Object Library,这个对象库中的所有控件都是支持Unicode的
------------------
你说的这个我试过,粘贴是可以的,直接输入ñ会变成乱码的
我在vc6下面,用这个控件试过直接输入还是没问题的
billy145533 2008-11-22
  • 打赏
  • 举报
回复
显示的问题不大
问题是F1不是我要的值,我要的是A4
supergreenbean 2008-11-22
  • 打赏
  • 举报
回复
用AscW是可以转换的,现在的问题是VB6内部窗体以及它所有控件的窗体过程(WndProc)都是Ansi版本的,也就是说,控件窗体本身不支持Unicode输入(你会发现在vb中粘贴ñ后会变成一个问号,你即便通过Byte类型将Ascii码超过&H7F的字符赋值给String后也会变成问号)。一个比较恶心的解决方案是,你在控件工具盒中引用 Microsoft Forms 2.0 Object Library,这个对象库中的所有控件都是支持Unicode的。

举个例子:
引用上面的控件库后,放个TextBox和CommandButton在窗体上,然后输入下面的代码

Private Sub CommandButton1_Click()
MsgBox (Hex(AscW(TextBox1.Text)))
End Sub

运行程序,在TextBox中粘贴入ñ,点命令按钮,你看,是不是F1出来了呢

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧