string类型转成byte[]:
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );
反过来,byte[]转成string:
string str = System.Text.Encoding.Default.GetString ( byteArray );
其它编码方式的,如System.Text.UTF8Encoding,System.Text.UnicodeEncoding class等;例如:
string类型转成ASCII byte[]:("01" 转成 byte[] = new byte[]{ 0x30, 0x31})
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str );
ASCII byte[] 转成string:(byte[] = new byte[]{ 0x30, 0x31} 转成 "01")
string str = System.Text.Encoding.ASCII.GetString ( byteArray );
有时候还有这样一些需求:
byte[] 转成原16进制格式的string,例如0xae00cf, 转换成 "ae00cf";new byte[]{ 0x30, 0x31}转成"3031":
public static string ToHexString ( byte[] bytes ) // 0xae00cf => "AE00CF "
{
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}:
public static byte[] GetBytes(string hexString, out int discarded)
{
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;
}
分享到:
相关推荐
C# string byte数组转换解析 C# string byte数组转换实现的过程是什么呢?C# string byte数组间的转换需要注意什么呢?C# string byte数组间转换所涉及的方法是什么呢?让我们来看看具体的内容: C# string byte数组...
c#中string转换成字节byte的处理方法
输入string 输出byte 16进制下的转换
1.字符串(String)与Byte[] 的互相转换. 2.Hex(String形式)与Byte[] 的互相转换.
1、char[]与string之间的转换 //string 转换成 Char[] string str=hello; char[] arr=str.ToCharArray(); //Char[] 转换成 string string str1 = new ...//string 转换成 byte[] (字符串是用哪种编码生成的byte[]
C# string byte数组转换解析.pdfC# string byte数组转换解析.pdf
C# string byte数组转换解析.docxC# string byte数组转换解析.docx
(2)byte[] bt=Convert.FromBase64String(“字符串”); 2.字符串转流 代码如下:(1)MemoryStream ms=new MemoryStream(System.Text.Encoding.Default.GetBytes(“字符串”)); (2)MemoryStream ms=new MemoryStream...
很多朋友不清楚如何在Byte[]和String之间进行转换?下面小编给大家带来了byte与string转换的方法,感兴趣的朋友参考下吧
Cstring 与BYTE类型之间转换,实现字节与字符串的转换,将字符串转化为字节类型。0进制与16进制间转换
C#各种数据类型转换,字符串 转换 char数组,char数组 转换 字符串,byte数组 转换 字符串,字符串 转换 byte数组 注意转换出来会使原来的bytearray长度变短,字符串 转换 byte数组 长度为传如的长度,将字符串编码为Base...
实现汉字转换成byte[]类型后,转成二进制加密,再解密成string类型,再转成byte[]类型,再转成汉字输出!适用于网络传输协议,提高被拦截的安全性!希望对大家有所帮助
图片在于string转换的时候中间借助于MemorySteam和Byte数组,下面是我写的FormatChange类,里面两个互相转换的过程。当然这里面也就包含了图片与Byte[]数组的相互转换喽。 class FormatChange { public static ...
在写c#程序时,string和byte[]之间的转换比较烦,在移植一些老程序时感觉很不好。我在c#中使用des和tripledes时移植一块老代码时也遇到了同样的情况。为了下次不为同样的事情烦恼,就写了下面的帮助类。
本文以实例形式详细介绍了C#实现Stream与byte[]之间的转换的方法,分享给大家供大家参考之用。...二、C#中byte[]与string的转换代码 1. System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding
图片到byte[]再到base64string的转换: Bitmap bmp = new Bitmap(filepath); MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); byte[] arr = new byte[ms.Length...
本文实例讲述了C#实现把图片转换成二进制以及把二进制转换成图片的方法。分享给大家供大家参考,具体如下: private void button1_Click(object sender, EventArgs e) { string path = this.textBox1.Text; byte...