How do I convert an array of integers to binary?

Rashid Raza :
for (int i = 0; i < n; i++) {
    arr[i] = scanner.nextInt();
}
String[] bin = new String[n];
for (int i = 0; i < n; i++) {
    bin[i] = Integer.toBinaryString(arr[i]);
}

The above code will convert the the whole array of integers into an array of Strings (containing binary format of the input string), but there is a caveat.

For Example:
If the input array is: 2 3 7 10
The binary string array will be: 10
11
111
1010

But I want the output array to be like the following:
0010
0011
0111
1010

#2
If the input array is: 2 10 20
The binary string array will be:
10
1010
10100

But I want the output array to be like the following:
00010
01010
10100

hev1 :

To have all of the binary strings match the length of the longest String, you can first find the longest length and then use String#format and String#replace to pad with zeroes.

int maxLen = 0;
for (int i = 0; i < n; i++) {
    bin[i] = Integer.toBinaryString(arr[i]);
    maxLen = Math.max(maxLen, bin[i].length());
}
for (int i = 0; i < n; i++) {
    if (bin[i].length() != maxLen)
        bin[i] = String.format("%" + maxLen + "s", bin[i]).replace(' ', '0');
    System.out.println(bin[i]);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert an integer to binary in JavaScript?

How can I convert (binary) integer into array?

How do I validate an array of integers in Laravel

How can I convert a string of numbers to an array or vector of integers in Rust?

How do I convert an array of integers to an array of objects?

How do I convert a vector of strings to a vector of integers in a functional way?

How do I convert an ArrayList of integers to a double[]?

How do I open a binary matrix and convert it into a 2D array or a dataframe?

How can I convert the array of integers to rows?

In pandas, how do I convert a series of float or none to strings with integers

How to convert array of Integers to binary encoding in TensorFlow or Numpy?

How do I convert an image in a np array to the same format as reading that image using binary read

How do I convert an array of floats into an array of unique integers?

How can I convert a array of integers of a JPG image to a JPG image?

How to read a binary file in python and convert it into an array of integers?

How do I convert a character into binary?

How do I convert this to an array?

How do I convert a list of integers into bytes?

Convert numpy array of integers to 12 bit binary

Convert a binary file into an array of integers directly in python?

How do I a split a line of integers into array?

How do I convert 'Single' to binary?

How do i convert Swift strings into Integers?

I got a array contains integers like 737511, how do I convert every integer in that array into date like 2020-03-27

how do I convert a tuple of floats to a tuple of integers

How do I convert a list of integers to a string - Python

How do i store a binary number in an array?

How do I convert a matrix of integers to a matrix of lists of integers in numpy?

How can I convert an ARRAY of INTEGERS into a JSON dict?