在Asp.Net中如何将带有中文字符的字符串转换成byte数组?

ChonglingFan 2003-07-09 09:09:57
在Asp.Net中如何将带有中文字符的字符串转换成byte数组?

急!

在线等待。

谢谢!
...全文
962 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
aspnetwuxueyou 2003-07-09
  • 打赏
  • 举报
回复
Use Encoding.GetChars() to convert from byte[] to char[]

May you good luck
aspnetwuxueyou 2003-07-09
  • 打赏
  • 举报
回复
refer to

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);
}
}
}
ChonglingFan 2003-07-09
  • 打赏
  • 举报
回复
非常感谢您如此热心帮助!

不好意思,我该如何字符串将转换出来后byte数组中的某位或某几位转换成字符呢?

我用以下方法转换
strFirstChar = System.Convert.ToString(arrBytes(0))
For i = 0 To 10
arrLineData(0) += System.Convert.ToString(arrLineData(i))
Next
最后生成结果依然是byte数字。

不好意思。
tix66 2003-07-09
  • 打赏
  • 举报
回复
经验总结--字符串与编码:
首先应该把字节数组看成是String的载体。
dot Net使用的字符串String是Unicode编码的;它也是以Unicode编码的形式显示字符串。

以下是用自己语言对几个常用函数的说明:
(自己总结的,反正看不明MSDN)
bytes=System.Text.Encoding.Unicode.GetBytes(str)
作用:把str的载体作Unicode->Unicode的编码转换--也就是没有对载体作任何的转换。因些使用此函数可以得代表该String载体的字节数组。
str=System.Text.Encoding.Unicode.GetString(bytes)
作用:对字节数组作Unicode->Unicode的编码转换--即没有转换,把经过转换后的字节数组作为str的载体。

bytes=System.Text.Encoding.Utf8.GetBytes(str)
作用:把str的载体作Utf8->Unicode的编码转换。返回的是经过转换后的字符数组
str=System.Text.Encoding.Utf8.GetString(bytes)
作用:对字节数组作Gb2312->Unicode的编码转换,把经过转换后的字节数组作为str的载体。

bytes=System.Text.Encoding.GetEncoding("GB2312").GetBytes(str)
作用:把str的载体作Gb2312->Unicode的编码转换。返回的是经过转换后的字符数组
str=System.Text.Encoding.GetEncoding("GB2312").GetString(bytes)
作用:对字节数组作Gb2312->Unicode的编码转换,把经过转换后的字节数组作为str的载体。

如此类推
bytes=System.Text.Encoding.GetEncoding("XXX").GetBytes(str)
作用:把str的载体作XXX->Unicode的编码转换。返回的是经过转换后的字符数组
str=System.Text.Encoding.GetEncoding("XXX").GetString(bytes)
作用:对字节数组作XXX->Unicode的编码转换,把经过转换后的字节数组作为str的载体。

这里是我收集的一些有关字符编码的资料:http://61.145.116.154/bm/
还有:
http://www.unicode.org/charts/unihan.html
根据Unicode编码查其对应字符的字形、Utf8、汉字区位码等
http://www.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/GB/GB2312.TXT Unicode与Gb2312的对照表
http://www.sun.com/developers/gadc/technicalpublications/articles/mabiao.txt
Unicode与Gbk对照表
例子1:得到各种编码的字节数组

对于“测试”,查表得
Unicode: 75 109 | 213 139
(16进制:) 4B 6D D5 8B
Utf8: 230 181 139 | 232 175 149
(16进制:) E6 B5 8B E8 AF 95
Gbk: 178 226 | 202 212
(16进制:) B2 E2 CA D4
Gb2312: 50 98 | 74 84

Imports System.Text

Dim str As String = "测试"
Dim Bytes As Byte()
Bytes = Encoding.Unicode.GetBytes(str)
'Bytes: 75 109 213 139
Bytes = Encoding.UTF8.GetBytes(str)
'Bytes: 230 181 139 232 175 149
Bytes = Encoding.GetEncoding("Gb2312").GetBytes(str)
'Bytes: 178 226 202 212 <-为什么不是50 98 74 84呢?搞不清~~
Bytes = Encoding.Default.GetBytes(str)
'Bytes: 178 226 202 212
例子2:把Utf8编码的字符串转为Unicode编码的字符串

Dim Bytes As Byte() = {230, 181, 139, 232, 175, 149}
Dim str As String = Encoding.Unicode.GetString(Bytes)
'str的载体为“测试”的Utf8编码。以Unicode的形式显示为“뗦閯”
'查编码表,230 181 139 232 175 149刚好就是“뗦閯”的Unicode码
Bytes = Encoding.Unicode.GetBytes(str)
'Bytes: 230 181 139 232 175 149,跟原来一样,没有变化
str = Encoding.UTF8.GetString(Bytes)
'str:“测试”
例子3:
(参考http://expert.csdn.net/Expert/topic/1861/1861857.xml?temp=.558407)
对于“个”,查表得:
Unicode: 42 78
Utf8: 228 184 170
Gbk: 184 246

Dim s As String = "个"
Dim b As Byte()
b = Encoding.Utf8.GetBytes(s)
'把42 78作Unicode->Utf8的转换b:228 184 170
s = Encoding.Default.GetString(b)
'把228 184 170作Gb->Unicode的转换。s:"涓" ("涓"的Unicode编码为:147 109)
b = Encoding.Unicode.GetBytes(s)
'此时s的载体为147 109 0 0 <--问题已经出现了
b = Encoding.Default.GetBytes(s)
'把s的载体147 109 0 0作Unicode->Gb的编码转换b:228 184 0
s = Encoding.UTF8.GetString(b)
'把228 184 0作Utf8->Unicode的编码转换。s=""
b = Encoding.Unicode.GetBytes(s) 'b(0)=0 b(1)=0
'此时s的载体为0 0

例子说明:字符串经过UnicodeToUtf8->GbToUnicode->UnicodeToGb->Utf8ToUnicode这样的转换过程,理所当然地认为最后得到的应该是原来的字符串。然而有些情况却不是(例如这例子)。
原因:Gb字符串是可以转换成Unicode编码的(对Gb中不存在的Uicode字符会以“?”代替),但前提是要进行这种转换的字符串必须是Gb编码。对于上面的
“把228 184 170作Gb->Unicode的转换”,而这228 184 170却是“个”的Utf8编码,所以转换时有数据丢失了。
ChonglingFan 2003-07-09
  • 打赏
  • 举报
回复
thanks a lot to two friends

62,075

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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