 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sat Sep 22, 2007 4:31 pm Post subject: [C#] Binary to String and vice versa... =D |
|
|
Well, I've been kinda bored lately, so I made a Binary to String converter.
I'm not sure exactly what practical purposes this might serve, other than entertainment of course, but oh well.
Here's the source (link for the compiled version below).
| Code: |
using System;
namespace BinaryString
{
class Program
{
public static void OutputHeader()
{
Console.WriteLine("##########################################");
Console.WriteLine("### ###");
Console.WriteLine("### Binary to String ###");
Console.WriteLine("### and vice versa ###");
Console.WriteLine("### converter, written ###");
Console.WriteLine("### By samuri25404 a.k.a ###");
Console.WriteLine("### HeheWaffles ###");
Console.WriteLine("### ###");
Console.WriteLine("##########################################");
Console.WriteLine(); Console.WriteLine();
}
public static void OutputBinaryHelp()
{
Console.WriteLine("The binary format should consist of ones, zeros, and dots(.).");
Console.WriteLine("For example, the binary string for \"Hello World\":");
Console.WriteLine("1001000.1100101.1101100.1101100.1101111.0100000.1110111.1101111.1110010.1101100.1100100");
Console.WriteLine("Notice how after each character there is a period(.)."); Console.WriteLine(); Console.WriteLine();
}
static void Main(string[] args)
{
Console.Clear();
Console.BackgroundColor = ConsoleColor.DarkRed; //You can change these to whatever
Console.ForegroundColor = ConsoleColor.Black; //you want; ConsoleColor is an enum
#if RELEASE //I didn't want the Header to come out while I was debugging. =P
OutputHeader();
#endif
string s = "";
Console.WriteLine("Would you like to convert from string to binary,");
Console.WriteLine("or from binary to string? S for string to binary,");
Console.WriteLine("B for binary to string.");
char c = (char)Console.Read();
switch (c.ToString().ToUpper())
{
case "S" :
s = ConvertToBinary();
break;
case "B" :
s = ConvertToString();
break;
default :
Console.WriteLine("Invalid character.");
Console.WriteLine("Please rerun the program.");
break;
}
Console.WriteLine(s);
Console.ReadLine();
}
public static string ConvertToBinary()
{
string s = "";
string temp = "";
int i = 0;
Console.WriteLine("Please input a string to be converted to a binary array.");
Console.ReadLine(); //I had a good bit of trouble with this--it would keep going
temp = Console.ReadLine(); //while the user was still typing in his/her binary array
foreach (char c in temp.ToCharArray())
{
string zeTmp = "";
i = (int)c;
zeTmp = ToBinary(i);
zeTmp += ".";
zeTmp = zeTmp.PadLeft(8, '0');
s += zeTmp;
}
s = s.TrimEnd('.');
return s;
}
public static string ConvertToString()
{
OutputBinaryHelp();
int tmp = 0;
string s = "";
System.Collections.Generic.List<int> Chars = new System.Collections.Generic.List<int>();
Console.WriteLine("Please input an array of binary to be converted to string format.");
Console.ReadLine();
string temp = Console.ReadLine();
string[] ary = temp.Split('.'); //This makes a new element whenever it finds a .,
foreach (string stuff in ary) //then the . is deleted
{
s = stuff;
if (s.Length != 7)
return "Error. Characters (in binary format) must consist of 7 digits.";
tmp = int.Parse(s);
Chars.Add(tmp);
}
s = "";
foreach (int i in Chars)
{
int x = int.Parse(ToDecimal(i));
s += ((char)x).ToString();
}
return s;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* The Converters *
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
public static string ToBinary(int Decimal)
{
int BinaryHolder;
char[] BinaryArray;
string BinaryResult = "";
while (Decimal > 0)
{
BinaryHolder = Decimal % 2;
BinaryResult += BinaryHolder;
Decimal = Decimal / 2;
}
BinaryArray = BinaryResult.ToCharArray();
Array.Reverse(BinaryArray);
BinaryResult = new string(BinaryArray);
return BinaryResult;
}
public static string ToDecimal(Double n)
{
int cnt = n.ToString().Length;
string p = n.ToString();
double a = 0;
while (cnt > 0)
{
foreach (char y in p.ToCharArray())
{
int c = int.Parse(y.ToString());
if (c == 1)
a = a + (Math.Pow(2, cnt - 1));
cnt = cnt - 1;
}
}
return a.ToString();
}
}
}
|
Compiled form.
P.S., Feel free to use any of the functions in there (the only two that you MIGHT need are the ToDecimal and ToBinary, but it doesn't matter. =P)
Edit:
Added a Windows App version:
| Code: |
//Main
using System;
using System.Windows.Forms;
namespace BinaryStringW
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void Main_Load(object sender, EventArgs e)
{
About abt = new About();
abt.ShowInTaskbar = false;
abt.ShowDialog();
}
public void OutputBinaryHelp()
{
#if RELEASE
const string l1 = "The binary format should consist of ones, zeros, and dots(.). \r\n";
const string l2 = "For example, the binary string for \"Hello World\": \r\n";
const string l3 = "1001000.1100101.1101100.1101100.1101111.0100000.1110111.1101111.1110010.1101100.1100100 \r\n";
const string l4 = "Notice how after each character there is a period(.). \r\n";
const string Help = l1 + l2 + l3 + l4;
MessageBox.Show(Help);
#endif
}
private void ConStToBin_Click(object sender, EventArgs e)
{
ConvertToBinary();
}
private void ConBinToSt_Click(object sender, EventArgs e)
{
ConvertToString();
}
public void ConvertToBinary()
{
string s = "";
string temp = "";
int i = 0;
temp = StToBin.Text;
foreach (char c in temp.ToCharArray())
{
string zeTmp = "";
i = (int)c;
zeTmp = ToBinary(i);
zeTmp += ".";
zeTmp = zeTmp.PadLeft(8, '0');
s += zeTmp;
}
s = s.TrimEnd('.');
BinToSt.Text = s;
return;
}
public void ConvertToString()
{
int tmp = 0;
string s = "";
System.Collections.Generic.List<int> Chars = new System.Collections.Generic.List<int>();
string temp = BinToSt.Text;
string[] ary = temp.Split('.'); //This makes a new element whenever it finds a .,
foreach (string stuff in ary) //then the . is deleted
{
s = stuff;
if (s.Length != 7)
{
MessageBox.Show("Error. Characters (in binary format) must consist of 7 digits.");
return;
}
tmp = int.Parse(s);
Chars.Add(tmp);
}
s = "";
foreach (int i in Chars)
{
int x = int.Parse(ToDecimal(i));
s += ((char)x).ToString();
}
StToBin.Text = s;
return;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* The Converters *
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
public static string ToBinary(int Decimal)
{
int BinaryHolder;
char[] BinaryArray;
string BinaryResult = "";
while (Decimal > 0)
{
BinaryHolder = Decimal % 2;
BinaryResult += BinaryHolder;
Decimal = Decimal / 2;
}
BinaryArray = BinaryResult.ToCharArray();
Array.Reverse(BinaryArray);
BinaryResult = new string(BinaryArray);
return BinaryResult;
}
public static string ToDecimal(Double n)
{
int cnt = n.ToString().Length;
string p = n.ToString();
double a = 0;
while (cnt > 0)
{
foreach (char y in p.ToCharArray())
{
int c = int.Parse(y.ToString());
if (c == 1)
a = a + (Math.Pow(2, cnt - 1));
cnt = cnt - 1;
}
}
return a.ToString();
}
private void BinToSt_Click(object sender, EventArgs e)
{
OutputBinaryHelp();
}
}
}
|
| Code: |
//About
using System;
using System.Windows.Forms;
namespace BinaryStringW
{
public partial class About : Form
{
public About()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
|
I had an incredibly hard time getting the logo in Message Box form, so I decided to just stick it in an about form, and I stuck a button on that which said "Ok", making it seem like a Message Box. ;)
Other than that, not really too much change. I got rid of the "Please input a string..." because it's just annoying--the buttons tell you what to do. =P
Anyways, have fun.
Compiled
Last edited by samuri25404 on Sun Sep 23, 2007 8:11 am; edited 1 time in total |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sat Sep 22, 2007 7:46 pm Post subject: |
|
|
awesome ,just add a copy function orsomething ...
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sat Sep 22, 2007 9:03 pm Post subject: |
|
|
| oib111 wrote: | | awesome ,just add a copy function orsomething ... |
or just make a Windows Form instead of Console
_________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sat Sep 22, 2007 9:08 pm Post subject: |
|
|
yeah lol
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sun Sep 23, 2007 7:07 am Post subject: |
|
|
Or you could browse to the location in CMD and run it from there... =P
For example, if you put it in your My Documents folder:
| Code: |
C:\Documents and Settings\ACCOUNT> dir
Volume in drive D has no label.
Volume Serial Number is 28C4-85C8
Directory of D:\Documents and Settings\ACCOUNT
09/19/2007 06:06 PM <DIR> .
09/19/2007 06:06 PM <DIR> ..
09/16/2007 04:05 PM <DIR> Desktop
09/11/2007 07:25 PM <DIR> Favorites
09/14/2007 07:08 PM <DIR> My Documents
09/23/2006 05:43 AM <DIR> Start Menu
2 File(s) 959 bytes
6 Dir(s) 12,290,895,872 bytes free
D:\Documents and Settings\ACCOUNT> cd My Documents
D:\Documents and Settings\ACCOUNT\My Documents> BinaryString
|
=P
I suppose for the sake of ease, I can make one though.
Edit:
Edited main post with Windows App
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|