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 #