怎么转换编码?
vb.net吧
全部回复
仅看楼主
level 11
DuXian6 楼主
读取ini的项
ini为utf8编码
汉字输出为乱码,如何修复
2012年06月14日 10点06分 1
level 12
GB2312
2012年06月14日 13点06分 2
level 8
system.text.encoder
2012年06月15日 00点06分 3
具体一点好么
2012年06月15日 04点06分
level 10
原来楼主也是起源吧的啊
ini的话你调用的API方式可能有问题,需要采用 Auto 方式声明API引用,用我这个方法来读取
'读写配置文件Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Private Declare Auto Function WritePrivateProfileString Lib "kernel32" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Boolean
''' <summary>定义读取配置文件函数。默认最大256字节。</summary>
''' <param name="AppName">键名。</param><param name="StrVal">设置读取字节数。</param>
Public Function GetINI(ByVal AppName As String, Optional ByVal StrVal As UInteger = 255) As String
Dim Str As String = LSet(Nothing, StrVal)
GetPrivateProfileString("Setting", AppName, Nothing, Str, StrVal, appset)
Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1)
End Function
''' <summary>定义写入配置文件函数。失败返回0。</summary>
''' <param name="AppName">键名。</param><param name="lpDefault">写入的字符。</param>
Public Function SetINI(ByVal AppName As String, ByVal lpDefault As String) As Boolean
Return WritePrivateProfileString("Setting", AppName, lpDefault, appset)
End Function

2012年06月15日 09点06分 4
楼主要的是转换编码 读入文件不是重点
2012年06月15日 10点06分
我要的是读取UTF-8格式的ini显示到textbox中,而且可以写入UTF-8格式中文不乱码,你有什么好办法么
2012年06月15日 23点06分
我就是用的这个源代码,读取的ini为UTF-8格式,输出为汉字乱码,可不可以接受byte然后转为ansi
2012年06月15日 23点06分
level 10
回复 @度娘管你○ :
那调用API的声明方式为这样
Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Boolean
另外,利用IO读取文本,除非你要自己写一个INI分析提取工具,否则不推荐你直接读取文本
按编码读取文本的方式为下面几种
读取文件文本,无法读出中文
IO.File.ReadAllText("path")
读取文件文本,让程序自行判断读取内容的编码,推荐
IO.File.ReadAllText("path", Text.Encoding.Default)
读取文件文本,强制用UTF-8来读取
IO.File.ReadAllText("path", Text.Encoding.UTF8)
读取文本文件,用GB2312编码来读取
IO.File.ReadAllText("path", Text.Encoding.GetEncoding("gb2312"))

2012年06月16日 11点06分 5
level 11
DuXian6 楼主
我已经解决了这个问题,原文件我直接全转ANSI再读,然后保存为UTF-8,
可又出现了问题,就是源文件必须保存为无BOM的UTF-8格式,请问怎么去掉BOM,听别人说BOM就是在无BOM的基础上在文件头添加了三个字节,现在可能有思路就是说去掉ini文件头的三个字节保存,你看看怎样帮我实现功能
2012年06月16日 11点06分 6
level 10
用Notepad++转换下呗
2012年06月16日 13点06分 7
我有那个软件,但是我要实现全自动的程序控制啊,难道notepad++有启动参数可以直接转换?
2012年06月16日 23点06分
level 12
创建UTF8编码对象的时候构造函数参数传个false进去就不带bom了。
Dim utf8 As New System.Text.UTF8Encoding(False)
2012年06月17日 03点06分 8
怎么改?
2012年06月17日 05点06分
level 11
DuXian6 楼主
Dim path1 As String = Application.StartupPath + "\rev.ini"
Dim Mazmun As String
Dim strFileName As String
strFileName = "rev.ini"
Dim a
a = CreateObject("ADODB.Stream")
a.Charset = "utf-8 without bom"
a.open()
a.LoadFromFile(strFileName)
Mazmun = a.ReadText
a.Close()
Dim stm
stm = CreateObject("adodb.stream")
stm.Type = 2
stm.mode = 3
stm.Charset = "gb2312"
stm.open()
stm.WriteText(Mazmun)
stm.SaveToFile(strFileName, 2)
stm.flush()
stm.Close()
stm = Nothing
playername = GetINI("steamclient", "playername", "", path1)
clan = GetINI("steamclient", "ClanTag", "", path1)
TextBox1.Text = playername
TextBox2.Text = clan
请问怎么改?
2012年06月17日 05点06分 9
a.Charset = "utf-8 without bom" 这儿是UTF-8 那个是我改的,没用
2012年06月17日 05点06分
level 12
用StreamReader/StreamWriter读写
2012年06月17日 07点06分 10
写个简单代码号么,我不懂
2012年06月17日 09点06分
level 12
没有异常处理,用法:
Public Sub Test_()
Dim fileName As String = "" 'ini文件路径
Dim utf8 As New UTF8Encoding(False) '原ini文件的编码
Dim ini As New INIHelper(fileName, utf8) '打开ini文件
ini.GetINI("OPTION", "UserName")
ini.SetINI("OPTION", "MaxCount", "3")
ini.Close() '关闭
End Sub
Imports System.IO
Imports System.Text
Public Class INIHelper
Implements IDisposable
#Region "Native Methods"
Private Declare Auto Function WritePrivateProfileString Lib "kernel32" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Boolean
Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
#End Region
Private mfileName As String
Private writeCount As Integer
Public Sub New(fileName As String, encoding As Encoding)
mfileName = fileName
OriginalEncoding = encoding
PrepareTempFile()
End Sub
#Region "properties"
Public Property OriginalEncoding As Encoding = Encoding.Default
Public ReadOnly Property FileName As String
Get
Return mfileName
End Get
End Property
Public ReadOnly Property TempFileName As String
Get
Return mfileName & "." & Me.GetHashCode() & ".tmp"
End Get
End Property
#End Region
#Region "private methods"
Private Sub PrepareTempFile()
If File.Exists(TempFileName) Then
File.Delete(TempFileName)
End If
Using sr As New StreamReader(FileName, OriginalEncoding),
sw As New StreamWriter(TempFileName, False, Encoding.Unicode)
sw.Write(sr.ReadToEnd)
End Using
End Sub
Private Sub Submit()
Using sr As New StreamReader(TempFileName, Encoding.Unicode),
sw As New StreamWriter(New FileStream(FileName, FileMode.Truncate, FileAccess.ReadWrite))
sw.Write(sr.ReadToEnd)
End Using
End Sub
#End Region
#Region "public methods"
Public Function GetINI(sectionName As String, keyName As String, Optional stringLength As Integer = 255) As String
Dim buffer As New StringBuilder(stringLength)
GetPrivateProfileString(sectionName, keyName, Nothing, buffer, stringLength, TempFileName)
Return buffer.ToString
End Function
Public Function GetINI(keyName As String, Optional stringLength As Integer = 255) As String
Return GetINI("Setting", keyName)
End Function
Public Function SetINI(sectionName As String, keyName As String, lpValue As String) As Boolean
writeCount += 1
Return WritePrivateProfileString(sectionName, keyName, lpValue, TempFileName)
End Function
Public Function SetINI(keyName As String, lpValue As String) As Boolean
Return SetINI("Setting", keyName, lpValue)
End Function
Public Sub Close()
Dispose()
End Sub
#End Region
#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: dispose managed state (managed objects).
End If
If writeCount > 0 Then Submit()
File.Delete(TempFileName)
' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
' TODO: set large fields to null.
End If
Me.disposedValue = True
End Sub
' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
Protected Overrides Sub Finalize()
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(False)
MyBase.Finalize()
End Sub
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class

