Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we convert binary number into its octal number using c#?

Tags:

c#

**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"

like image 788
Pro_Zeck Avatar asked Dec 09 '25 22:12

Pro_Zeck


1 Answers

"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"
like image 70
Michael Petrotta Avatar answered Dec 12 '25 11:12

Michael Petrotta