I am reading a file as byte and converting them into binary string using
String.format("%8s", Integer.toBinaryString(resultByteArray[i] & 0xFF)).replace(' ', '0')
After that, using StringBuilder I have combined all strings into one string. Now I want to convert this string into an array like [1,0,1,0....].
Is there any way to do so?
Since you've already compiled it into one big String, you can immediately convert it to a char array. There's a method called .toCharArray() that you can use to return a char array.
Here's an example of this:
http://www.javatpoint.com/java-string-tochararray
Additionally, if you want to convert it to a different type of array, you could iterate through and add it to an array that has the same size as the char array.
Best of luck!
Related
Using the Java API for NetCDF, I have an HDF5 file with an array of type CHAR, which according to the documentation is similar to strings containing only ASCII characters: "The char type contains uninterpreted characters, one character per byte. Typically these contain 7-bit ASCII characters." In HDFView, an example of one of thee values in the array is "13".
I know that for an array of integers I can get them all as a Java array like this:
int[] data = (int[]) netCDFArray.get1DJavaArray(int.class);
But how do I get back an array of this CHAR type? Unfortunately the documentation I referenced is of no help.
The following cannot be correct, because some of the items are more than single characters:
char[] data = (char[]) netCDFArray.get1DJavaArray(char.class);
The following attempts all throw a ForbiddenConversionException:
char[] data = (char[]) netCDFArray.get1DJavaArray(char.class);
char[][] data = (char[][]) netCDFArray.get1DJavaArray(char[].class);
String[] data = (String[]) netCDFArray.get1DJavaArray(String.class);
If I use netCDFArray.toString() I see my array of strings, because ArrayChar uses a StringIterator. I too could use such an iterator and do something with each string, I suppose. But I don't need to get an int iterator to retrieve integers. How can I efficiently retrieve all strings of a CHAR type in one go, analogous to how I can retrieve integers (see above)? I would be content with retrieving a Java Strings[], CharSequence[], or char[][].
It seems that a NetCDF string of type CHAR is represented logically as a two-dimensional array of type char, but internally it is stored as a single array of type char. Therefore, the most efficient way to retrieve the data is the following:
char[] data = (char[]) netCDFArray.get1DJavaArray(char.class);
One must then extract the individual strings from this single array:
assert netCDFArray.getRank()==2 : "Expected a two-dimensional logical array of chars.";
int stringLength=netCDFArray.getShape()[1];
int stringCount=netCDFArray.getShape()[0];
//iterate through stringCount positions of stringLength length
The added twist is that apparently strings are stored as zero-terminated, that is, the supposedly fixed-length strings apparently may be variable-length strings of less than stringLength using ASCII 0 as a delimiter. I derived this from the code; I couldn't find it in the documentation.
In my program I'm trying to compare my char array asterixA[] to a String word in an if condition like:
if (word.equals(asterixA))
but it's giving me an error. Is there any other way I can compare them?
you have to convert the character array into String or String to char array and then do the comparision.
if (word.equals(new String(asterixA)))
or
if(Arrays.equals(word.toCharArray(), asterixA))
BTW. if is a conditional statement not a loop
You seem to be taking the "A String is an array of chars" line too literal. String's equals method states that
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
It all depends of the circumstances, but generally you compare two objects of the same type or two objects belonging to the same hierarchy (sharing a common superclass).
In this case a String is not a char[], but Java provides mechanisms to go from one to the other, either by doing a String -> char[] transformation with String#toCharArray() or a char[] -> String transformation by passing the char[] as a parameter to String's constructor.
This way you can compare both objects after either turning your String into a char[] or vice-versa.
You can compare the arrays:
if (Arrays.equals(asterixA, word.toCharArray()) {}
do as follows: if (word.equals(new String(asterixA))) { ... }
I am making a hangman game. I have two char arrays and I need to check if they are equal.
One of them has letters and underscores: char checkLetter[]
The other one has only letters: char currentWord[]
Eventually, after the user has guessed all the words in the checkLetter[] array it will also consist of only letters. But I need to keep continually checking (in a boolean method) if the array into which they guess and their letters get stored, is the exact same as the word they are trying to guess.
If I could convert both of the arrays into strings then I could check them for equality. I am not experienced, and I don't know how to do this. Any help help would be appreciated!
You don't need to convert them to strings at all. Use Arrays.equals().
you can convert an char array into string using String's overloaded constructor which takes char[] array as argument.
char[] carr ;
String s = new String(carr);
You may use new String(char[] value)` to create String from char array.
Use the String-constructor:
String str = new String(yourCharArray);
However, that's useless; use Arrays.equals(arr1, arr2) instead.
You really don't need to convert the array, but if you really want to then
try using String word = currentWord.toString() to convert the char array.
I have a big string having at most 100000 character. Instead of using string.charAt[index] to read a character from the string, I converted that string into char array using string.toCharArray() method and now i am working with charArray[index]. which takes less time than string.charAt[index] method. However i want to know that, is there any other way which is faster than string.toCharArray(); method?
I do not think there is a faster way. But please correct me!
A String instance is backed by a char array. charAt() does some index checks which may be the cause for it being slower than working with the array returned by toCharArray(). toCharArray() simply does a System.arraycopy() of the backing array.
Can you explain what is happening in this line of code? Specially what is args[0].tocharArray ?
char[] password = args[0].toCharArray();
char[] is your datatype. "char" is a single 16 bit character, and char[] is a character array.
args[0] is the first argument that's passed to the program.
.toCharArray(); converts that argument to a character array.
This line of code is basically taking an argument, turning it into a character array, and storing it in "password" which is a character array.
It's converting the first argument of a Java program—passed as a String[] to the main method—to a character array.
Most password-oriented APIs use char[] so that after calling the method, the caller can "zero-ize" the array, effectively erasing the password from memory. Since Java String instances are immutable, they can't be zero-ized. However, in practice, it's hard to get user-input without using a String. All web frameworks will convert passwords submitted in a web request to a String. Swing password widgets and Java 6's Console class will input char[], however.
args[0] is presumably a String array. Thus it is a call to the method String.toCharArray() which converts a String to an array of chars.
EDIT: Corrected my answer after comment.
It converts the first item of the args array (presumably, the first command line argument passed to the main method, which is of string type) to an equivalent array of chars (an array containing all the chars that build up the string).
args is an array.
The type of the array contains a function called toCharArray which returns an array of characters. NOTE: args is most likely an array of strings
So it takes the string in args[0] and creates an array of characters which represents that string.
args[0] - representing a string
toCharArray() - convert this string to char array
I thought this, toCharArray(), might help.