# Converting from binary to unary [closed]

I have function that convert binary number to unary number based on a Markov algorithm. In what ways can I improve the code?

The algorithm goes as follows:

1. Replace all 0l with 0l

"|0" -> "0||"

2. Replace all l0 with 0ll

"1" -> "0|"

3. Major Step 3: Remove the 0

"0" -> ""

    public List BinaryToUnary(List value)
{
var temp = new List();
var temp2 = new List();
var temp3 = new List();
string hold = "";
int count = 1;

TxtToDisPlay = "Major Step 1: replace All 1 with 0l userInput " + string.Join("", value) + "\n\n";

foreach (var x in value)
{
if (x.Equals("1"))
{
TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + x + " = " + "0l" + "\n";
}
else
{
TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + x + " = " + x + "\n";
}
count++;
}
TxtToDisPlay = TxtToDisPlay + "\n\nMajor Step 2: Replace all l0 with 0ll  userInput = " + string.Join("", temp) + " \n\n";

foreach (var x in string.Join("", temp))
{
hold = hold + x;
if (hold.Equals("l0"))
{
TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + hold + " = " + "0ll" + "\n";
hold = "";
}
if (hold.Equals("0"))
{
TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + hold + " = " + hold + "\n";
hold = "";
}
count++;
}
if (hold.Equals("l"))
{
}

hold = "";

foreach (var y in string.Join("", temp2))
{
if (temp2.Count == 1)
{
break;
}

hold = hold + y;
if (hold.Equals("l0"))
{
TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + hold + " = " + "0ll" + "\n";
hold = "";
}
if (hold.Equals("0") || hold.Equals("ll"))
{
if (hold.Equals("ll"))
{
TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + hold + " = " + hold + "\n";
hold = "l";
}
else
{
TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + hold + " = " + hold + "\n";
hold = "";
}

}
}
if (value.Count == 3 && hold.Equals("l"))
{
}
else if (value.Count == 3 || hold.Equals("l"))
{

}

TxtToDisPlay = TxtToDisPlay + "\n\nMajor Step 3 : get rid of the 0 from  userInput = " + string.Join("", temp3) + " \n\n";
foreach (var x in temp3)
{
if (x.Contains("0ll"))
{
foreach (var y in x.Where(y => y.Equals('l')))
{
TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + "x = l so " + "Add l To new Array result = " + string.Join("", answer) + "\n";
}
}
else if (x.Contains("l"))
{
TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + "Add l To new Array result = " + string.Join("", answer) + "\n";
}
count++;
}

}

• Could you perhaps provide a plain-English description of the "algorithm"? Commented Apr 20, 2015 at 1:17
• i add the plain version of the algorithm Commented Apr 20, 2015 at 1:56
• As far as I can tell, this code does not work as intended. For example, BinaryToUnary(new List<string> { "1", "0", "0", "0" }) returns 1, 1, 1 when it should return 1, 1, 1, 1, 1, 1, 1, 1. Commented Apr 20, 2015 at 2:24
• A good explanation of the algorithm is here en.wikipedia.org/wiki/Markov_algorithm. I would use a string and not a list since then you can search for substrings easily. You only need one loop. In each iteration you check if any pattern is contained in the string and if so you replace it according to the rules. Commented Apr 20, 2015 at 3:15

You are not supposed to replace all matches but only the first one. And then repeat the process.

Here is some javascript code that implements the markov algorithm exactly the way it is described on the wikipedia page. Now all you have to do is translate that to C#.

function binaryToUnary(value) {
var matchFound = false,
rules = [["|0", "0||"],
["1",  "0|" ],
["0",  ""   ]];
do {
matchFound = false;
for(var i = 0; i < rules.length; i++) {
var rule = rules[i],
pattern = rule[0], replacement = rule[1],
index = value.indexOf(pattern);
if(index >= 0) {
value = value.substring(0, index) +
replacement +
value.substring(index + pattern.length);
matchFound = true;
break; //break because only the first pattern that matches should be replaced
}
}
} while(matchFound);
return value;
}