2012年06月17日 11点06分 11
level 12
看上去一大篇,其实很简单。就是先用原来的编码读,保存为Unicode编码的临时文件。然后调用API操作这个Unicode编码的临时文件,最后用原来的编码把临时文件内容覆盖到原文件并删除临时文件。
2012年06月17日 11点06分 12
那怎么无BOM呢?
2012年06月18日 07点06分
@DuXian6 Dim utf8 As New UTF8Encoding(False)这个就是无BOM,传True就是有BOM
2012年06月18日 07点06分
@坂井悠二の阴谋 可以在我那个简单东西改么,必须用你这个,你这个我还得研究一会
2012年06月18日 07点06分
@DuXian6 你把核心代码提出来,其实就几句……
2012年06月18日 07点06分
level 11
DuXian6 楼主
@坂井悠二の阴谋
你这段代码一复制进VB.NET就有了100多个错误
2012年06月18日 07点06分 13
level 12
……
Public Sub Test_()
Dim fileName As String = "" 'ini文件路径
Dim utf8 As New UTF8Encoding(False) '原ini文件的编码
Dim ini As New INIHelper(fileName, utf8) '打开ini文件
ini.GetINI("OPTION", "UserName")
ini.SetINI("OPTION", "MaxCount", "3")
ini.Close() '关闭
End Sub
这一段是例子
后面的内容要单独放在一个文件里

2012年06月18日 08点06分 14
对于getini和setini的函数跟这个没关系吧?
2012年06月19日 03点06分
@DuXian6 什么
2012年06月19日 03点06分
@坂井悠二の阴谋 VB.NET里没有这两个函数,得先声明吧?
2012年06月19日 03点06分
@DuXian6 我那段代码里有
2012年06月19日 03点06分
level 12
你不一定要用我那个,我那个是随便写的,只是个例子
2012年06月19日 03点06分 15
未定义内型INIHELPER?[$1]未定义内型UTF8Encoding?
2012年06月19日 03点06分
@DuXian6 引入命名空间System.Text
2012年06月19日 03点06分
@坂井悠二の阴谋 我引入了啊
2012年06月19日 03点06分
@坂井悠二の阴谋 你那段代码的声明部分放到类库里面去么?
2012年06月19日 03点06分
level 11
DuXian6 楼主
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32
'定义读取配置文件函数
Public Function GetINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As String
Dim Str As String = LSet(Str, 256)
GetPrivateProfileString(Section, AppName, lpDefault, Str, Len(Str), FileName) Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1)
End Function '定义写入配置文件函数
Public StreamReader
Dim fbl As String, playername As String, clan As String
Function WriteINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As Long
WriteINI = WritePrivateProfileString(Section, AppName, lpDefault, FileName) End Function
我是这样声明函数的
2012年06月19日 03点06分 16
level 11
DuXian6 楼主
我对imports那段有点困惑,他说签名必须为声明
2012年06月19日 03点06分 17
level 11
DuXian6 楼主
我搞明白了,能留下QQ教我么
2012年06月19日 03点06分 18
level 12
我很少上Q
2012年06月19日 03点06分 19
1