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 ...
随机推荐
- mysql/MariaDB 搭建后创建密码及开启远程
创建密码: mysqladmin -u root -p password 123 或 MariaDB [(none)]> use mysql MariaDB [mysql]> update ...
- ROS Create a Catkin Workspace
Step1 : First, create the top level catkin workspace directory and a sub-directory named src (pronou ...
- 360网站卫士SQL注入绕过案例一个
不要以为用了360就可以高枕无忧,直接在netcraft的site_report中找到源站服务器IP,直接SQL脱裤,甚至可获取服务器权限. 存在漏洞的网站: 手工测试存在注入点: 但是网站有360保 ...
- bzoj1818 [Cqoi2010]内部白点
Description 无限大正方形网格里有n个黑色的顶点,所有其他顶点都是白色的(网格的顶点即坐标为整数的点,又称整点).每秒钟,所有内部白点同时变黑,直到不存在内部白点为止.你的任务是统计最后网格 ...
- SSD 单发多框检测
其实现在用的最多的是faster rcnn,等下再弄项目~~~ 图像经过基础网络块,三个减半模块,每个减半模块由两个二维卷积层,加一个maxPool减半(通道数依次增加[16,32,64]) 然后是多 ...
- CF311E Biologist
嘟嘟嘟 很显然是一道最小割模型. 做完几道题后.图的大概就能想出来了: 1.对于每一个动物,如果是0,就和s连一条边,否则向t连一条边. 2.对于每一个任务,题中要求最大利润,可以转化成最小损失. ( ...
- 19、配置嵌入式servlet容器(下)
使用外置的Servlet 嵌入式Servlet容器:应用打成可执行的j ar 优点:简单.便携: 缺点:默认不支持JSP.优化定制比较复杂 使用定制器[ServerProperti ...
- 4springboot:日志(下)
1.指定配置 位置: 给类路径下放上每个日志框架自己的配置文件即可: SpringBoot就不使用他默认配置的了 使用什么日志则配置什么文件以及注意文件名 自定义: <?xml version ...
- MFC中用户自定义类响应自定义消息
这篇技术文章不是讨论经典的MFC中的消息工作机理的,讨论消息工作原理.方式和路径的文章在网上和书本中随处可见.网上众多的讨论都是关于如何响应并进行用户自定义消息映射的:网上还有一些文章介绍如何在自定义 ...
- 关于JavaScript中省略元素对数组长度的影响
在学习<JavaScript权威指南>第六版的第7.1节中通过数组直接量创建数组时,我们可以不给数组的某个元素赋值,它就会使undefined.虽然是undefined,但我们调用数组对象 ...