Unicode字符问题 [英] Unicode Character Issue

查看:92
本文介绍了Unicode字符问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试读取以ANSI格式保存的文本文件,包括Unicode

字符,例如法语和德语大S等,以及当我读取文件时,这些

字符显示为正方形等。


我知道如果文件将保存为Unicode,则不会是

问题。


问题是当我创建Stream时是否有选项

Reader应用程序将识别它作为Unicode字符。

谢谢,

Samuel

Hi

I am trying to read text files that are saved in ANSI format with Unicode
characters such as French e German big S etc, and as I read the file these
characters appear as squares etc.

I know that if the file would be saved as Unicode this wouldn''t be a
problem.

The question is whether there is an option that when I create the Stream
Reader the application will recognize it as Unicode characters.
Thank you,
Samuel

推荐答案

你好 -


有一个重载的构造函数接受一个编码(例如

System.Text.UnicodeEncoding)。 Visual Basic将所有字符串视为

Unicode,因此如果对String执行ReadLine(),它应该可以工作。


Joe

6月4日,12:04 * pm,Samuel < samuel.shul ... @ ntlworld.comwrote:
Hello -

There is an overloaded constructor that takes an Encoding (e.g.
System.Text.UnicodeEncoding). Visual Basic treats all strings as
Unicode so if you do a ReadLine() to a String it should work.

Joe

On Jun 4, 12:04*pm, "Samuel" <samuel.shul...@ntlworld.comwrote:




我正在尝试阅读那些文本文件以ANSI格式保存,带有Unicode

字符,例如法语e德语大S等,当我读取文件时,这些

字符显示为正方形等。


我知道如果文件保存为Unicode,这不会是一个

问题。


这个问题是否有一个选项,当我创建流

读者时,应用程序将其识别为Unicode字符。


谢谢,

Samuel
Hi

I am trying to read text files that are saved in ANSI format with Unicode
characters such as French e German big S etc, and as I read the file these
characters appear as squares etc.

I know that if the file would be saved as Unicode this wouldn''t be a
problem.

The question is whether there is an option that when I create the Stream
Reader the application will recognize it as Unicode characters.

Thank you,
Samuel


你好 -


有一个重载的构造函数接受编码(例如

System.Text.UnicodeEncoding)。 Visual Basic将其String视为

Unicode,因此如果您使用ReadLine(),则应该找到它。


Joe


6月4日,12:04 * pm,Samuel < samuel.shul ... @ ntlworld.comwrote:
Hello -

There is an overloaded constructor that takes an Encoding (e.g.
System.Text.UnicodeEncoding). Visual Basic treats its String as
Unicode so you should be find if you use ReadLine().

Joe

On Jun 4, 12:04*pm, "Samuel" <samuel.shul...@ntlworld.comwrote:




我正在尝试阅读那些文本文件以ANSI格式保存,带有Unicode

字符,例如法语e德语大S等,当我读取文件时,这些

字符显示为正方形等。


我知道如果文件保存为Unicode,这不会是一个

问题。


这个问题是否有一个选项,当我创建流

读者时,应用程序将其识别为Unicode字符。


谢谢,

Samuel
Hi

I am trying to read text files that are saved in ANSI format with Unicode
characters such as French e German big S etc, and as I read the file these
characters appear as squares etc.

I know that if the file would be saved as Unicode this wouldn''t be a
problem.

The question is whether there is an option that when I create the Stream
Reader the application will recognize it as Unicode characters.

Thank you,
Samuel




这是一个小的工作示例。无视

ReadBinary的东西。这只是来自另一个项目的
,可以写成

更小/更清洁等等。


问候,

Joergen Bech


--- snip ---


选项明确在上面

选项严格开启


进口System.IO

进口System.Text

进口系统


公共类Form1


Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase。加载

Dim ansi()As Byte = ReadBinary(" d:\ ansi.txt")

Dim s As String =

Encoding.GetEncoding(" iso-8859-1")。GetString(ansi)

TextBox1.Text = s


''Dim sb As New StringBuilder

''每个b作为字节在ansi

''sb.Append(Chr(b))

''下一个
''TextBox1.Text = sb.ToString

End Sub


公共共享函数ReadBinary(ByVal文件为字符串)As Byte()

Dim errorInformation As String = String.Empty

Dim result As Byte()= ReadBinary (file,errorInformation)

如果String.IsNullOrEmpty(errorInformation)那么

返回结果

否则

扔新的IOException(errorInformation)

结束如果

结束函数


公共共享函数ReadBinary(ByVal文件为String,_

ByRef errorInformation As

String)As Byte()


尝试

Dim fInfo作为新FileInfo(文件)

Dim numBytes As Long = fInfo.Length

Dim fStream As New FileStream(file,FileMode.Open,

FileAccess.Read)

Dim br As New BinaryReader(fStream)

Dim data()As Byte = br.ReadBytes(CInt(numBytes))


br.Close()

fStream.Close()


返回数据


Catch ex As Exception

errorInformation = ex。消息

什么都不退货


结束尝试


结束功能


2008年6月4日星期三17:04:48 +0100,Samuel

< sa ************ @ ntlworld.comwrote:

Here is a small, working example. Disregard
the ReadBinary stuff. That was just something
from another project and could be written
smaller/cleaner/etc.

Regards,

Joergen Bech

---snip---

Option Explicit On
Option Strict On

Imports System.IO
Imports System.Text
Imports System

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim ansi() As Byte = ReadBinary("d:\ansi.txt")
Dim s As String =
Encoding.GetEncoding("iso-8859-1").GetString(ansi)
TextBox1.Text = s

'' Dim sb As New StringBuilder
'' For Each b As Byte In ansi
'' sb.Append(Chr(b))
'' Next
'' TextBox1.Text = sb.ToString
End Sub

Public Shared Function ReadBinary(ByVal file As String) As Byte()
Dim errorInformation As String = String.Empty
Dim result As Byte() = ReadBinary(file, errorInformation)
If String.IsNullOrEmpty(errorInformation) Then
Return result
Else
Throw New IOException(errorInformation)
End If
End Function

Public Shared Function ReadBinary(ByVal file As String, _
ByRef errorInformation As
String) As Byte()

Try
Dim fInfo As New FileInfo(file)
Dim numBytes As Long = fInfo.Length
Dim fStream As New FileStream(file, FileMode.Open,
FileAccess.Read)
Dim br As New BinaryReader(fStream)
Dim data() As Byte = br.ReadBytes(CInt(numBytes))

br.Close()
fStream.Close()

Return data

Catch ex As Exception
errorInformation = ex.Message
Return Nothing

End Try

End Function

On Wed, 4 Jun 2008 17:04:48 +0100, "Samuel"
<sa************@ntlworld.comwrote:

>嗨

我正在尝试读取以ANSI格式保存的文本文件,其中包含Unicode
字符,例如法语e德语big S等,当我读取文件时,这些
字符显示为正方形等。

我知道如果文件将保存为Unicode,则不会是
问题。

问题是,当我创建Stream
Reader时,应用程序会将其识别为Unicode字符。

谢谢,<塞缪尔
>Hi

I am trying to read text files that are saved in ANSI format with Unicode
characters such as French e German big S etc, and as I read the file these
characters appear as squares etc.

I know that if the file would be saved as Unicode this wouldn''t be a
problem.

The question is whether there is an option that when I create the Stream
Reader the application will recognize it as Unicode characters.
Thank you,
Samuel


这篇关于Unicode字符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