2

**Hey i was working on an application which converts any basenumber like (2,8,10,16,etc) to user's desire base system. I am having a problem in converting a binary number to its octal number can anyone help me out?

I tried everthing like

// i am taking a binary number in value and then converting it to base 8

Int32 value = int.Parse(convertnumber);                           
Console.WriteLine(Convert.ToString(value, 8));

For example: value =10011

Answer should be this "23" but using the above code i am getting "23433"

devoured elysium
  • 101,373
  • 131
  • 340
  • 557
Pro_Zeck
  • 405
  • 2
  • 13
  • 25
  • Have you tried `Console.WriteLine(value)` to see the actual value of the number? In other words: I think your code is correct, but the value is different from what you think it is. – Travis Gockel Sep 23 '10 at 19:18

7 Answers7

5

"23433" is is the correct answer, when converting "10011" in base 10 to base 8.

You may have meant to interpret "10011" as a binary number. In which case, you want:

int value = Convert.ToInt32(convertnumber, 2);

Edit: in response to comments, here's almost-complete code:

string val = "10011";
int convertnumber = Convert.ToInt32(val, 2);
Console.WriteLine(Convert.ToString(convertnumber, 8)); // prints "23"
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • i want 10011 binary number Converted to base 8 number – Pro_Zeck Sep 23 '10 at 19:41
  • @Pro_Zeck: I got that. Does my answer not help? You want to read in the string into an integer variable, interpreting that string as a base-2 number, as I've demonstrated above. Then, you want the representation of that integer as base 8, as you've got in your last line of code. – Michael Petrotta Sep 23 '10 at 19:50
  • @SIMUCAL code is working perfectly. still i dont know why we have to first convert binary to decimal and then decimal to octal. – Pro_Zeck Sep 23 '10 at 20:05
  • why acn we just convert binary to octal directly there must be a way ? – Pro_Zeck Sep 23 '10 at 20:06
  • @Pro_Zeck: can you elaborate? The code I'm using in my question (see my edit, with an almost-complete program) prints "23". – Michael Petrotta Sep 23 '10 at 20:06
  • @Pro_Zeck: there's no way to convert in one statement, AFAIK. You could write a method for it, I suppose (`ConvertIntegerBase(string val, int baseIn, int baseOut);`). Why do you need to do it in one line? – Michael Petrotta Sep 23 '10 at 20:10
  • dude now what you wrote is correct beacuse in your first comment program you were converting binary to decimal. But i was asking for binary to octal ... What you wrote now its correct . – Pro_Zeck Sep 23 '10 at 20:11
  • @Pro_Zeck: you didn't read closely enough. I was describing what you **were** doing, then I described what you **should** be doing. – Michael Petrotta Sep 23 '10 at 20:12
  • i wannted that in one statement because i wanted to increase the efficiency and the execution of the program must faster ! Suppose if i were making a appliction in which a text file is uploaded containing 1 thousand or more binary to octal base that would take long time to solve it. – Pro_Zeck Sep 23 '10 at 20:14
  • @Pro_Zeck: ah, I see. Important thing to learn: fewer lines of code does not always mean faster execution. If you want a detailed lesson in this, go grab Reflector, and walk through the call stack for the `Int32.Parse` method. That simple little one-line chunk of code does so much, your head will spin. – Michael Petrotta Sep 23 '10 at 20:17
  • @Pro_Zeck: there's no reasonable way to convert bases without having an integer to work with first. Thus, you need to convert your string (which is not a number, not as far as .NET's concerned) to an integer (using base-2 representation), then convert that integer to a string (using base-8 representation). – Michael Petrotta Sep 23 '10 at 20:20
4
string binary = "10011";
int integer = Convert.ToInt32(binary, 2);
Console.WriteLine(Convert.ToString(integer, 8));

Output: 23

In this example we convert the binary string representation to an integer and from an integer to the octal string representation.

