1.十六进制字符串转换为字节数组
private const string CharSet="0123456789ABCDEF";
int nLen=%%1.Length/2;
byte[] %%2=new byte[nLen];
for(int i=0;i<nLen;i++) {
string chs=%%1.ToUpper();
if(chs.Length<2) return -1;
%%2[i]=(byte)(CharSet.IndexOf(chs.Substring(i*2,1)*16+CharSet.IndexIf(chs.Substring(i*2+1,1)));
}
2.字节数组转换为十六进制字符串
byte[] todetail=%%1;
string %%2 = BitConverter.ToString(todetail,0,todetail.Length).Replace("-"," ");
3.字节数组转换为英文字符串
byte[] buff=%%1;
string %%2 = Encoding.UTF8.GetString(buff,0,buff.Length);
4.英文字符串转换为字节数组
byte[] %%2=Encoding.UTF8.GetBytes(%%1);
5.Unicode编码转换为字符串
byte[] utf16be=%%1;
string %%2=Encoding.BigEndianUnicode.GetString(utf16be,0,utf16be.Length);
6.字符串转换为 Unicode编码
byte[] %%2=Encoding.BigEndianUnicode.GetBytes(%%1);
7.从ByteBuffer中获取byte数组
byte[] byRead = %%1;
int nLen=Buffer.ByteLength(byRead);
byte[] %%2=new byte[nLen];
Array.Copy(byRead,0,%%2,0,nLen);
8.十六进制字符串中插入空格
string %%2="";
int nLen=%%1.Length;
for(int i=0;i<nLen/2;i++) {
%%2+=%%1.Substring(i*2,2)+" ";
}
9.十六进制字符串中删除空格
%%2=%%1.Trim(" ");
c#数据处理
最新推荐文章于 2024-11-12 14:16:30 发布