I need to convert a string to a corresponding int array in java. i wrote the following code but its not working as i expected .
String temp= "abc1";
int[] intArray = new int[temp.length()];
for (int i = 0; i < temp.length(); i++) {
intArray[i] = Integer.parseInt(temp[i]);
}
I have written an rc4 encryption program which take the key and plain text as int arrays. So I need to convert the user specified key to int array before passing it to the encryption function. Is this the correct way of using key in encryption programs?
Use this to get the ASCII code
intArray[i] = (int)temp.charAt(i);
You can convert string to charArray. Travesing charArray you can convert as:
char[] c = inputString.toCharArray()
for(int i=0;i<c.length;i++)
int n = Integer.parseInt(c[i]);
if you are Using Java8 or higher then it might be helpful for you
String temp="abc1";
int[] intArray =temp.chars().map(x->x-'0').toArray();
System.out.println(Arrays.toString(intArray ));
int array would be [49, 50, 51, 1]
I solved this by using byte instead of int. Modified the rc4 to take byte array.
converted the string to byte using
String Nkey = jTextField2.getText();
jTextField3.setText(Nkey);
int i;
byte[] key = Nkey.getBytes();
Related
I am trying to create a random WPA password generator using java to give me as secure of a Wifi password as possible, which can be changed when I desire.
Is there a way to quickly populate an array with all of the characters which would be allowed to be used in a WPA(2) password as opposed to manually entering the chars one by one?
Shortest way I can think of would be:
String s = new String(IntStream.rangeClosed(32, 126).toArray(), 0, 95);
char[] ascii = s.toCharArray();
If you're just generating a sequence of characters, you don't need to make an array of all ASCII characters:
String s = new String(random.ints(length, 32, 127).toArray(), 0, length);
int pwLength = 8;
Random numberGenerator = new Random();
char[] pw = new char[pwLength];
for(int i = 0; i<pwLength; i++){
pw[i] = numberGenerator.nextInt(95) + 32;
}
Well the range of valid ascii values are 32-126 (see https://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters). So you could simply generate a random number between those values for each character and then turn the ascii value to a string.
You do
Character.toString ((char) i); or String.valueOf(Character.toChars(int)) to turn an ascii value to a string.
Put together 8-63 of these characters and you have yourself a random wpa2 passphrase.
private void buildPassword() {
for (int i = 33; i < 127; i++) {
l.add(i);
}
l.remove(new Integer(34));
l.remove(new Integer(47));
l.remove(new Integer(92));
for (int i = 0; i < 10; i++) {
randInt = l.get(new SecureRandom().nextInt(91));
sb.append((char) randInt);
}
str = sb.toString();
}
I need for my app this int[] format as I wrote in the title.
I'm fetching the value out of a HashMap which is filled out of an XML file with this format [00, 00, 00, 00], but I need it for every int with 0xin front.
I tried to "convert" it via a for-loop:
for(int i = 0; i < value.length; i++){
value[i] = "0x" + value[i];
}
but I cant convert from String to int this way.
Then I tried to change it directly in the XML file, but then my app crashes with an NPE.
Now I want to know if there is a solution to my problem.
EDIT:
For further explanation:
I tried this before, but it did not work:
public int[] getValue(Map map, String key) {
Map keyMap = map;
int[] value = {};
#SuppressWarnings("rawtypes")
Iterator iter = keyMap.entrySet().iterator();
while (iter.hasNext()) {
#SuppressWarnings("rawtypes")
Map.Entry mEntry = (Map.Entry) iter.next();
if (mEntry.getKey().equals(key)) {
value = (int[]) mEntry.getValue();
}
}
return value;
}
Then I tried one of the answers and added
for(int i = 0; i < value.length; i++){
value[i] = Integer.valueOf(String.valueOf(value[i]), 16);
}
before the return. Now it works... but I dont know why it works :(
In any typed language int/number values have no format. The way Java stores int values is as 4 byte number using Two's complement, so 0 or 0x0 or 00 (octal) or 0b is the same value: 32 zeros in a binary word.
SECOND EDIT:
I think you are over-engineering. What you should use is a
Map<String,int[]> map;
then, if you write map.get(key), you'll obtain what you're looking for.
NEW EXPLANATION: The previous solution worked because when you read the XML stored the values as decimal ones:
String s="80";
int i= Integer.valueOf(s); //it stores 80 (decimal value)
int value= Integer.valueOf(String.valueOf(i),16); //It stores 128, or 0x80 (hex value)
for(int i = 0; i < value.length; i++){
value[i] = Integer.valueOf(String.valueOf(value[i]), 16);
}
Try this. If you want the hex value. Or in case if you want hex string you can use below code.
Integer.toHexString(integerValue)
i need to convert char array into string , the problem in doing this is......i need to convert the character in char array of particular length say k to string. ie, char array is "b" .b takes value dynamically.....for instance take as "p,a,p,e,r,s" now k value also dynamic ,for this word "k=5" ,and then only 4 characters in char array "b" should be converted into string...ie the string should print as "paper"........
the code what i have now is
for(int c=0;c<=k;c++)
{
System.out.print(b[c]);
}
str=new String(b);
System.out.println(str);
where b[c] prints correct value(in char array) as "paper". While converting to string str (in program) it prints as "papers" itself....can anyone give me solution for this?
You can use a different constructor of String that lets you specify the array along with the start point and number of characters to use.
In your case, you would try:
str = new String( b, 0, k );
char newArr[] = new char[k];
for (int i = 0; i < k; i++) {
newArr[i] = b[i];
System.out.print(b[i]); // print until the kth index
}
return new String(newArr);
I made a function that requires an int[], Conflict(int[] tab), and as you all know we extract information from a JTextField in the form of an String (s) ,so I then convert in into an integer with:
int input=Integer.parseInt(s);
Now, I found this way to do it :
int input= 1234;
String input= String.valueOf(input);
Vector<int> tab = new Vector<int>();
for (int cpt = 0; cpt <input.length(); cpt++)
{
tab.add(Integer.valueOf(input.substring(cpt, cpt+1)).intValue());
}
but I'm looking for a simple way without the vector ,so again the Question is How can I convert the input (int) into a table of int[] so that I can use it in the Conflict function?
Here is an answer to my question for all of you who's looking to turn an int into an int[] using a string with a very smart simple trick
tcst = ""+inputc;
int tcn = tcst.length();
int[] tctab = new int[tcn];
for(int h=0; h<tcn; h++){
tctab[h] = (int) Integer.parseInt("" + tcst.charAt(h));
}
If you have a String of number like "12345" best way is to convert it into char array using toCharArray() method of string. Once you get a char array of numbers these char can be used as integer in you program. just when you get an element from the char array and convert it into int by using getNumericValue() method of character
If your input is a String (from a JTextField), then I don't see why you're converting it to an integer, just to convert it to a String again.
To go from String to int[], here's a way to do it.
char[] chars = inputString.toCharArray();
int[] result = new int[chars.length];
for(int i=0; i<chars.length; i++){
result[i] = Character.getNumericValue(chars[i]);
}
I got a little problem here with Java and I am fairly new to it.
My program reads a String via InputStreamReader and saves it in the String input.
How do I save the elements of the String in a 2d char array with n x m elements?
Edit:
I think I´ve got a solution:
I used 2 for-loops (is that the right english translation for it? ) and .toCharArray to convert the String.
public static char[][] transform (String text, int arrBreite, int arrLaenge) {
char[][] returnArray = new char[arrBreite][arrLaenge];
char[] buffer = text.toCharArray();
for (int i = 0; i < arrBreite; i++) {
for (int j = 0; j <arrLaenge; j++) {
if (((i * arrBreite) + j) > buffer.length - 1) returnArray[i][j] = " ".charAt(0);
else returnArray[i][j] = buffer[(i*arrBreite)+j];
}
}
return returnArray;
}
Thanks for your help guys.
You can use the toCharArray() method to get a char array from your String.
If you need to split with a given delimiter to determine the array lines, you use first the Split method on the String, then use toCharArray to create your 2 dimensional array.
You should use String.toCharArray().