Click here to Skip to main content
16,004,678 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, I need help to convert binary to octal ( int )

i appreciate the help or some example thanks in advance.
Posted
Updated 11-Nov-13 7:14am
v2
Comments
Sergey Alexandrovich Kryukov 11-Nov-13 13:23pm    

Any piece of data is a block of bytes; in .NET usually represented as the array byte[]. And each byte can be represented as octal string:
C#
byte[] data = //... read the file or whatever you have

//...

string octalNumber = System.Convert(data[someIndex], 8);


On top of it, convert your data the way you like.

—SA
 
Share this answer
 
C#
string binary = "10011";
int integer = Convert.ToInt32(binary, 2);
string octal = Convert.ToString(integer, 8);


Reference - http://stackoverflow.com/questions/3781764/how-can-we-convert-binary-number-into-its-octal-number-using-c[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 11-Nov-13 13:18pm    
It can confuse OP who is apparently not very experienced. Only last line is relevant. If you added first two lines just for illustration, at least you should explain it to OP.

And using integer makes no sense because it is signed. And using uint or something else would be questionable. What would you advise to do it the file size is not N*4 bytes? Therefore, instead of it you should better use byte.

—SA
Kornfeld Eliyahu Peter 11-Nov-13 13:28pm    
First I can't see where you found that file thing...
To show how to covert from binary to octal, one must start with binary and finish with octal...
Sergey Alexandrovich Kryukov 11-Nov-13 13:55pm    
Just for example. Any data, of course. "Convert binary to octal" does not assume you convert string to numeric and than back to string. It only means numeric to string. You did not start with binary, started with string... However, we don't know exactly what OP meant by "binary", so, this is another reason not to assume it was the string data of any certain format.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900