Transfer from http://luohonghong.blog.163.com/blog/static/78312058201242632055642/
As follows:
1, numbers, and bytesintnum=12345;byte[] bytes=bitconverter.getbytes (NUM);//convert Int32 to byte arrayNum=bitconverter.toint32 (Bytes,0);//Convert byte array contents to int32 type2, converts the string to 16 characters, allows the ChinesePrivate stringStringtohexstring (strings, Encoding encode) { byte[] b = encode. GetBytes (s);//encodes a string into a byte array according to the specified encoding stringresult =string. Empty; for(inti =0; i < b.length; i++)//Convert bytes to 16 characters{result+ = Convert.ToString (B[i], -); } returnresult; } such as:///Note that a Chinese conversion to utf-8 accounts for three bytes, and the English account is one byteSystem.Console.WriteLine (Stringtohexstring ("China", System.Text.Encoding.UTF8)); or use Bitconverter.tostring (Encoding.UTF8.GetBytes ("China") ) Return the result to XX-xx-xx and then remove"-" 3, converting a 16 binary string to a stringPrivate stringHexstringtostring (stringHS, Encoding encode) { stringstrtemp=""; byte[] b=New byte[HS. length/2]; for(inti =0; I < HS. Length/2; i++) {strtemp= HS. Substring (i *2,2); B[i]= Convert.tobyte (strtemp, -); } //converts a byte array into a string by the specified encoding returnencode. GetString (b); } such as:stringHexstring= stringtohexstring ("China", System.Text.Encoding.UTF8);stringContent=hexstringtostring (hexstring, System.Text.Encoding.UTF8)4, convert byte[] to 16 binary string Public Static stringBYTETOHEXSTR (byte[] bytes) { stringReturnstr =""; if(Bytes! =NULL) { for(inti =0; I < bytes. Length; i++) {Returnstr+ = Bytes[i]. ToString ("X2"); } } returnReturnstr; }5, convert the 16 binary string to byte[]Private Static byte[] Strtotohexbyte (stringhexstring) {hexstring= Hexstring.replace (" ",""); if((hexstring.length%2) !=0) hexstring+=" "; byte[] Returnbytes =New byte[Hexstring.length/2]; for(inti =0; i < returnbytes.length; i++) Returnbytes[i]= Convert.tobyte (hexstring.substring (i *2,2), -); returnreturnbytes; }
C # numbers, 16 binary strings, and bytes to and from each other