c#中的流,字节,字符,字符串
来源:互联网 发布:java基础笔记 编辑:程序博客网 时间:2024/06/06 02:57
首先要明白它们本身是由什么组成的:
流:二进制
字节:无符号整数
字符:Unicode编码字符
字符串:多个Unicode编码字符
那么在.net下它们之间如何转化呢?
一般是遵守以下规则:
流->字节数组->字符数组->字符串
下面就来具体谈谈转化的语法
流->字节数组
MemoryStream ms = new MemoryStream();
byte[] buffer = new byte[ms.Length];
ms.Read(buffer, 0, (int)ms.Length);
字节数组->流
byte[] buffer = new byte[10];
MemoryStream ms = new MemoryStream(buffer);
字节数组->字符数组
1.
byte[] buffer = new byte[10];
char[] ch = new ASCIIEncoding().GetChars(buffer);
//或者:char[] ch = Encoding.UTF8.GetChars(buffer)
2.
byte[] buffer = new byte[10];
char[] ch = new char[10];
for(int i=0; i<buffer.Length; i++)
{
ch[i] = Convert.ToChar(buffer[i]);
}
字符数组->字节数组
1.
char[] ch = new char[10];
byte[] buffer = new ASCIIEncoding().GetBytes(ch);
//或者:byte[] buffer = Encoding.UTF8.GetBytes(ch)
2.
char[] ch = new char[10];
byte[] buffer = new byte[10];
for(int i=0; i<ch.Length; i++)
{
buffer[i] = Convert.ToByte(ch[i]);
}
字符数组->字符串
char[] ch = new char[10];
string str = new string(ch);
字符串->字符数组
string str = "abcde";
char[] ch=str .ToCharArray();
字节数组->字符串
byte[] buffer = new byte[10];
string str = System.Text.Encoding.UTF8.GetString(buffer);
//或者:string str = new ASCIIEncoding().GetString(buffer);
字符串->字节数组
string str = "abcde";
byte[] buffer=System.Text.Encoding.UTF8.GetBytes(str);
//或者:byte[] buffer= new ASCIIEncoding().GetBytes(str);
说明:主要就是用到了Convert类和System.Text命名空间下的类,Encoding是静态类,ASCIIEncoding是实体类,方法都是一样的!
- c#中的流,字节,字符,字符串
- c#中的流,字节,字符,字符串
- c#中的流,字节,字符,字符串
- C#中的流、字节、字符和字符串
- C#中的流、字节、字符和字符串
- C#中字节、字符、字符串的区别
- Win32中的字符串(位,字符,字节,编码)
- C# 去除字符串中的'\'字符
- C#中的字符串截取(字节数)
- C# 流 字符串 字节转换
- C#中根据字节处理字符串(含中文字符)
- 字符,字节,字符串
- Java 字节 字符 字符串
- Java 字节 字符 字符串
- 字符串、字符、字节
- java 字节,字符,字符串
- 字符串,字符和字节
- 字符串、字符、字节
- ActionError,ActionMessage介绍
- vc++ greta html转 txt
- jsp2.0新特性
- hbase集群配置
- Android Stream to String
- c#中的流,字节,字符,字符串
- oracle跨平台移动
- HibernateTemplate相关方法
- Linux常用基本命令及应用技巧
- 设计模式之责任链模式
- C++ using namespace std 详解
- XCode 3 中我的常用快捷键
- javascript 未结束的字符串常量
- Hibernate中Query.uniqueResult();