C#  byte 和 char 可以认为是等价的。但是在文本显示的时候有差异。

char 占两个字节,unicode字符

1、内存转换:

  • char转化为byte:
public static byte[] charToByte(char c) {
byte[] b = new byte[];
b[] = (byte) ((c & 0xFF00) >> );
b[] = (byte) (c & 0xFF);
return b;
}
  • byte转换为char:
 public static char byteToChar(byte[] b) {
char c = (char) (((b[] & 0xFF) << ) | (b[] & 0xFF));
return c;
}

2、字符串转换

char[]转化为byte[]:

char[] cChar=new char[]{a,b,c,d,e};
byte[] byteData=Encoding.Default.GetBytes(cChar);

byte[]转化为char[]:

byte[] byteData=new byte[]{0x01,0x02,0x03,0x04,0x05};
char[] cChar=Encoding.ASCII.GetChars(byteData);

string类型转成byte[]:

byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );

byte[]转成string:

string str = System.Text.Encoding.Default.GetString ( byteArray );

string类型转成ASCII byte[]:

byte[] = new byte[]{ 0x30,0x31};    // "01"
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str );

ASCIIbyte[]转成string:

byte[] = new byte[]{ 0x30, 0x31};  //  "01"
string str = System.Text.Encoding.ASCII.GetString ( byteArray );

string 转换成 Char[]
  

string ss = "我爱你,中国";
char[] cc = ss.ToCharArray();

Char[] 转换成string
  

string s = new string(cc);

byte[] 与 string 之间的装换
  

byte[] bb = Encoding.UTF8.GetBytes(ss);
string s = Encoding.UTF8.GetString(bb);

char[] byte[] string的更多相关文章

  1. 对bit、byte、TByte、Char、string、进制的认识

    在学校老师就教1byte = 8bit,一个Byte在内存中占8个房间.每个房间都有门牌号.找到内存中的内容就找门牌号,寻址什么的,虽然在听,但是脑袋里一头雾水,到现在只知道会用就行,但原理也不是那么 ...

  2. Java之byte、char和String类型相互转换

    package basictype; /** * byte.char和String类型相互转换 */ public class CHJavaType { public static void main ...

  3. C#中char[]与string之间的转换;byte[]与string之间的转化

    目录 1.char[]与string之间的转换 2.byte[]与string之间的转化 1.char[]与string之间的转换 //string 转换成 Char[] string str=&qu ...

  4. 探究 C# 中的 char 、 string(一)

    目录 探究 C# 中的 char . string(一) 1. System.Char 字符 2. 字符处理 3. 全球化 4. System.String 字符串 4.1 字符串搜索 4.2 字符串 ...

  5. Cstring转char、string、int等数据类型的方法(转载)

    Cstring转char.string.int等数据类型的方法 (-- ::) 转载 标签: 杂谈 分类: VC CString 转char * CString cstr; char *p = (LP ...

  6. string to char* and char* to string 玩转 String 和 Char*

    char 类型是c语言中常见的一个数据类型,string是c++中的一个,它的定义为 Strings are objects that represent sequences of character ...

  7. 【C++】int、const char*、char*、char、string之间的转换

    #include "stdafx.h" #include<string> #include<vector> #include<iostream> ...

  8. C++ 中int,char,string,CString类型转换

      1. c++中string到int的转换 1) 在C标准库里面,使用atoi: #include <cstdlib> #include <string> std::stri ...

  9. C++ char*,const char*,string的相互转换

    1. string转const char* string s ="abc";constchar* c_s = s.c_str(); 2. const char*转string   ...

随机推荐

  1. Fiddler抓包工具如何可以抓取HTTPS

  2. Python-12-装饰器

    一.定义 器即函数 装饰即修饰,意指为其他函数添加新功能 装饰器定义:本质就是函数,功能是为其他函数添加新功能 原则: 1.不修改被装饰函数的源代码(开放封闭原则) 2.为被装饰函数添加新功能后,不修 ...

  3. TensorFlow学习笔记(1)—— 基本概念与框架

    入门框架时的常见问题 学习框架的原因? 方便.易用 学习框架的哪些知识点? 掌握一个项目的基本流程,就知道需要学习哪些知识点了 迅速学习框架的方法 根据项目每块流程的需要针对性的学 可以看官方的入门教 ...

  4. linux系统调整磁盘分区

    xfs分区格式调整分区大小 调整前备份: mkdir /tmp/home cp -r /home/* /tmp/home/ umount /home 卸载时报错有占用 fuser -m -v -i - ...

  5. Ali-Tomcat 安装

    通过在 Eclipse 安装 Tomcat4e 插件,或者在 Intellij Idea 安装配置 Ali-tomcat,可以快 速方便地启动并调试基于 EDAS 服务化框架 HSF 开发的应用. 1 ...

  6. ax 2012批处理不运行问题

    最近在开发12的批处理,但是很奇怪所有的都配置好了就是不跑批处理,假如你也出现了那用下面的方法试试: 12的批处理和09不一样,不是运行x++代码,而且运行你CIL生成的DLL文件, 1.你必须让你的 ...

  7. Python3基础语法(20190617)

    字符串 字符串是以单引号'或双引号"括起来的任意文本,比如'abc',"xyz"等等.请注意,''或""本身只是一种表示方式,不是字符串的一部分,因此 ...

  8. ES6之reduce和reduceRight方法应用实例

    for循环是最基本的遍历循环,但是有些时候并不是很实用,且效率和性能较低,故本文列举出工作学习中碰到的reduce方法应用实例,供自己揣摩熟练应用,以提高自己的研发水平和研发效率. reduce方法( ...

  9. JSP 9大隐式对象和四个作用域的范围

    Java中 九大隐式对象说明 输入/输出对象:  request   response   out 作用域通信对象: session  application  pageContext Servlet ...

  10. Bad state: Stream has already been listened to.

    https://stackoverflow.com/questions/51396769/flutter-bad-state-stream-has-already-been-listened-to T ...