Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,476 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,596 people online right now. Registration is fast and FREE... Join Now!




java

 
Reply to this topicStart new topic

java, convertion of binary to octal

jaztine1120
2 Jul, 2008 - 07:37 PM
Post #1

New D.I.C Head
*

Joined: 1 Jul, 2008
Posts: 1

hi friend give me a code of conversion of binary to octal
User is offlineProfile CardPM
+Quote Post

pbl
RE: Java
2 Jul, 2008 - 07:40 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,113



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(jaztine1120 @ 2 Jul, 2008 - 08:37 PM) *

hi friend give me a code of conversion of binary to octal


Easy task but

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
User is online!Profile CardPM
+Quote Post

Locke37
RE: Java
2 Jul, 2008 - 08:46 PM
Post #3

I'm not a thief...I prefer the term TREASURE HUNTER!
Group Icon

Joined: 20 Mar, 2008
Posts: 1,012



Thanked: 39 times
Dream Kudos: 325
My Contributions
D'accord avec pbl. (I took French in High School smile.gif, although not quite sure if that's right)

In other language, I agree with pbl.

But I will offer you one little hint to making this easy.

3 Binary Digits = 1 Octal Digit
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Java
3 Jul, 2008 - 10:13 AM
Post #4

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,179



Thanked: 6 times
Dream Kudos: 100
Expert In: Java

My Contributions
There are static utility methods in the Java API that handle number conversions to/from common bases (2,8,16). Look in an obvious class (hint, it's a wrapper class for a simple type) and you'll find them.

User is offlineProfile CardPM
+Quote Post

Locke37
RE: Java
3 Jul, 2008 - 02:05 PM
Post #5

I'm not a thief...I prefer the term TREASURE HUNTER!
Group Icon

Joined: 20 Mar, 2008
Posts: 1,012



Thanked: 39 times
Dream Kudos: 325
My Contributions
Well, yes there are, but normally, there are assignments in classes to make these methods and such yourself. I had something like that in my java class, and I looked up the methods to do it, but my teacher said write the code myself...don't use the methods...unsure.gif
User is offlineProfile CardPM
+Quote Post

JeroenFM
RE: Java
4 Jul, 2008 - 01:01 AM
Post #6

D.I.C Head
Group Icon

Joined: 30 Jun, 2008
Posts: 188



Thanked: 9 times
Dream Kudos: 100
My Contributions
QUOTE(Locke37 @ 3 Jul, 2008 - 03:05 PM) *

Well, yes there are, but normally, there are assignments in classes to make these methods and such yourself. I had something like that in my java class, and I looked up the methods to do it, but my teacher said write the code myself...don't use the methods...unsure.gif


Binary: 1 bit per number
Octal: 3 bits per number
Hexadecimal: 4 bits per number

Let's take the number 47:

Binary: 00101111

Each binary number represents a power of 2: the rightmost is 2^1, the next is 2^2, and so on

For every octal "place", you take the rightmost 3 bits from the binary number:

111 = 1*4 + 1*2 + 1*1 = 7
101 = 1*4 + 0*2 + 1*1 = 5
00 = 0*2 + 0*1 = 0

So 47 decimal = 00101111 binary = 57 octal

Hexadecimal is similar, except you take 4 bits and replace any number higher than 9 with A-F:

Decimal 47 =
Binary 00101111

1111 = 1*8 + 1*4 + 1*2 + 1*1 = 15 = F
0010 = 0*8 + 0*4 + 1*2 + 0*1 = 2

So 47 decimal = 2F hexadecimal

---

Now surely you can turn this into an algorithm?

This post has been edited by JeroenFM: 4 Jul, 2008 - 01:05 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 06:13PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month