将一个int转换为Java中的二进制string表示forms?

在Java中将int转换为二进制string表示的最好方法是什么(理想情况下最简单)?

例如,说int是156.这个的二进制string表示将是“10011100”。

Integer.toBinaryString(int i) 

还有java.lang.Integer.toString(int i,int base)方法,如果你的代码有一天可能处理除2(二进制)之外的其他基础 ,那么这个方法会更合适。

还有一种方法 – 通过使用java.lang.Integer ,可以获得第二个参数指定的radix (Octal - 8, Hex - 16, Binary - 2)中第一个参数istring表示forms。

  Integer.toString(i, radix) 

例_

 private void getStrtingRadix() { // TODO Auto-generated method stub /* returns the string representation of the unsigned integer in concern radix*/ System.out.println("Binary eqivalent of 100 = " + Integer.toString(100, 2)); System.out.println("Octal eqivalent of 100 = " + Integer.toString(100, 8)); System.out.println("Decimal eqivalent of 100 = " + Integer.toString(100, 10)); System.out.println("Hexadecimal eqivalent of 100 = " + Integer.toString(100, 16)); } 

OutPut_

 Binary eqivalent of 100 = 1100100 Octal eqivalent of 100 = 144 Decimal eqivalent of 100 = 100 Hexadecimal eqivalent of 100 = 64 
 public static string intToBinary(int n) { string s = ""; while (n > 0) { s = ( (n % 2 ) == 0 ? "0" : "1") +s; n = n / 2; } return s; } 
 public class Main { public static String toBinary(int n, int l ) throws Exception { double pow = Math.pow(2, l); StringBuilder binary = new StringBuilder(); if ( pow < n ) { throw new Exception("The length must be big from number "); } int shift = l- 1; for (; shift >= 0 ; shift--) { int bit = (n >> shift) & 1; if (bit == 1) { binary.append("1"); } else { binary.append("0"); } } return binary.toString(); } public static void main(String[] args) throws Exception { System.out.println(" binary = " + toBinary(7, 4)); System.out.println(" binary = " + Integer.toString(7,2)); } } 

这是我几分钟前写的东西,只是搞乱了。 希望能帮助到你!

 public class Main { public static void main(String[] args) { ArrayList<Integer> powers = new ArrayList<Integer>(); ArrayList<Integer> binaryStore = new ArrayList<Integer>(); powers.add(128); powers.add(64); powers.add(32); powers.add(16); powers.add(8); powers.add(4); powers.add(2); powers.add(1); Scanner sc = new Scanner(System.in); System.out.println("Welcome to Paden9000 binary converter. Please enter an integer you wish to convert: "); int input = sc.nextInt(); int printableInput = input; for (int i : powers) { if (input < i) { binaryStore.add(0); } else { input = input - i; binaryStore.add(1); } } String newString= binaryStore.toString(); String finalOutput = newString.replace("[", "") .replace(" ", "") .replace("]", "") .replace(",", ""); System.out.println("Integer value: " + printableInput + "\nBinary value: " + finalOutput); sc.close(); } 

}

整数转换为二进制:

 import java.util.Scanner; public class IntegerToBinary { public static void main(String[] args) { Scanner input = new Scanner( System.in ); System.out.println("Enter Integer: "); String integerString =input.nextLine(); System.out.println("Binary Number: "+Integer.toBinaryString(Integer.parseInt(integerString))); } } 

输出:

input整数:

10

二进制数字:1010

使用内置函数:

 String binaryNum = Integer.toBinaryString(int num); 

如果您不想使用内置函数将int转换为二进制,那么您也可以这样做:

 import java.util.*; public class IntToBinary { public static void main(String[] args) { Scanner d = new Scanner(System.in); int n; n = d.nextInt(); StringBuilder sb = new StringBuilder(); while(n > 0) { int r = n%2; sb.append(r); n = n/2; } System.out.println(sb.reverse()); } } 

使用内置函数:

 String binaryNum = Integer.toBinaryString(int num); 

如果您不想使用内置函数将int转换为二进制,那么您也可以这样做:

 import java.util.*; public class IntToBinary { public static void main(String[] args) { Scanner d = new Scanner(System.in); int n; n = d.nextInt(); StringBuilder sb = new StringBuilder(); while(n > 0){ int r = n%2; sb.append(r); n = n/2; } System.out.println(sb.reverse()); } } 

这里是我的方法,固定的字节数是有点说服力的

 private void printByte(int value) { String currentBinary = Integer.toBinaryString(256 + value); System.out.println(currentBinary.substring(currentBinary.length() - 8)); } public int binaryToInteger(String binary) { char[] numbers = binary.toCharArray(); int result = 0; for(int i=numbers.length - 1; i>=0; i--) if(numbers[i]=='1') result += Math.pow(2, (numbers.length-i - 1)); return result; } 

最简单的方法是检查数字是否是奇数。 如果按照定义,最右边的二进制数将是“1”(2 ^ 0)。 在我们确定了这个之后,我们把这个数字转移到右边,并使用recursion来检查相同的值。

 @Test public void shouldPrintBinary() { StringBuilder sb = new StringBuilder(); convert(1234, sb); } private void convert(int n, StringBuilder sb) { if (n > 0) { sb.append(n % 2); convert(n >> 1, sb); } else { System.out.println(sb.reverse().toString()); } } 

这可以用伪代码表示为:

 while(n > 0): remainder = n%2; n = n/2; Insert remainder to front of a list or push onto a stack Print list or stack 

这应该是这样的:

 public static String toBinary(int number){ StringBuilder sb = new StringBuilder(); if(number == 0) return "0"; while(number>=1){ sb.append(number%2); number = number / 2; } return sb.reverse().toString(); } 

另一个可能的实现是:

另一个整数到二进制的实现

您也可以使用while循环将int转换为二进制。 喜欢这个,

 import java.util.Scanner; public class IntegerToBinary { public static void main(String[] args) { int num; String str = ""; Scanner sc = new Scanner(System.in); System.out.print("Please enter the a number : "); num = sc.nextInt(); while(num > 0) { int y = num % 2; str = y + str; num = num / 2; } System.out.println("The binary conversion is : " + str); sc.close(); } } 

源和参考 – 在Java示例中将int转换为二进制 。