欢迎来到天天文库
浏览记录
ID:35233357
大小:85.00 KB
页数:19页
时间:2019-03-22
《c#数据格式转换函数集合》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、常用数据格式转换函数集合usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceLaisonTech.CommonBLL{//////数据类型转换///publicclassDataFormatProcessor{publicconstByteASCII_0=48;publicconstByteASCII_9=57;publicconstByteASCII_A=65;publicconstByteASCII_F=70;publicconstByteASCII_Z=9
2、0;publicconstByteASCII_a=97;publicconstByteASCII_f=102;publicconstByteASCII_z=122;//////字节转换为字符串/////////publicstaticStringByteToHexChar(Byteval){returnval.ToString("X2");}//////字节转换为字符串//////3、am>///publicstaticStringByteToBitString(Byteval){if(val==0){return"00000000";}if(val==255){return"11111111";}Stringret=String.Empty;Int32bitval=0;for(intbitidx=7;bitidx>-1;--bitidx){bitval=val&(1<0){ret=ret+"1";}else{ret=ret+"0";}}returnret;}//////24、个HEX字符转换为1个字节数组//////"A1"///161///publicstaticBooleanHexStringToByte(Stringhexstr,outBytebyteval){byteval=0;if(hexstr.Length!=2){returnfalse;}byteval=Convert.ToByte(hexstr,16);returntrue;}//////HEX字符串转换为5、字节数组////////////publicstaticBooleanHexStringToBytes(Stringhexstr,outByte[]bytes){bytes=null;if((hexstr==null)6、7、(hexstr.Length<2)){returnfalse;}if(hexstr.Length%2==1){returnfalse;}intbytelen=hexstr.Length/2;if(byt8、elen<1){returnfalse;}bytes=newByte[bytelen];Stringcurbytestr=String.Empty;for(intbidx=0;bidx9、10、bytes.Length<1){returnfalse;}returntrue;}//////HEX字符串转换为字节数11、组////////////publicstaticBooleanHexStringToBytesWithSplitter(Stringhexstr,outByte[]bytes){bytes=null;if(hexstr==null12、13、hexstr.Length<2){retur
3、am>///publicstaticStringByteToBitString(Byteval){if(val==0){return"00000000";}if(val==255){return"11111111";}Stringret=String.Empty;Int32bitval=0;for(intbitidx=7;bitidx>-1;--bitidx){bitval=val&(1<0){ret=ret+"1";}else{ret=ret+"0";}}returnret;}//////2
4、个HEX字符转换为1个字节数组//////"A1"///161///publicstaticBooleanHexStringToByte(Stringhexstr,outBytebyteval){byteval=0;if(hexstr.Length!=2){returnfalse;}byteval=Convert.ToByte(hexstr,16);returntrue;}//////HEX字符串转换为
5、字节数组////////////publicstaticBooleanHexStringToBytes(Stringhexstr,outByte[]bytes){bytes=null;if((hexstr==null)
6、
7、(hexstr.Length<2)){returnfalse;}if(hexstr.Length%2==1){returnfalse;}intbytelen=hexstr.Length/2;if(byt
8、elen<1){returnfalse;}bytes=newByte[bytelen];Stringcurbytestr=String.Empty;for(intbidx=0;bidx9、10、bytes.Length<1){returnfalse;}returntrue;}//////HEX字符串转换为字节数11、组////////////publicstaticBooleanHexStringToBytesWithSplitter(Stringhexstr,outByte[]bytes){bytes=null;if(hexstr==null12、13、hexstr.Length<2){retur
9、
10、bytes.Length<1){returnfalse;}returntrue;}//////HEX字符串转换为字节数
11、组////////////publicstaticBooleanHexStringToBytesWithSplitter(Stringhexstr,outByte[]bytes){bytes=null;if(hexstr==null
12、
13、hexstr.Length<2){retur
此文档下载收益归作者所有