Parsi Coders
ASCII to binary - نسخه قابل چاپ

+- Parsi Coders (http://parsicoders.com)
+-- انجمن: Software Development Programming (http://parsicoders.com/forumdisplay.php?fid=37)
+--- انجمن: C# Programming (http://parsicoders.com/forumdisplay.php?fid=55)
+--- موضوع: ASCII to binary (/showthread.php?tid=1186)



ASCII to binary - Ghoghnus - 10-31-2011


این یه راه برای تبدیل ASCII to binary
کد:
public string ToBinary(string str)

        {

            string converted = string.Empty;

            byte[] byteArray = GetByteArray(str);



            //create a memory stream

            MemoryStream stream = new MemoryStream();



            //create BinaryWriter based on our MemoryStream

            BinaryWriter writer = new BinaryWriter(stream);



            //convert to binary

            try

            {

                writer.Write(byteArray);

            }

            catch (Exception ex)

            {

                return ex.Message;

            }



            for (int i = 0; i < byteArray.Length; i++)

            {

                for (int j = 0; j < 8; j++)

                {

                    converted += (byteArray[i] & 0x80) > 0 ? "1" : "0";

                    byteArray[i] <<= 1;

                }

            }



            return converted;

        }