需要这样一个记录本-7 C#字符串与字节数组相互转换

来源:互联网 发布:类似爱情2只有我知360 编辑:程序博客网 时间:2024/06/08 03:37

1.字符串转换为字节数组

byte[] pMsgData = System.Text.Encoding.Unicode.GetBytes(String m_sContent);

2.字节数组转换为字符串

方法一:
<span style="white-space:pre"></span>public static String GetStringByBytes(byte[] pBuffer, int nOffset, int nBufferLen)        {            String sResults = String.Empty;            int nCount = 0;            for (nCount = 0; nCount < nBufferLen; nCount++)            {                if (pBuffer[nOffset + nCount] == 0)                {                    break;                }            }            sResults = Encoding.ASCII.GetString(pBuffer, nOffset, nCount);            if (sResults == String.Empty)                sResults = "0";            return sResults;        }方法二:
        static public string GetBytesString(byte[] bytes, int index, int count, string sep)        {            return String.Join(sep, bytes.Skip(index).Take(count).Select(b => b.ToString("X2")));        }


0 0