C#字符串与 byte数据的互相转换
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#字符串与 byte数据的互相转换的更多相关文章
- python学习笔记08-字符串
字符串是用单引号或者双引号引起来来的 单引号和双引号没有什么区别 1字符串支持乘法操作 >>> print('hello'*2) hellohello >>> 2 ...
- 【Python全栈-JavaScript】JavaScript-字符串详解
JavaScript-字符串详解 预热:Number() 方法 <script> //重要等级 1,2,3,4,5 var s=10; //最高级别5 var s1=new Number( ...
- TJI读书笔记17-字符串
TJI读书笔记17-字符串 不可变的String 重载”+”和StringBuilder toString()方法的一个坑 String上的操作 格式化输出 Formatter类 字符串操作可能是计算 ...
- java学习笔记05--字符串 .
java学习笔记05--字符串 . 一.String类 由字符所组成的一串文字符号被称之为字符串.在java中字符串不仅仅是字符数组,而且是String类的一个实例,可以使用String类来构建. 字 ...
- 笔记-python-字符串格式化-format()
笔记-python-字符串格式化-format() 1. 简介 本文介绍了python 字符串格式化方法format()的常规使用方式. 2. 使用 2.1. Accessi ...
- shell编程系列2--字符串的处理
shell编程系列2--字符串的处理 字符串的处理 .计算字符串的长度 方法1 ${#string} 方法2 expr length "$string" (如果string中间有空 ...
- HDU-2017-字符串统计
/* Name: HDU-2017-字符串统计 Date: 18/04/17 20:19 Description: 水过 */ #include<bits/stdc++.h> using ...
- NYOJ--113--字符串替换
/* Name: NYOJ--113--字符串替换 Author: shen_渊 Date: 18/04/17 15:41 Description: 字符串水题,秒过 */ #include<b ...
- (replace find)nyoj113-字符串替换
113-字符串替换 内存限制:64MB 时间限制:3000ms 特判: No通过数:171 提交数:388 难度:2 题目描述: 编写一个程序实现将字符串中的所有"you"替换成& ...
随机推荐
- Django项目:CRM(客户关系管理系统)--64--54PerfectCRM实现CRM客户报名链接
# kingadmin.py # ————————04PerfectCRM实现King_admin注册功能———————— from crm import models #print("ki ...
- 洛谷P3749 [六省联考2017]寿司餐厅
传送门 题解 这几道都是上周llj讲的题,题解也写得十分好了,所以直接贴了几个链接和代码. //Achen #include<algorithm> #include<iostream ...
- get请求中文乱码
get请求时要将url进行encodeURI http://www.ruanyifeng.com/blog/2010/02/url_encoding.html http://www.ruanyifen ...
- ajax传输数据
AJAX向后台传输数//1 直接在url中传递 $.ajax({ type: "POST", url: "/testAjax/addUser?id=1&name= ...
- int 13h,磁盘中断
直接磁盘服务(Direct Disk Service——INT 13H) 00H —磁盘系统复位 01H —读取磁盘系统状态 02H —读扇区 03H —写扇区 04H —检验扇区 05H —格式化 ...
- SVN添加自动忽略文件.settings .project .classpath target等
eclipse svn提交忽略文件及文件夹,ignore设置无效.. 一.方法: 1. 将文件夹或文件从Eclipse中删除.记得要在Eclipse中删除,而不是Windows文件管理界面删除. 2. ...
- Leetcode106. Construct Binary Tree from Inorder and Postorder Traversal中序后续构造二叉树
根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9,15, ...
- Go之路一
一.声明变量 var a int var b string var c []float32 var d func() bool var e struct{ x int } 第1行,声明一个整型类型的变 ...
- Redis源码解析:23sentinel(四)故障转移流程
十:故障转移流程中的状态转换 当哨兵针对某个主节点进行故障转移时,该主节点的故障转移状态master->failover_state,要依次经历下面六个状态: SENTINEL_FAILOVER ...
- 洛谷P3300 城市规划
题意:给你一个6 * n的网格题,单点修改,询问区间联通块数.n <= 10w 解:看起来就很显然的一道题......线段树每个点用一个ufs维护连通性.完了. 我为了方便思考把图转成横着的了. ...