0

How can I convert an int number from decimal to binary. For example:

int x=10; // radix 10

How can I make another integer has the binary representation of x, such as:

int y=1010; // radix 2

by using c only?

thkala
86.8k24 gold badges166 silver badges205 bronze badges
asked Dec 14, 2010 at 22:10

8 Answers 8

4

An integer is always stored in binary format internally -- saying that you want to convert int x = 10 base 10 to int y = 1010 base 2 doesn't make sense. Perhaps you want to convert it to a string representing the binary format of the integer, in which case you can use Integer.toBinaryString.

answered Dec 14, 2010 at 22:15
Sign up to request clarification or add additional context in comments.

Comments

3

First thing you should understand is that a value is an abstract notion, that is not bounded to any representation. For example, if you have 20 apples, the number of apples will be the same regardless of the representation. So, dec("10") == bin("1010").
The value of an int reffers to this abstract notion of value, and it does not have any form until you with to print it. This means that the notion of base is important only for conversions from string to int and back.

answered Dec 14, 2010 at 22:17

2 Comments

I just wanted to point out to @Gain that this concept of ints having no base does NOT extend to floats and doubles. It works for integers because they have exact representations in any base; fractional values in general do not. So for example, once you translate a base 10 value like 0.1 to binary, you've lost some information.
@Rick Regan: I think it's something for the "next class" :) Before someone could dive there, he should be familiar with integers. Maybe understanding fixed point arithmetics could also help.
2
answered Dec 14, 2010 at 22:13

Comments

1

Whether it's binary or decimal doesn't really have anything to do with the integer itself. Binary or decimal is a property of a physical representation of the integer, i.e. a String. Thus, the methods you should look at are Integer.toString() and Integer.valueOf() (the versions that take a radix parameter).

BTW, internally, all Java integers are binary, but literals in the source code are decimal (or octal).

answered Dec 14, 2010 at 22:17

Comments

1

Your question is a bit unclear but I'll do my best to try to make sense of it.

How can I make another integer has the binary representation of x such as: int y=1010 radix 2?

From this it looks like you wish to write a binary literal in your source code. Java doesn't support binary integer literals. It only supports decimal, hexadecimal and octal.

You can write your number as a string instead and use Integer.parseInt with the desired radix:

int y = Integer.parseInt("1010", 2);

But you should note that the final result is identical to writing int y = 10;. The integer 10 that was written as a decimal literal in the source code is identical in every way to one which was parsed from the binary string "1010". There is no difference in their internal representation if they are both stored as int.


If you want to convert an existing integer to its binary representation as a string then you can use Integer.toBinaryString as others have already pointed out.

answered Dec 14, 2010 at 22:14

1 Comment

try this public static void main(String [] arg) { int v=15; String x=Integer.toString(v,2); System.out.println(x); }
0

Both integers will have the same interior representation, you can however display as binary via Integer.toBinaryString(i)

answered Dec 14, 2010 at 22:13

Comments

0

Use Integer.toBinaryString()

String y = Integer.toBinaryString(10);
answered Dec 14, 2010 at 22:13

Comments

0

Converting an integer to another base (string representation):

int num = 15;
String fifteen = Integer.toString(num, 2);
// fifteen = "1111"

Converting the string back into an integer

String fifteen = "1111";
int num = Integer.valueOf(fifteen, 2);
// num = 15

This covers the general case for any base. There's no way to explicitly assign an integer as binary (only decimal, octal, and hexadecimal)

int x = 255; // decimal
int y = 0377; // octal (leading zero)
int z = 0xFF; // hex (prepend 0x)
answered Dec 14, 2010 at 22:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.