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

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

急!

在线等待。

谢谢!
...全文
979 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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
net的最近面试经典试题ASP.NET面试题集合 1. 简述 private、 protected、 public、 internal 修饰符的访问权限。 答 . private : 私有成员, 在类的内部才可以访问。 protected : 保护成员,该类内部和继承类可以访问。 public : 公共成员,完全公开,没有访问限制。 internal: 在同一命名空间内可以访问。 2 .列举ASP.NET 页面之间传递值的几种方式。 答. 1.使用QueryString, 如....?id=1; response. Redirect().... 2.使用Session变量 3.使用Server.Transfer 3. 一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。 答:public class MainClass { public static void Main() { Console.WriteLine(Foo(30)); } public static int Foo(int i) { if (i 0 && i <= 2) return 1; else return Foo(i -1) + Foo(i - 2); } } 4.C#的委托是什么?事件是不是一种委托? 答 : 委托可以把一个方法作为参数代入另一个方法。 委托可以理解为指向一个函数的引用。 是,是一种特殊的委托 5.override与重载的区别 答 : override 与重载的区别。重载是方法的名称相同。参数或参数类型不同,进行多次重载以适应不同的需要 Override 是进行基类函数的重写。为了适应需要。 6.如果在一个B/S结构的系统需要传递变量值,但是又不能使用Session、Cookie、Application,您有几种方法进行处理? 答 : this.Server.Transfer 7.请编程遍历页面上所有TextBox控件并给它赋值为string.Empty? 答: foreach (System.Windows.Forms.Control control in this.Controls) { if (control is System.Windows.Forms.TextBox) { System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)control ; tb.Text = String.Empty ; } } 8.请编程实现一个冒泡排序算法? 答: int [] array = new int ; int temp = 0 ; for (int i = 0 ; i < array.Length - 1 ; i++) { for (int j = i + 1 ; j < array.Length ; j++) { if (array[j] < array) { temp = array ; array = array[j] ; array[j] = temp ; } } } 9.描述一下C#索引器的实现过程,是否只能根据数字进行索引? 答:不是。可以用任意类型。 10.求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m [Page] 答: int Num = this.TextBox1.Text.ToString() ; int Sum = 0 ; for (int i = 0 ; i < Num + 1 ; i++) { if((i%2) == 1) { Sum += i ; } else { Sum = Sum - I ; } } System.Console.WriteLine(Sum.ToString()); System.Console.ReadLine() ; 11.用.net做B/S结构的系统,您是用几层结构来开发,每一层之间的关系以及为什么要这样分层? 答:一般为3层 数据访问层,业务层,表示层。 数据访问层对数据库进行增删查改。 业务层一般分为二层,业务表观层实现与表示层的沟通,业务规则层实现用户密码的安全等。 表示层为了与用户交互例如用户添加表单。 优点: 分工明确,条理清晰,易于调试,而且具有可扩展性。 缺点: 增加成本。 12.在下面的例子里 using Sy

62,244

社区成员

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

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

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

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