///summary///从汉字转换到16进制////summary///paramname=s/param///returns/returnspublicstaticstringGetHexFromChs(strings){if((s.Length%2)!=0){s+=;//空格//thrownewArgumentException(sisnotvalidchinesestring!);}System.Text.Encodingchs=System.Text.Encoding.GetEncoding(gb2312);byte[]bytes=chs.GetBytes(s);stringstr=;for(inti=0;ibytes.Length;i++){str+=string.Format({0:X},bytes[i]);}returnstr;}///summary///从16进制转换成汉字////summary///paramname=hex/param///returns/returnspublicstaticstringGetChsFromHex(stringhex){if(hex==null)thrownewArgumentNullException(hex);if(hex.Length%2!=0){hex+=20;//空格//thrownewArgumentException(hexisnotavalidnumber!,hex);}//需要将hex转换成byte数组。byte[]bytes=newbyte[hex.Length/2];for(inti=0;ibytes.Length;i++){try{//每两个字符是一个byte。bytes[i]=byte.Parse(hex.Substring(i*2,2),System.Globalization.NumberStyles.HexNumber);}catch{//Rethrowanexceptionwithcustommessage.thrownewArgumentException(hexisnotavalidhexnumber!,hex);}}//获得GB2312,ChineseSimplified。System.Text.Encodingchs=System.Text.Encoding.GetEncoding(gb2312);returnchs.GetString(bytes);}