/// <summary> ///Convert a hexadecimal string to a byte array/// </summary> /// <param name= "src" ></param> /// <returns></returns> Public byte[] Hexstring2bytes (stringsrc) { byte[] Retbytes =New byte[SRC. Length/2]; for(inti =0; I < SRC. Length/2; i++) {Retbytes[i]=byte. Parse (src. Substring (i *2,2), System.Globalization.NumberStyles.AllowHexSpecifier); } returnretbytes; } /// <summary> ///string goto 16 binary/// </summary> /// <param name= "Mhex" ></param> /// <returns>returns the hexadecimal representation of a string</returns> Public stringHEXTOSTR (stringMhex,BOOLisuft8=false)//returns the hexadecimal representation of a string { if(Isuft8 = =false) { byte[] Btemp =System.Text.Encoding.Default.GetBytes (Mhex); stringstrtemp =""; for(inti =0; i < btemp.length; i++) {strtemp+ = Btemp[i]. ToString ("X"); } returnstrtemp; } Else { byte[] Btemp =System.Text.Encoding.UTF8.GetBytes (Mhex); stringstrtemp =""; for(inti =0; i < btemp.length; i++) {strtemp+ = Btemp[i]. ToString ("X"); } returnstrtemp; } } /// <summary> ///hexadecimal conversion to decimal/// </summary> /// <param name= "Strcolorvalue" ></param> /// <returns></returns> Public Static intGethexadecimalvalue (String strcolorvalue) {Char[] Nums =Strcolorvalue.tochararray (); intTotal =0; Try { for(inti =0; I < Nums. Length; i++) {String strnum=Nums[i]. ToString (). ToUpper (); Switch(strnum) { Case "A": Strnum="Ten"; Break; Case "B": Strnum=" One"; Break; Case "C": Strnum=" A"; Break; Case "D": Strnum=" -"; Break; Case "E": Strnum=" -"; Break; Case "F": Strnum=" the"; Break; default: Break; } DoublePower = Math.pow ( -, Convert.todouble (nums. Length-i-1)); Total+ = Convert.ToInt32 (strnum) *Convert.ToInt32 (Power); } } Catch(System.Exception ex) {String Strerorr=Ex. ToString (); return 0; } returnTotal ; }
TCP Binary Conversions