string和byte[]的转换 (C#)
来源:互联网 发布:java五子棋源代码 编辑:程序博客网 时间:2024/06/04 19:33
string类型转成byte[]: 反过来,byte[]转成string: string类型转成ASCII byte[]:("01" 转成 byte[] = new byte[]{ 0x30, 0x31}) ASCII byte[] 转成string:(byte[] = new byte[]{ 0x30, 0x31} 转成 "01") 有时候还有这样一些需求: byte[] 转成原16进制格式的string,例如0xae00cf, 转换成 "ae00cf";new byte[]{ 0x30, 0x31}转成"3031":
其它编码方式的,如System.Text.UTF8Encoding,System.Text.UnicodeEncoding class等;例如:
{
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}:
{
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;
}
- string和byte[]的转换 (C#)
- string和byte[]的转换 (C#)
- (C#) string和byte[]的转换
- string和byte[]的转换 (C#)
- string和byte[]的转换 (C#)
- string和byte[]的转换 (C#)
- (转) string和byte[]的转换 (C#)
- string和byte[]的转换 (C#)
- string和byte[]的转换 (C#)
- string和byte[]之间的转换 (C#)
- string和byte[]的转换 (C#)
- string和byte[]的转换 (C#)
- string和byte[]的转换 (C#)
- string和byte[]的转换 (C#)
- string和byte[]的转换 (C#)
- string和byte[]的转换 (C#)
- string和byte[]的转换 (C#)
- string和byte[]的转换 (C#)
- vim 改变窗口的大小
- 从一个小功能看软件开发过程
- tornado 自动加载(autoreload)
- 为JAVA性能而设计(2)
- jQuery 属性操作方法
- string和byte[]的转换 (C#)
- 合并byte[]字节数组
- Qt 2D绘图(六)坐标系统
- jQuery CSS 操作函数
- 服务器header返回状态码详解
- 编译ffmpeg for ios
- request 生命周期及参数信息
- Axis + Tomcat + Eclipse 发布 WebService
- 获取当前选中的值