i want to store integer into 200 byte register. This is the code.
please help. ThanksCode:
int[] register = new int[200];
for(int j = 0; j < 200; j++)
{
register[j] = binary.charAt(j);
register[j] = digit;
System.out.println(register[j]);
Printable View
i want to store integer into 200 byte register. This is the code.
please help. ThanksCode:
int[] register = new int[200];
for(int j = 0; j < 200; j++)
{
register[j] = binary.charAt(j);
register[j] = digit;
System.out.println(register[j]);
What is it you want the code to do?
String.charAt returns a character element, not an integer, so you'll need to convert "binary.charAt(j)" to an Integer before you store it in your "int[] register" array
i've already converted a text into binary.what i want the code to do is to place each of binary bits into the register.for example,let the text be "axcbd" and here is the binary for the text --> a= 1010101 , b 1101010 , c = 1110101, d = 1111010, x = 1111101
I want to make this binary (10101011111101111010111010101111010) as an initialization for the register to run.hopefully,anyone here can help me to fix this problem.tq
So? What do you want us to do about it? If you ask a specific question then you will get a specific answer.
Is your binary String always going to be 200 chars long? If not you will get an IndexOutOfBoundsExceptionCode:for(int j = 0; j < 200; j++)
Do you realise that the first line is totally pointless? What ever value is inserted into the array on this line is immediately overwritten by the second line.Code:register[j] = binary.charAt(j);
register[j] = digit;
Ah, i see. But the register is an integer array, so once you get the binary representation of each letter you'll need to convert it into a string as you've done in the bracket:
Once you have a long string of all the numbers you can use String.split(String regex) to cut it after each number into an array, and then cast the array of string-numbers into your int register arrayQuote:
I want to make this binary (10101011111101111010111010101111010)
and yet still no question
but i thought the register was initialised...
Quote:
Code:int[] register = new int[200];
sorry,my mistake.what i need to do to make those register call the each of binary bits and put it in that register?
That is still a statement. Sticking a question mark on the end doesn't make it a question.
You have a binary String which may be shorter than 200 chars long, so you should loop the length of the String and not hard code it to be 200.
For each char in the String convert it to an int and insert into your array.
Done.
First convert your binary to a String.
Code:String binaryString = "10101011111101111010111010101111010";
String[] registerString = binaryString.split("(?<=\\G.)");
for (i=0;i<registerString.length;i++) {
register[i] = Integer.parseInt(registerString[i]);
}
Using String.split and a regular expression might be overkill in this situation. It can be achieved simply enough with charAt.
Anything else would require more logic which is obviously lacking in this case...
No it wouldn't. In fact it requires less.
Eliminates an array and the call to String.split. Note I deliberatley have omitted the actual code because it is the OP's job to write it not me.Code:binaryString
loop each char in binaryString {
array[postion] = convert char to int;
}