binary conversions in C #

Source: Internet
Author: User

Today happened to touch a bit or expression, just like you do a binary between the converter, the basic skills are not solid, binary operation between the confused.

The common way to do this is binary, octal, decimal, and hexadecimal, and the general conversion method is to convert non-decimal data types to decimal and then convert to other types.

Entry functions for the binary conversions:

       /// <summary>       ///Binary Conversion/// </summary>       /// <param name= "input" ></param>       /// <param name= "Fromtype" ></param>       /// <param name= "ToType" ></param>       /// <returns></returns>        Public stringConvertgenericbinary (stringInputbyteFromtype,byteToType) {           stringOutput =input; Switch(fromtype) { Case 2: Output=convertgenericbinaryfrombinary (Input,totype);  Break;  Case 8: Output=convertgenericbinaryfromoctal (Input,totype);  Break;  Case Ten: Output=Convertgenericbinaryfromdecimal (Input,totype);  Break ;  Case  -: Output=Convertgenericbinaryfromhexadecimal (Input,totype);  Break; default:                    Break; }           returnoutput; }     

Convert from binary to other formats:

       /// <summary>       ///convert from binary to other binary/// </summary>       /// <param name= "input" ></param>       /// <param name= "ToType" ></param>       /// <returns></returns>       Private stringConvertgenericbinaryfrombinary (stringInputbyteToType) {                      Switch(totype) { Case 8:                   //convert to decimal and then go to octalinput = convert.tostring (Convert.ToInt32 (Input,2),8);  Break;  Case Ten: Input= Convert.ToInt32 (Input,2).                   ToString ();  Break;  Case  -: Input= convert.tostring (Convert.ToInt32 (Input,2), -);  Break; default:                    Break; }           returninput; }

Convert from octal to other formats:

       /// <summary>       ///convert from octal to other binary/// </summary>       /// <param name= "input" ></param>       /// <param name= "ToType" ></param>       /// <returns></returns>       Private stringConvertgenericbinaryfromoctal (stringInputbyteToType) {           Switch(totype) { Case 2: Input= convert.tostring (Convert.ToInt32 (Input,8),2);  Break;  Case Ten: Input= Convert.ToInt32 (Input,8).                   ToString ();  Break;  Case  -: Input= convert.tostring (Convert.ToInt32 (Input,8), -);  Break; default:                    Break; }           returninput; }

Convert from decimal to other formats:

       /// <summary>       ///convert from decimal to another binary/// </summary>       /// <param name= "input" ></param>       /// <param name= "ToType" ></param>       /// <returns></returns>       Private stringConvertgenericbinaryfromdecimal (stringInputintToType) {           stringOutput =""; intIntinput =Convert.ToInt32 (input); Switch(totype) { Case 2: Output= Convert.ToString (Intinput,2);  Break;  Case 8: Output= Convert.ToString (Intinput,8);  Break;  Case  -: Output= Convert.ToString (Intinput, -);  Break; default: Output=input;  Break; }           returnoutput; }

Convert from 16 to other formats:

       /// <summary>       ///convert from 16 binary to other binary/// </summary>       /// <param name= "input" ></param>       /// <param name= "ToType" ></param>       /// <returns></returns>       Private stringConvertgenericbinaryfromhexadecimal (stringInputintToType) {           Switch(totype) { Case 2: Input= convert.tostring (Convert.ToInt32 (Input, -),2);  Break;  Case 8: Input= convert.tostring (Convert.ToInt32 (Input, -),8);  Break;  Case Ten: Input= Convert.ToInt32 (Input, -).                   ToString ();  Break; default:                    Break; }           returninput; }

Finally, a binary addition is posted:

       /// <summary>       ///addition of binary/// </summary>       /// <param name= "x" ></param>       /// <param name= "y" ></param>       /// <returns></returns>        Public stringAddbetweenbinary (stringXstringy) {intIntsum = Convert.ToInt32 (x,2) + Convert.ToInt32 (Y,2); returnConvert.ToString (Intsum,2) ; }

binary conversions in C #

Related Article

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.