VB.NET里面有没有从Big5到GB码的转换函数?

GSDN 2004-10-14 09:43:29
如:
Big5->GB
GB->Big5
Big5->UTF8
GB->UTF8
...全文
558 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
CSDN_0 2004-10-14
  • 打赏
  • 举报
回复
Encoding 类
表示字符编码。

有关此类型所有成员的列表,请参阅 Encoding 成员。

System.Object
System.Text.Encoding
System.Text.ASCIIEncoding
System.Text.UnicodeEncoding
System.Text.UTF7Encoding
System.Text.UTF8Encoding

[Visual Basic]
<Serializable>
MustInherit Public Class Encoding

[C#]
[Serializable]
public abstract class Encoding

[C++]
[Serializable]
public __gc __abstract class Encoding

[JScript]
public
Serializable
abstract class Encoding

线程安全
此类型的所有公共静态(Visual Basic 中为 Shared)成员对多线程操作而言都是安全的。但不保证任何实例成员是线程安全的。

备注
提供了将 Unicode 字符的数组和字符串与针对目标代码页编码的字节数组互相转换的方法。

System.Text 命名空间提供了若干 Encoding 实现,包括:

ASCIIEncoding 类将 Unicode 字符编码为单个的 7 位 ASCII 字符。此编码仅支持 U+0000 和 U+007F 之间的字符值。

UnicodeEncoding 类将每个 Unicode 字符编码为两个连续的字节。支持 Little-Endian(代码页 1200)和 Big-Endian(代码页 1201)字节顺序。

UTF7Encoding 类使用 UTF-7 编码(UTF-7 代表 7 位形式的 UCS 转换格式)对 Unicode 字符进行编码。此编码支持所有 Unicode 字符值,并且还可以按代码页 65000 访问。

UTF8Encoding 类使用 UTF-8 编码(UTF-8 代表 8 位形式的 UCS 转换格式)对 Unicode 字符进行编码。此编码支持所有 Unicode 字符值,并且还可以按代码页 65001 访问。

使用带有代码页或名称参数的 GetEncoding 方法来获取其他编码。

当要转换的数据仅在连续块(如从流中读取的数据)中可用时,应用程序可以使用 Decoder 或 Encoder 执行转换。这在数据量大到需要分成较小的块时也很有用。解码器和编码器通过 GetDecoder 和 GetEncoder 方法获取。应用程序可使用此类的属性(如 ASCII、Default、Unicode、UTF7 和 UTF8)来获取编码。应用程序可通过 ASCIIEncoding、UnicodeEncoding、UTF7Encoding 和 UTF8Encoding 类初始化 Encoding 对象的新实例。

通过编码,GetBytes 方法用于将 Unicode 字符数组转换为字节数组,而 GetChars 方法用于将字节数组转换为 Unicode 字符数组。GetBytes 和 GetChars 方法在转换之间不维护状态。

核心 GetBytes 和 GetChars 方法要求调用方提供目标缓冲区并确保该缓冲区大到足以容纳整个转换结果。应用程序可使用下列方法之一来计算目标缓冲区所需的大小。

可以用 GetByteCount 和 GetCharCount 方法计算某个特定转换结果的精确大小,然后就可以为该转换分配大小合适的缓冲区。
GetMaxByteCount 和 GetMaxCharCount 方法可用于计算给定字节或字符数的转换的最大大小,这样该大小的缓冲区就可以重复用于多次转换。
第一个方法通常使用较少的内存,而第二个方法通常执行得更快。

示例
[Visual Basic]
Imports System
Imports System.Text

Namespace Convert_Example
Class MyConvertExampleClass
Shared Sub Main()
Dim unicodeString As String = "This string contains the unicode character Pi(?"

' Create two different encodings.
Dim ascii As Encoding = Encoding.ASCII
Dim [unicode] As Encoding = Encoding.Unicode

' Convert the string into a byte[].
Dim unicodeBytes As Byte() = [unicode].GetBytes(unicodeString)

' Perform the conversion from one encoding to the other.
Dim asciiBytes As Byte() = Encoding.Convert([unicode], ascii, unicodeBytes)

' Convert the new byte[] into a char[] and then into a string.
' This is a slightly different approach to converting to illustrate
' the use of GetCharCount/GetChars.
Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)) As Char
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)

' Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString)
Console.WriteLine("Ascii converted string: {0}", asciiString)
End Sub
End Class
End Namespace

[C#]
using System;
using System.Text;

namespace ConvertExample
{
class ConvertExampleClass
{
static void Main()
{
string unicodeString = "This string contains the unicode character Pi(\u03a0)";

// Create two different encodings.
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;

// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString);

// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);

// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);

// Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString);
Console.WriteLine("Ascii converted string: {0}", asciiString);
}
}
}

[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;

int main()
{
String* unicodeString = S"This String* contains the unicode character Pi(\u03a0)";

// Create two different encodings.
Encoding * ascii = Encoding::ASCII;
Encoding * unicode = Encoding::Unicode;

// Convert the String* into a Byte->Item[].
Byte unicodeBytes[] = unicode -> GetBytes(unicodeString);

// Perform the conversion from one encoding to the other.
Byte asciiBytes[] = Encoding::Convert(unicode, ascii, unicodeBytes);

// Convert the new Byte into[] a char and[] then into a String*.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
Char asciiChars[] = new Char[ascii -> GetCharCount(asciiBytes, 0, asciiBytes -> Length)];
ascii -> GetChars(asciiBytes, 0, asciiBytes->Length, asciiChars, 0);
String* asciiString = new String(asciiChars);

// Display the strings created before and after the conversion.
Console::WriteLine(S"Original String*: {0}", unicodeString);
Console::WriteLine(S"Ascii converted String*: {0}", asciiString);
}

[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 。

要求
命名空间: System.Text

平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版 - Windows CE .NET

程序集: Mscorlib (在 Mscorlib.dll 中)

wangdequan1024 2004-10-14
  • 打赏
  • 举报
回复
string tmp = "";
byte[] tmpBy = System.Text.Encoding.UTF8.GetBytes(tmp);;
byte[] newBy=System.Text.Encoding.Convert(System.Text.Encoding.UTF8,System.Text.Encoding.Default,tmpBy);

16,705

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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