荣格公司产品价格表:string和byte[]的转换 (C#)
来源:百度文库 编辑:偶看新闻 时间:2024/04/30 02:33:41
string和byte[]的转换 (C#)
2008-04-09 10:40 by Mainz, 13713 visits, 网摘, 收藏, 编辑 string类型转成byte[]:反过来,byte[]转成string:
其它编码方式的,如System.Text.UTF8Encoding,System.Text.UnicodeEncoding class等;例如:
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":
{
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
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
hex = new String(new Char[] {newString[j], newString[j+1]});
bytes[i] = HexToByte(hex);
j = j+2;
}
return bytes;
}
关于C#中string和Byte的转换问题
.net中使用c#对byte[]到字符串的转换
能不能把BufferedImage类型转换成String或者byte
C#中STRING 怎么转换成 LONG ?
c# 里面 String 和string有什么区别?
C#中String和string有什么区别?详细点
c#里string 与char数组怎么转换
C#中的Byte类型的数组怎样初始化?
String 转换
C#中的string 不是引用型的吗?
在C#中如何将STRING型数据强制转换为INT型?
在C#中如何将INT型数据强制转换为STRING型?
C#里string类型变量强制类型转换成int,怎么写?
char和string的区别
StringBuffer 和string 的区别
thread和string的区别?
c# 怎样实行 字符串和ASCII码 的转换
StringBuffer对象和String对象如何互相转换?
有一个BYTE[ 2]的数组,BYTE[0]=0x04.BYTE[1]=0x05,请问我怎样才能将其转换为字符串“0405”?
delphi vc byte 类型转换
如何将string 转换成string[]
vc中,int类型的如何转换为string类型
关于C++里面的char和string
.net中char 和 string的区别