string和byte[]的转换 (C#)
string类型转成byte[]:

byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );反过来,byte[]转成string:

string str = System.Text.Encoding.Default.GetString ( byteArray );
其它编码方式的,如System.Text.UTF8Encoding,System.Text.UnicodeEncoding class等;例如:

string类型转成ASCII byte[]:("01" 转成 byte[] = new byte[]{ 0x30, 0x31})

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

string str = System.Text.Encoding.ASCII.GetString ( byteArray );
有时候还有这样一些需求:
byte[] 转成原16进制格式的string,例如0xae00cf, 转换成 "ae00cf";new byte[]{ 0x30, 0x31}转成"3031":

public static string ToHexString ( byte[] bytes ) // 0xae00cf => "AE00CF "
{
string hexString = string.Empty;
if ( bytes != null )
{
StringBuilder strB = new StringBuilder ();

for ( int i = 0; i < bytes.Length; i++ )
{
strB.Append ( bytes[i].ToString ( "X2" ) );
}
hexString = strB.ToString ();
}
return hexString;
}

反过来,16进制格式的string 转成byte[],例如, "ae00cf"转换成0xae00cf,长度缩减一半;"3031" 转成new byte[]{ 0x30, 0x31}:

public static byte[] GetBytes(string hexString, out int discarded)
{
discarded = 0;
string newString = "";
char c;
// remove all none A-F, 0-9, characters
for (int i=0; i<hexString.Length; i++)
{
c = hexString[i];
if (IsHexDigit(c))
newString += c;
else
discarded++;
}
// if odd number of characters, discard last character
if (newString.Length % 2 != 0)
{
discarded++;
newString = newString.Substring(0, newString.Length-1);
}

int byteLength = newString.Length / 2;
byte[] bytes = new byte[byteLength];
string hex;
int j = 0;
for (int i=0; i<bytes.Length; i++)
{
hex = new String(new Char[] {newString[j], newString[j+1]});
bytes[i] = HexToByte(hex);
j = j+2;
}
return bytes;
}

C​#​字​符​串​与​ ​b​y​t​e​数​据​的​互​相​转​换的更多相关文章

  1. python学习笔记08-字符串

    字符串是用单引号或者双引号引起来来的  单引号和双引号没有什么区别 1字符串支持乘法操作 >>> print('hello'*2) hellohello >>> 2 ...

  2. 【Python全栈-JavaScript】JavaScript-字符串详解

    JavaScript-字符串详解 预热:Number() 方法 <script> //重要等级 1,2,3,4,5 var s=10; //最高级别5 var s1=new Number( ...

  3. TJI读书笔记17-字符串

    TJI读书笔记17-字符串 不可变的String 重载”+”和StringBuilder toString()方法的一个坑 String上的操作 格式化输出 Formatter类 字符串操作可能是计算 ...

  4. java学习笔记05--字符串 .

    java学习笔记05--字符串 . 一.String类 由字符所组成的一串文字符号被称之为字符串.在java中字符串不仅仅是字符数组,而且是String类的一个实例,可以使用String类来构建. 字 ...

  5. 笔记-python-字符串格式化-format()

    笔记-python-字符串格式化-format() 1.      简介 本文介绍了python 字符串格式化方法format()的常规使用方式. 2.      使用 2.1.    Accessi ...

  6. shell编程系列2--字符串的处理

    shell编程系列2--字符串的处理 字符串的处理 .计算字符串的长度 方法1 ${#string} 方法2 expr length "$string" (如果string中间有空 ...

  7. HDU-2017-字符串统计

    /* Name: HDU-2017-字符串统计 Date: 18/04/17 20:19 Description: 水过 */ #include<bits/stdc++.h> using ...

  8. NYOJ--113--字符串替换

    /* Name: NYOJ--113--字符串替换 Author: shen_渊 Date: 18/04/17 15:41 Description: 字符串水题,秒过 */ #include<b ...

  9. (replace find)nyoj113-字符串替换

    113-字符串替换 内存限制:64MB 时间限制:3000ms 特判: No通过数:171 提交数:388 难度:2 题目描述: 编写一个程序实现将字符串中的所有"you"替换成& ...

随机推荐

  1. typescript使用小结

    1. typescript使得js在书写的过程中有了参数类型的限制在 传参的过程中变得严格,减少了不必要的错误的发生 2. tslint同时也兼备了一部分eslint的作用,在一定程度上我们使用tsl ...

  2. linux 编译C语言代码后产生OBJ文件的方法

    如果你不指定编译成什么文件,gcc默认一步到位,直接生成可执行文件你可以试试以下几个参数 -c 只激活预处理,编译,和汇编,也就是他只把程序做成obj文件 例子用法: gcc -c hello.c 他 ...

  3. Jeecms之JSP访问action类

    因为Jeecms采用spring容器管理类,在web容器加载的时候类都已经实例化好了.我们可以通过在JSP中访问spring上下文的方式来调用action业务类例: ApplicationContex ...

  4. OpenCASCADE动画功能

    OpenCASCADE动画功能 eryar@163.com 1.Introduction OpenCASCADE提供了类AIS_Animation等来实现简单的动画功能. 从其类图可以看出,动画功能有 ...

  5. 匿名/局部内部类访问局部变量时,为什么局部变量必须加final

    我们都知道方法中的匿名/局部内部类是能够访问同一个方法中的局部变量的,但是为什么局部变量要加上一个final呢? 首先我们来研究一下变量生命周期的问题,局部变量的生命周期是当该方法被调用时在栈中被创建 ...

  6. 2019-8-31-dotnet-如何调试某个文件是哪个代码创建

    title author date CreateTime categories dotnet 如何调试某个文件是哪个代码创建 lindexi 2019-08-31 16:55:58 +0800 201 ...

  7. Leetcode589.N-ary Tree Preorder TraversalN叉树的前序遍历

    给定一个 N 叉树,返回其节点值的前序遍历. class Node { public: int val; vector<Node*> children; Node() {} Node(in ...

  8. Leetcode448.Find All Numbers Disappeared in an Array找到所有数组中消失的数字

    给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能在不 ...

  9. 02Redis入门指南笔记(基本数据类型)

    一:热身 获得符合规则的健名列表:keys  pattern pattern支持glob风格的通配符,具体规则如下表: Redis命令不区分大小写.keys命令需要遍历Redis中的所有健,当键的数量 ...

  10. MySQL系列(十)--用户权限及远程访问

    本文基于MySQL8.0,记录一下完整的远程访问的过程,以及这个过程中可能遇到的问题,MySQL运行在阿里云服务器,操作系统:CentOS 7.6 64位 顺便说下,买服务器还是要双十二这种拉新活动再 ...