C#数据格式转换函数集合

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

常用数据格式转换函数集合usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceLaisonTech.CommonBLL{///summary///数据类型转换////summarypublicclassDataFormatProcessor{publicconstByteASCII_0=48;publicconstByteASCII_9=57;publicconstByteASCII_A=65;publicconstByteASCII_F=70;publicconstByteASCII_Z=90;publicconstByteASCII_a=97;publicconstByteASCII_f=102;publicconstByteASCII_z=122;///summary///字节转换为字符串////summary///paramname=val/param///returns/returnspublicstaticStringByteToHexChar(Byteval){returnval.ToString(X2);}///summary///字节转换为字符串////summary///paramname=val/param///returns/returnspublicstaticStringByteToBitString(Byteval){if(val==0){return00000000;}if(val==255){return11111111;}Stringret=String.Empty;Int32bitval=0;for(intbitidx=7;bitidx-1;--bitidx){bitval=val&(1bitidx);if(bitval0){ret=ret+1;}else{ret=ret+0;}}returnret;}///summary///2个HEX字符转换为1个字节数组////summary///paramname=hexstrA1/param///paramname=bytes161/param///returns/returnspublicstaticBooleanHexStringToByte(Stringhexstr,outBytebyteval){byteval=0;if(hexstr.Length!=2){returnfalse;}byteval=Convert.ToByte(hexstr,16);returntrue;}///summary///HEX字符串转换为字节数组////summary///paramname=hexstr/param///paramname=bytes/param///returns/returnspublicstaticBooleanHexStringToBytes(Stringhexstr,outByte[]bytes){bytes=null;if((hexstr==null)||(hexstr.Length2)){returnfalse;}if(hexstr.Length%2==1){returnfalse;}intbytelen=hexstr.Length/2;if(bytelen1){returnfalse;}bytes=newByte[bytelen];Stringcurbytestr=String.Empty;for(intbidx=0;bidxbytelen;bidx++){curbytestr=hexstr.Substring(bidx*2,2);//数字的进制,它必须是2、8、10和16.bytes[bidx]=Convert.ToByte(curbytestr,16);}if(bytes==null||bytes.Length1){returnfalse;}returntrue;}///summary///HEX字符串转换为字节数组////summary///paramname=hexstr/param///paramname=bytes/param///returns/returnspublicstaticBooleanHexStringToBytesWithSplitter(Stringhexstr,outByte[]bytes){bytes=null;if(hexstr==null||hexstr.Length2){returnfalse;}intcharidx=0;StringBuilderstrbuild=newStringBuilder(hexstr.Length);//对每个字符进行判断,过滤非法字符while(charidxhexstr.Length){//A-65,F-70,a-97,f-102,0-489-57if((hexstr[charidx]=DataFormatProcessor.ASCII_0&&hexstr[charidx]=DataFormatProcessor.ASCII_9)||(hexstr[charidx]=DataFormatProcessor.ASCII_A&&hexstr[charidx]=DataFormatProcessor.ASCII_F)||(hexstr[charidx]=DataFormatProcessor.ASCII_a&&hexstr[charidx]=DataFormatProcessor.ASCII_f)){strbuild.Append(hexstr[charidx]);}charidx++;}Stringpurehexstr=strbuild.ToString();returnHexStringToBytes(purehexstr,outbytes);}///summary///字节数组转换为字符串////summary///paramname=bytes/param///returns/returnspublicstaticStringBytesToHexString(Byte[]bytes){if(bytes==null||bytes.Length1){returnString.Empty;}StringBuilderstr=newStringBuilder(bytes.Length*2);for(inti=0;ibytes.Length;i++){str.Append(bytes[i].ToString(X2));}returnstr.ToString();}///summary///字节数组转换为字符串////summary///paramname=bytes/param///paramname=start待转换的首个字节下标/param///paramname=len/param///paramname=eachlinebytes每隔多少字节,就插入一个换行符.0表示不换行/param///returns/returnspublicstaticStringBytesToHexString(Byte[]bytes,Int32start,Int32len,Int32eachlinebytes){if(bytes==null||bytes.Length1||start0||len1||start+lenbytes.Length){returnString.Empty;}Int32curlinebytes=0;StringBuilderstr=newStringBuilder(bytes.Length*2);for(inti=0;ilen;i++){str.Append(bytes[i+start].ToString(X2));if(eachlinebytes0){curlinebytes++;if(curlinebytes=eachlinebytes){str.Append(Environment.NewLine);curlinebytes=0;}}}returnstr.ToString();}///summary///字节数组转换为带间隔符的字符串,显示用////summary///paramname=bytes/param///paramname=start待转换的首个字节下标/param///paramname=len/param///paramname=splitter/param///paramname=eachlinebytes每隔多少字节,就插入一个换行符.0表示不换行/param///returns/returnspublicstaticStringBytesToHexString(Byte[]bytes,Int32start,Int32len,Charsplitter,Int32eachlinebytes){if(bytes==null||bytes.Length1||start0||len1||start+lenbytes.Length){returnString.Empty;}Int32curlinebytes=0;StringBuilderstr=newStringBuilder(bytes.Length*2);for(inti=0;ibytes.Length;i++){str.Append(bytes[i+start].ToString(X2));if((Byte)(splitter)!=0){str.Append(splitter);}if(eachlinebytes0){curlinebytes++;if(curlinebytes=eachlinebytes){str.Append(Environment.NewLine);curlinebytes=0;}}}str.Remove(str.Length-1,1);returnstr.ToString();}///summary///字节数组转换为ASCII字符串////summary///paramname=bytes/param///returns/returnspublicstaticStringBytesToString(Byte[]bytes){Stringret=Encoding.Default.GetString(bytes);returnret;}///summary///1个字节的BCD转换为HEX///0x12-12////summary///paramname=bcdBCD字节/param///returnsInt32/returnspublicstaticByteByteBCDToHex(Bytebcd){Byteresult=(Byte)(bcd&0x0F);//个位bcd=4;result+=(Byte)(bcd*10);//十位returnresult;//返回结果}///summary///把数组里的每个字节,都从HEX转换为BCD////summary///returns/returnspublicstaticBooleanByteArrayHexToBCD(Byte[]hexbytes,Int32hexstart,Byte[]bcdbytes,Int32bcdstart,Int32len){if(hexbytes==null||bcdbytes==null||hexbytes.Length1||bcdbytes.Length1||hexs

1 / 19
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功