mmcdole
  • 91,488
  • 60
  • 186
  • 222
  • cant we directly convert binary to octal system ? – Pro_Zeck Sep 23 '10 at 19:42
  • 1
    @Simucal, @Pro_Zeck: This answer doesn’t convert anything to decimal at all. – Timwi Sep 23 '10 at 21:06
  • 1
    @Timwi, how does it not? From base 2 (binary) to base 10 (decimal) then from base 10 to base 8 (octal). – mmcdole Sep 24 '10 at 14:45
  • 1
    @Simucal: Your code (correctly) converts from the binary string representation to an integer and then from the integer to the octal string representation. There is absolutely no base-10 representation anywhere in this process. – Timwi Sep 24 '10 at 16:14
  • @Simucal the integer is just an integer. Only a _representation_ of an integer can have a base. Like, when you write it down as a series of digits. Or, when you store it in transistors or capacitors as a series of bits. But semantically, a value stored in C#'s "int" type has *no base* at all. – Roman Starkov Sep 24 '10 at 17:06
  • @Timwi, I see what you are saying now. I'll make a correction. – mmcdole Sep 24 '10 at 17:53
3
int value = Convert.ToInt32(convertnumber, 2);
Console.WriteLine(Convert.ToString(value, 8));

You are taking a base 10 number 10011 and converting it to base 8. Which is 23433.

Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
  • Why are you calling this “convert to decimal”? There is no conversion to decimal anywhere. – Timwi Sep 23 '10 at 21:07
  • @Timwi: The decimal numeral system (also called base ten or occasionally denary) has ten as its base – Yuriy Faktorovich Sep 23 '10 at 21:35
  • @Yuriy: No, really? You don’t say! There’s still no decimal anywhere in your code though. (And of course there shouldn’t be. The code is correct. The comment is wrong.) – Timwi Sep 24 '10 at 16:15
  • @Timwi `int` is in base 10, aka decimal – Yuriy Faktorovich Sep 24 '10 at 16:48
  • 1
    @Yuriy: LOL, no it isn’t... has never been. Where did you get that from? – Timwi Sep 24 '10 at 16:55
  • @Timwi ok, what base do you think `int` is in? – Yuriy Faktorovich Sep 24 '10 at 17:08
  • 1
    @Yuriy: **int is not a string of digits.** An `int` is not a sequence of characters. It is *the number itself*. It is not in any base. (Of course physically it is stored in memory as *binary*, but that’s irrelevant.) – Timwi Sep 24 '10 at 17:10
  • @Yuriy I strongly recommend that you ask "What base is `int` in?" as a separate question, here on SO, then you can get much more detailed answers. – Roman Starkov Sep 24 '10 at 17:14
  • @romkyns: No reason, I agree with him. The value is stored in binary. By default the representation of it is base 10. Thats the reason why I say it is decimal. If you want to argue semantics, that I never displayed it, so it was never base 10, you're possibly right. I'll take down the comment since I've also seemed to confuse myself. – Yuriy Faktorovich Sep 24 '10 at 17:41
2

If you want to do this manually (so you understand what is going on) here is a suggestion:

First pad the binary string to be divisable by 3 ( 3 bits = 1 octal digit )

string binary = "10011";
int pad = binary.Length % 3;
binary = new string('0', 3-pad) + binary;

Then process each three bits into one octal digit

int n = binary.Length / 3;
char[] bin_digits = binary.ToCharArray();
char[] oct_digits = new char[n];
for (int i = 0; i < n; i++)
{
    int digit = bin_digits.Skip(3 * i).Take(3).Aggregate(0,
        (x, v) => (int)v - (int)'0' + 2 * x);
    // x is the value accumulation
    // v is a char '0' or '1' representing a bit and is converted to int 0, 1
    oct_digits[i] = (char)(digit + (int)'0');
    // convert int to char digit
}

Convert the digits array into a string

string oct_value = new string(oct_digits);

Example results: "10011" -> "23" "11000" -> "30" "1011011" -> "133"

John Alexiou
  • 28,472
  • 11
  • 77
  • 133
1

Naturally, int.Parse parses a decimal number. If your input is binary, then you'll need to first do a conversion from binary to integer.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
1
Int32 value = Convert.ToInt32( "10011", 2 );
Console.WriteLine(Convert.ToString(value, 8)); 
Dan Byström
  • 9,067
  • 5
  • 38
  • 68
0

That's because int.Parse is converting 10011 to, well, 10011 in decimal. It is not converting it from 10011 binary to 23 octal (19 decimal) as you want it to.

Orca
  • 2,035
  • 4
  • 24
  • 39