C# string 转 byte[]
string 转 byte[]
/// <summary>
/// string 转 byte
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
byte[] Str_change_byte(string str)
{
byte[] Data = new byte[str.Length];
int count = ;
string strSource = str.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", ""); try
{
for (int i = ; i < (strSource.Length - strSource.Length % ) / ; i++)//取余3运算作用是防止用户输入的字符为奇数个
{
Data[count] = Convert.ToByte(strSource.Substring(i * , ), );
count++;
}
if (strSource.Length % != )//剩下一位单独处理
{
Data[count] = Convert.ToByte(strSource.Substring(strSource.Length - , ), );//单独处理B(0B)
count++;
}
}
catch (Exception)
{
MessageBox.Show("Error:数据包含非法字符\n", "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error);//出错提示
return null;
}
byte[] buf = new byte[count];
Array.Copy(Data, , buf, , count); //复制原始数据
return buf;
}
string 转 ASCII
public static byte[] str2ASCII(String xmlStr)
{
return Encoding.Default.GetBytes(xmlStr);
}
以下内容为转载:
https://www.cnblogs.com/Maxq/p/5953682.html
string类型转成byte[]:
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );
byte[]转成string:
string str = System.Text.Encoding.Default.GetString ( byteArray );
string类型转成ASCII byte[]:
("01" 转成 byte[] = new byte[]{ 0x30,0x31})
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str );
ASCIIbyte[]转成string:
(byte[] = new byte[]{ 0x30, 0x31} 转成"01")
string str = System.Text.Encoding.ASCII.GetString ( byteArray );
byte[]转16进制格式string:
new byte[]{ 0x30, 0x31}转成"3031":
publicstaticstring ToHexString ( byte[] bytes ) // 0xae00cf => "AE00CF "
{
string hexString = string.Empty;
if ( bytes != null )
{
StringBuilder strB = new StringBuilder ();
for ( int i = ; i < bytes.Length; i++ )
{
strB.Append ( bytes[i].ToString ( "X2" ) );
}
hexString = strB.ToString ();
}return hexString;
}
16进制格式string 转byte[]:
publicstaticbyte[] GetBytes(string hexString, outint discarded)
{
discarded = ;
string newString = "";
char c;// remove all none A-F, 0-9, charactersfor (int i=0; i<hexString.Length; i++)
{
c = hexString[i];if (IsHexDigit(c))
newString += c;
else
discarded++;
}// if odd number of characters, discard last characterif (newString.Length % 2 != 0){ discarded++;
newString = newString.Substring(, newString.Length-); }
int byteLength = newString.Length / ;byte[] bytes = newbyte[byteLength];string hex;int j = ;for (int i=; i<bytes.Length; i++){
hex = new String(new Char[] {newString[j], newString[j+]});
bytes[i] = HexToByte(hex); j = j+;
}
return bytes;
}
C# string 转 byte[]的更多相关文章
- string转byte[]
static byte[] GetBytes(string str) { byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buff ...
- java String与Byte[]和String 与InputStream转换时注意编码问题。。。
前一段日子,我在做rsa加密和通过http get方式获取验证码图片通过BitmapFactory创建bitmap 出现了一系列的问题. 通过一系列的调试,发现有些问题原来是在进行String 与By ...
- java中string与byte[]的转换
1.string 转 byte[] byte[] midbytes=isoString.getBytes("UTF8"); //为UTF8编码 byte[] isoret = sr ...
- C# double float int string 与 byte数组 相互转化
在做通信编程的时候,数据发送多采用串行发送方法,实际处理的时候多是以字节为单位进行处理的.在C/C++中 多字节变量与Byte进行转化时候比较方便 采用UNION即可废话少说看示例:typedef u ...
- java Byte.toString 方法与String.ValueOf(Byte)效率比较
int times = 10000000; Byte[] li = new Byte[times]; for (int i = 0; i < times; i++) { li[i] = (byt ...
- C# String 与 byte 互转
String转换为byte数组用byte[] arr = System.Text.Encoding.Default.GetBytes("abcde") byte数组转换为Strin ...
- string和byte[]的转换 (C#)
原文 string和byte[]的转换 (C#) string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ...
- C#中string和byte[]相互转换问题解决
本来想讲string转换为byte数组,通过在VS上打 ‘str. “来找,结果半天没发现跳出来的函数中有想要的,哭瞎 /(ㄒoㄒ)/~~ 这回将两种情况都记下来了.... string ---> ...
- Go中string转[]byte的陷阱
Go中string转[]byte的陷阱html {overflow-x: initial !important;}#write, body { height: auto; }#write, #writ ...
- golang string和[]byte的对比
golang string和[]byte的对比 为啥string和[]byte类型转换需要一定的代价?为啥内置函数copy会有一种特殊情况copy(dst []byte, src string) in ...
随机推荐
- AutoHotkey使用Excel的Com对象可能导致进程残留问题的原因及解决方案
在AutoHotkey脚本中,对Excel的应用体验很不错,xl := ComObjActive("Excel.Application")就和当前Excel表连接了, 通过xl变量 ...
- 第一周 day1 Python学习笔记
为什么要学习Python? Python擅长的领域 1. python2.x中不支持中文编码,默认编码格式为ASCII码,而python3.x中支持Unicode编码,支持中文,变量名可以为中文,如: ...
- aop的概念以及 cglib-nodep-2.1_3.jar第三方jia包动态代理使用
引入 cglib-nodep-2.1_3.ja包 cglib产生的代理类是目标类的子类 定义接口,让切面都继承它,方便加入到动态代理方法 的那个类中使用 在SalaryInterceptor类中使用 ...
- CALayer & bitmap Content
Working with High-Resolution Images Layers do not have any inherent knowledge of the resolution of t ...
- [19/03/25-星期一] 容器_Collection(集合、容器)之Set(集合、安置,无顺序不可重复)
一.概念&方法 Set接口继承自Collection,Set接口中没有新增方法,方法和Collection保持完全一致.. Set容器特点:无序.不可重复.无序指Set中的元素没有索引,只能遍 ...
- 富文本使用之wangEditor3
一.介绍: wangEditor —— 轻量级 web 富文本编辑器,配置方便,使用简单.支持 IE10+ 浏览器. 二.使用方式: 直接下载:https://github.com/wangfupen ...
- 【洛谷P2123】皇后游戏
题目链接 这题的 实际上和"流水调度问题"是一样的 (我是不会告诉你我是看了讨论才知道的) 于是我就翻开了我们教练弄来的一本蓝不拉几的叫做"信息学奥赛一本通·提高篇&qu ...
- Google Guava -缓存cache简单使用
package guavacache; import java.util.concurrent.ExecutionException; import java.util.concurrent.Time ...
- Linq 表达
Lambda 简单了解 //Lambda //匿名方法 //delegate (Student s) { return s.Age > 12 && s.Age < 20; ...
- sharepoint2016安装OOS,OOS场负载均衡
Office Online Server is the successor to Office Web Apps Server. It may be connected to SharePoint, ...