VB6.0 如何将string 字符串转换成utf
答案:2 悬赏:30 手机版
解决时间 2021-02-12 09:10
- 提问者网友:蓝琪梦莎
- 2021-02-11 17:48
VB6.0 如何将string 字符串转换成utf
最佳答案
- 五星知识达人网友:执傲
- 2021-02-11 18:56
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 Long, ByVal cchWideChar As Long) As Long
Private Declare Function WideCharToMultiByte Lib "kernel32 " (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
Private Const CP_ACP = 0 ' default to ANSI code page
Private Const CP_UTF8 = 65001 ' default to UTF-8 code page
'字符转 UTF8
Public Function EncodeToBytes(ByVal sData As String) As Byte() ' Note: Len(sData) > 0
Dim aRetn() As Byte
Dim nSize As Long
nSize = WideCharToMultiByte(CP_UTF8, 0, StrPtr(sData), -1, 0, 0, 0, 0) - 1
If nSize = 0 Then Exit Function
ReDim aRetn(0 To nSize - 1) As Byte
WideCharToMultiByte CP_UTF8, 0, StrPtr(sData), -1, VarPtr(aRetn(0)), nSize, 0, 0
EncodeToBytes = aRetn
Erase aRetn
End Function
' UTF8 转字符
Public Function DecodeToBytes(ByVal sData As String) As Byte() ' Note: Len(sData) > 0
Dim aRetn() As Byte
Dim nSize As Long
nSize = MultiByteToWideChar(CP_UTF8, 0, StrPtr(sData), -1, 0, 0) - 1
If nSize = 0 Then Exit Function
ReDim aRetn(0 To 2 * nSize - 1) As Byte
MultiByteToWideChar CP_UTF8, 0, StrPtr(sData), -1, VarPtr(aRetn(0)), nSize
DecodeToBytes = aRetn
Erase aRetn
End Function
Private Sub Command1_Click()
Dim s As String
s = StrConv(EncodeToBytes("中文"), vbUnicode) '将utf编码的数组转VB可处理字符
MsgBox s
t = DecodeToBytes(StrConv(s, vbFromUnicode))
MsgBox t
End Sub
Private Declare Function WideCharToMultiByte Lib "kernel32 " (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
Private Const CP_ACP = 0 ' default to ANSI code page
Private Const CP_UTF8 = 65001 ' default to UTF-8 code page
'字符转 UTF8
Public Function EncodeToBytes(ByVal sData As String) As Byte() ' Note: Len(sData) > 0
Dim aRetn() As Byte
Dim nSize As Long
nSize = WideCharToMultiByte(CP_UTF8, 0, StrPtr(sData), -1, 0, 0, 0, 0) - 1
If nSize = 0 Then Exit Function
ReDim aRetn(0 To nSize - 1) As Byte
WideCharToMultiByte CP_UTF8, 0, StrPtr(sData), -1, VarPtr(aRetn(0)), nSize, 0, 0
EncodeToBytes = aRetn
Erase aRetn
End Function
' UTF8 转字符
Public Function DecodeToBytes(ByVal sData As String) As Byte() ' Note: Len(sData) > 0
Dim aRetn() As Byte
Dim nSize As Long
nSize = MultiByteToWideChar(CP_UTF8, 0, StrPtr(sData), -1, 0, 0) - 1
If nSize = 0 Then Exit Function
ReDim aRetn(0 To 2 * nSize - 1) As Byte
MultiByteToWideChar CP_UTF8, 0, StrPtr(sData), -1, VarPtr(aRetn(0)), nSize
DecodeToBytes = aRetn
Erase aRetn
End Function
Private Sub Command1_Click()
Dim s As String
s = StrConv(EncodeToBytes("中文"), vbUnicode) '将utf编码的数组转VB可处理字符
MsgBox s
t = DecodeToBytes(StrConv(s, vbFromUnicode))
MsgBox t
End Sub
全部回答
- 1楼网友:平生事
- 2021-02-11 19:08
这种转换一般用于网页地址; 我不知道 delphi 是不是有现成的函数, 用到了就写了一个.
//函数:
function toutf8encode(str: string): string;
var
b: byte;
begin
for b in bytesof(utf8encode(str)) do
result := format('%s%%%.2x', [result, b]);
end;
//测试:
var
str: string;
begin
str := '万一';
str := toutf8encode(str);
showmessage(str); //%e4%b8%87%e4%b8%80
end;
为 "小月124" 写了个反向函数:
function toutf8decode(const str: string): string;
var
list: tstrings;
tmpstr: ansistring;
i: integer;
begin
list := tstringlist.create;
extractstrings(['%'], ['%'], pchar(str), list);
setlength(tmpstr, list.count);
for i := 0 to list.count - 1 do
byte(tmpstr[i+1]) := strtoint('$' + list[i]);
list.free;
result := utf8decode(tmpstr);
end;
{ 调用测试 }
procedure tform1.formcreate(sender: tobject);
var
s1: ansistring;
s2: widestring;
begin
s1 := '%e4%b8%87%e4%b8%80';
s2 := toutf8decode(s1);
showmessage(s2); { 万一 }
end;
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