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 ...
随机推荐
- [问题记录]libpomelo编译报错:ssize_t重定义
1. 时间:2015/01/16 描述:添加libpomelo到cocos2dx项目,报错如下图所示: 解决: 修改代码,源代码: #if !defined(_SSIZE_T_) && ...
- linux防火墙放行了端口,但是仍然访问不到
我们的防火墙默认规则如下: 如果防火墙放行了端口,但是仍然访问不到的话,可能是因为添加规则的时候,用的是iptables -A 选项,这样,增加的规则会排列在 第6条 规则后面,虽然service i ...
- redis知识树
- 全新Chrome Devtool Performance使用指南
运行时性能表现(runtime performance)指的是当你的页面在浏览器运行时的性能表现,而不是在下载页面的时候的表现.这篇指南将会告诉你怎么用Chrome DevToos Performan ...
- NGUI UILabel文字宽度和 UITweener
做个记录 方便别人和自己以后查找. NGUI UILabel 文字宽度 高度 mLabel.GetComponent<UILabel>().getLabWidth() mLabel ...
- mysql配置远程登录
1.vim /etc/my.cnf注释这一行:bind-address=127.0.0.1 ==> #bind-address=127.0.0.1 2.重启服务:sudo service mys ...
- BZOJ2337:[HNOI2011]XOR和路径(高斯消元)
Description 给定一个无向连通图,其节点编号为 1 到 N,其边的权值为非负整数.试求出一条从 1 号节点到 N 号节点的路径,使得该路径上经过的边的权值的“XOR 和”最大.该路径可以重复 ...
- springmvc(3)注解
有疑问可以参考博主其他关于spring mvc的博文 此时直接进行代码的实现 一般的步骤: -加入jar包 -配置DispatcherServlet -加入Spring MVC配置文件 -编写请求的处 ...
- ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室 实战系列(内容已过期,阅读请慎重)
项目简介 利用ASP.NET SignalR技术与Layim前端im框架实现的一个简单的web聊天室,包括单聊,群聊,加好友,加群,好友搜索,管理,群组管理,好友权限设置等功能.涉及技术: Elast ...
- Windows 下制作CentOS7安装U盘
本文属于另类的U盘制作方法(更多U盘安装见U盘安装CentOS ),如何安装CentOS,请参考<安装指南> 以下列出了,完整的制作步骤: 1.下载安装镜像 选择一个合适的镜像网站,比如网 ...