VB中MultiByteToWideChar如何调用使得文本格式由ANSI转换成UTF-8
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VB中MultiByteToWideChar如何调用使得文本格式由ANSI转换成UTF-8相关的知识,希望对你有一定的参考价值。
F盘的TEMP文件夹里有一个A.TXT,其编码为ansi,希望高手帮写个程序,把该文本转换成UFT-8的编码
要求该函数或过程传入的参数是文件的路径,比如传入"f:\temp\a.txt"
然后就会将f:\temp\a.txt转换成UTF-8编码格式的文件,替换掉原先的a.txt
注意,是用MultiByteToWideChar函数,因为我把UTF-8转换成ANSI就是用这个函数,现在想转回去,即ANSI转UTF-8,用其他方法转的话是可以,但是容易出错
成功后追加100分,不胜感激!
好像转回来是用这个吧WideCharToMultiByte
分二步:
一、新建一个模块,复制下面代码:
Option Explicit
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long
Private Const CP_UTF8 = 65001
'读文件至变量
Function GetFile(Filename As String) As String
Dim i As Integer, S As String, BB() As Byte
If Dir(Filename) = "" Then Exit Function
i = FreeFile
ReDim BB(FileLen(Filename) - 1)
Open Filename For Binary As #i
Get #i, , BB
Close #i
S = BB
GetFile = S
End Function
'写入文件1
Public Function PutFile(Filename As String, txt As String)
If txt = "" Then Exit Function
If Dir(Filename) <> "" Then Kill Filename
Dim i As Integer, S As String, BB() As Byte
i = FreeFile
Open Filename For Binary As #i
Put #i, , txt
Close #i
End Function
Public Function UTF8_Encode(Filename As String) As Boolean
Dim i As Long
Dim TLen As Long
Dim lPtr As Long
Dim UTF16 As Long
Dim UTF8_EncodeLong As String
Dim strUnicode As String, S As String
If Dir(Filename) = "" Or Filename = "" Then Exit Function
strUnicode = GetFile(Filename)
strUnicode = StrConv(strUnicode, vbUnicode)
TLen = Len(strUnicode)
If TLen = 0 Then Exit Function
Dim lngBufferSize As Long
Dim lngResult As Long
Dim bytUtf8() As Byte
lngBufferSize = TLen * 3 + 1
ReDim bytUtf8(lngBufferSize - 1)
lngResult = WideCharToMultiByte(CP_UTF8, 0, StrPtr(strUnicode), _
TLen, bytUtf8(0), lngBufferSize, vbNullString, 0)
If lngResult Then
lngResult = lngResult - 1
ReDim Preserve bytUtf8(lngResult)
S = StrConv(bytUtf8, vbUnicode)
End If
PutFile Filename, S
UTF8_Encode = True
End Function
二、程序调用如下:
If UTF8_Encode("f:\temp\a.txt") Then
MsgBox ""文件转换成功!"
Else
MsgBox "文件转换失败!"
End If
注意:转换成功后的文件将被覆盖,直接变成UTF-8编码。本回答被提问者采纳 参考技术B Private Sub AnsiToUtf8 (FileName as String)
Dim b() As Byte, S As String
Dim ADO_Stream As Object
If Dir(FileName) = "" Then Exit Sub
ReDim b(FileLen(FileName))
Open FileName For Binary As #1
Get #1, , b
Close #1
S = StrConv(b, vbUnicode)
Set ADO_Stream = CreateObject("ADODB.Stream")
With ADO_Stream
.Type = 2
.Mode = 3
.Charset = "utf-8"
.open
.WriteText S
.SaveToFile FileName, 2
End With
Set ADO_Stream = Nothing
End Sub
参考资料: http://zhidao.baidu.com/question/83609963.html?si=3
关于VB.NET进度条的实例
我想编一个进度条程序,让它每0.5秒进度增加1%,并用文本显示当前的进度,该如何实现?
这是我自己写的一个进度条控件,如图:
边框粗细和边框颜色以及进度条颜色都可以调。
进度条大小你可以自己拉伸。
同时,进度条还提供了丰富的动画效果(如边框闪烁效果【可关闭】,进度条滑动效果等。)
将边框粗细调为0即可实现无边框(同时边框闪烁效果(见“Flash”属性)也将关闭!)
进度值(见“ProgressValue”属性)类型为Single(单精度浮点数)
将我提供的DLL文件拖到工具箱即可安装
只能用于Windows窗体应用程序!
目标平台:.Net Framework 3.5
属性说明:
1.BorderColor 用于调整边框颜色
2.BorderWidth 用于调整边框粗细
3.Flash 用于开启、关闭边框闪烁效果
4.ProgressValue 用于设置、查看进度值
5.ForeColor 用于设置进度条颜色
6.BackColor 用于设置控件背景色
附件如果丢失,请私信。
Me.ProgressBar1.Style = ProgressBarStyle.Continuous
n = 0
Me.Timer1.Enabled = True
Me.Timer1.Interval = 500
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Timer1.Enabled = False
n = n + 1
If n <= 100 Then
Me.ProgressBar1.Value = n
Me.Timer1.Enabled = True
Else
Me.ProgressBar1.Value = 100
End If
'在定时器里跟新n就可以了,可以添加个label
End Sub
n为全局变量,添加一个进度条控件和定时器控件,文本显示进度就显示n值就可以了本回答被提问者采纳
以上是关于VB中MultiByteToWideChar如何调用使得文本格式由ANSI转换成UTF-8的主要内容,如果未能解决你的问题,请参考以下文章
char向wchar的转换-MultiByteToWideChar
WideCharToMultiByte和MultiByteToWideChar函数的用法
C++程序中,MultiByteToWideChar的使用问题 ,下面的是一个简单的实现,但是转换不了UNICODE编码