C # numbers, 16 binary strings, and bytes to and from each other

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.