Gain
Gain

Reputation: 3853

How can I convert an int number from decimal to binary?

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?

Upvotes: 0

Views: 4323

Answers (8)

Mark Byers
Mark Byers

Reputation: 837936

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.

Upvotes: 1

dflems
dflems

Reputation: 214

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)

Upvotes: 0

ruslik
ruslik

Reputation: 14870

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.

Upvotes: 3

Michael Borgwardt
Michael Borgwardt

Reputation: 346250

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).

Upvotes: 1

casablanca
casablanca

Reputation: 70691

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.

Upvotes: 4

Caner
Caner

Reputation: 59148

String s = Integer.toBinaryString(10);

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html

Upvotes: 2

morja
morja

Reputation: 8550

Use Integer.toBinaryString()

String y = Integer.toBinaryString(10);

Upvotes: 0

Paul Croarkin
Paul Croarkin

Reputation: 14675

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

Upvotes: 0

Related Questions