XOR mask in java android - java

User enter code word and text to encrypt and program should put XOR mask - code on text - and turn back to normal , but it just put on mask and dont turn back to normal look , why ?
public void onClick(View arg0) {
code = etCode.getText().toString();
text = etText.getText().toString();
while(code.length()<text.length()){
code+=code;
}
char[] Ccode = code.toCharArray();
char[] Ctext = text.toCharArray();
for(i=0;i<Ctext.length;i++){
Ctext[i]^=Ccode[i];
}
rezult=Ctext.toString();
for(i=0;i<Ctext.length;i++){
Ctext[i]^=Ccode[i];
}
rezult+="\n";
rezult+=Ctext.toString();
tvMain.setText(rezult);
}
});
if I enter code : code , text : text
it shows:
[C#40527808
[C#40527808

You output the address of the array. You want the content.
Arrays have not useful toString() mwthod.
change
rezult=Ctext.toString();
to
rezult=new String(Ctext);
same for
rezult+=Ctext.toString();
=>
rezult+=new String(Ctext);

You cannot use toString() to convert a char array to String.
Use tvMain.setText(new String(Ctext));

In Java, the default toString() operation on arrays is to convert to an internal identifier, which is what you're seeing. Try using:
rezult = new String(Ctext);
...
rezult += new String(Ctext);
or, depending on what you want to display (since it's not clear to me that Ctext always contains displayable characters):
rezult = Arrays.toString(Ctext);
...
rezult += Arrays.toString(Ctext);
This will give you a comma-separated array of character values, surrounded by square brackets.

Related

Setting the color of multiple characters of TextView in java

I will do my best to explain my problem, sorry if I am not clear. Basically I want to set the color of individual characters of a text, and then display them in Android App Development Kit. The problem I am having is that I am taking an array of the characters, and don't know how to set the color of them to a certain hexadecimal value.
For instance if the String is "hello". I would want each character to be a different color.
So I would take 'h' and assign it the hexadecimal value of "#000000". And then display it using xml. Is this possible? Here is what I am attempting to do now with my code.
String end = "";
for (int p = 0; p < charzart.size(); p++) {
if (charzart.get(p).equals(" ")) {
}
else{
Spannable colorSpan = new SpannableString(charzart.get(p));
int fake = Integer.parseInt(color.get(p));
colorSpan.setSpan(new ForegroundColorSpan(fake), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
end += colorSpan;
}
}
output.setText(end);
Right now my code doesn't work and I am not sure why. So i am running through charzart which holds my characters. And then attempting to assign the hexadecimal value ( which I have in another array list called color). I check to see if there is a space, if there is I ignore it and move on until i reach a character. From there, I get the character, set it to a spannable. I then get the color, apply it to the spannable, add it to a string and at the end set the TextView to that string.
Basically I want to know how to assign a hexadecimal value to a character, which will then be outputted by XML.

how to replace a word with in contains() Method form a JtextPane

Well, I'm trying to replace a word by using contains() Method:
String z = tfB.getText().toString();
String show = textPane.getText().toString();
if(show.contains(z)){
// how I specify the word that were found and change it without
effecting anything with in that line
}
well what I main by that:
What I'm trying to do is get the value from the user.
then search if it found replace it with something. For example:
String x = "one two three four five";
It should set the textPane to "one two 3 four five"
or
"one two 3-three-3 four five"
could any one please tell me how to do it.
Thank you
What I'm trying to do is get the value from the user. then search if it found replace it with something.
Don't use the contains() method because you will need to search the text twice:
once to see if the text is found in the string
again to replace the text with a new string.
Instead, use the String.indexof(...) method. It will return the index of the text IF it is found in the String.
Then you should replace the text directly in the Document of the text pane, not in the String itself. So the code would be something like:
int length = textPane.getDocument().getLength();
String text = textPane.getDocument().getText(0, length);
String search = "abc...";
int offset = text.indexOf(search);
if (offset != -1)
{
textPane.setSelectionStart(offset);
textPane.setSelectionEnd(offset + search.length();
textPane.replaceSelection("123...");
}
Also, not that you get the text from the Document, not the text pane. This is to make sure the offsets are correct when you replace the text in the Document. Check out Text and New Lines for more information on why this is important.

Create string with emoji unicode flag countries

i need to create a String with a country flag unicode emoji..I did this:
StringBuffer sb = new StringBuffer();
sb.append(StringEscapeUtils.unescapeJava("\\u1F1EB"));
sb.append(StringEscapeUtils.unescapeJava("\\u1F1F7"));
Expecting one country flag but i havent..How can i get a unicode country flag emoji in String with the unicodes characters?
The problem is, that the "\uXXXX" notation is for 4 hexadecimal digits, forming a 16 bit char.
You have Unicode code points above the 16 bit range, both U+F1EB and U+1F1F7. This will be represented with two chars, a so called surrogate pair.
You can either use the codepoints to create a string:
int[] codepoints = {0x1F1EB, 0x1F1F7};
String s = new String(codepoints, 0, codepoints.length);
Or use the surrogate pairs, derivable like this:
System.out.print("\"");
for (char ch : s.toCharArray()) {
System.out.printf("\\u%04X", (int)ch);
}
System.out.println("\"");
Giving
"\uD83C\uDDEB\uD83C\uDDF7"
Response to the comment: How to Decode
"\uD83C\uDDEB" are two surrogate 16 bit chars representing U+1F1EB and "\uD83C\uDDF7" is the surrogate pair for U+1F1F7.
private static final int CP_REGIONAL_INDICATOR = 0x1F1E7; // A-Z flag codes.
/**
* Get the flag codes of two (or one) regional indicator symbols.
* #param s string starting with 1 or 2 regional indicator symbols.
* #return one or two ASCII letters for the flag, or null.
*/
public static String regionalIndicator(String s) {
int cp0 = regionalIndicatorCodePoint(s);
if (cp0 == -1) {
return null;
}
StringBuilder sb = new StringBuilder();
sb.append((char)(cp0 - CP_REGIONAL_INDICATOR + 'A'));
int n0 = Character.charCount(cp0);
int cp1 = regionalIndicatorCodePoint(s.substring(n0));
if (cp1 != -1) {
sb.append((char)(cp1 - CP_REGIONAL_INDICATOR + 'A'));
}
return sb.toString();
}
private static int regionalIndicatorCodePoint(String s) {
if (s.isEmpty()) {
return -1;
}
int cp0 = s.codePointAt(0);
return CP_REGIONAL_INDICATOR > cp0 || cp0 >= CP_REGIONAL_INDICATOR + 26 ? -1 : cp0;
}
System.out.println("Flag: " + regionalIndicator("\uD83C\uDDEB\uD83C\uDDF7"));
Flag: EQ
You should be able to do that simply using toChars from java.lang.Character.
This works for me:
StringBuffer sb = new StringBuffer();
sb.append(Character.toChars(127467));
sb.append(Character.toChars(127479));
System.out.println(sb);
prints 🇫🇷, which the client can chose to display like a french flag, or in other ways.
If you want to use emojis often, it could be good to use a library that would handle that unicode stuff for you: emoji-java
You would just add the maven dependency:
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>1.0.0</version>
</dependency>
And call the EmojiManager:
Emoji emoji = EmojiManager.getForAlias("fr");
System.out.println("HEY: " + emoji.getUnicode());
The entire list of supported emojis is here.
I suppose you want to achieve something like this
Let me give you 2 example of unicodes for country flags:
for ROMANIA ---> \uD83C\uDDF7\uD83C\uDDF4
for AMERICA ---> \uD83C\uDDFA\uD83C\uDDF8
You can get this and other country flags unicodes from this site Emoji Unicodes
Once you enter the site, you will see a table with a lot of emoji. Select the tab with FLAGS from that table (is easy to find it) then will appear all the country flags. You need to select one flag from the list, any flag you want... but only ONE. After that will appear a text code in the message box...that is not important. Important is that you have to look in the right of the site where will appear flag and country name of your selected flag. CLICK on that, and on the page that will open you need to find the TABLE named Emoji Character Encoding Data. Scroll until the last part of table where sais: C/C++/Java Src .. there you will find the correct unicode flag. Attention, always select the unicode that is long like that, some times if you are not carefull you can select a simple unicode, not long like that. So, keep that in mind.
Indications image 1
Indication image 2
In the end i will post a sample code from an Android app of mine that will work on java the same way.
ArrayList<String> listLanguages = new ArrayList<>();
listLanguages.add("\uD83C\uDDFA\uD83C\uDDF8 " + getString(R.string.English));
listLanguages.add("\uD83C\uDDF7\uD83C\uDDF4 " + getString(R.string.Romanian));
Another simple custom example:
String flagCountryName = "\uD83C\uDDEF\uD83C\uDDF2 Jamaica";
You can use this variable where you need it. This will show you the flag of Jamaica in front of the text.
This is all, if you did not understand something just ask.
Look at Creating Unicode character from its number
Could not get my machine to print the Unicode you have there, but for other values it works.

Cipher/decipher textArea text using XOR

I have got a code which should cipher text from my textArea with some sort of String key, after the button is pressed down. The problem is, that this method can cipher text using only one character, not the whole string. I need to have a little longer key, so string is needed here. How can I change that?
btnCipher.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent klik) {
String textToCipher = textArea.getText();
String cipherKey = textField.getText();
String cipheredText = "";
int xor;
char temp;
for (int i=0; i<textToCipher.length(); i++){
xor = textToCipher.charAt(i)^cipherKey; //error
temp = (char)xor;
cipheredText += temp;
}
textArea.setText(cipheredText);
}
});
If your strings are in UTF-8 encoding, then using exclusive or between two strings (assume equal length for a moment) may NOT produce a valid UTF-8 result (in particular, you may create a "continuation" character if you have "a" and "æ" in the same position). Look here for a deeper discusion.
What is the use-case that you are trying to handle?
Change that line to:
xor = textToCipher.charAt(i) ^ cipherKey.charAt(i % cipherKey.length);
As mentioned in this post, while String class doesn't really mind storing a string with unpaired surrogates (which is actually invalid for a UTF-16 format), you will run into trouble encoding/decoding the string in other charset and when using code point related methods.

How to search the whole string for a specific word?

I have this code which searches a string array and returns the result if the input string matches the 1st characters of a string:
for (int i = 0; i < countryCode.length; i++) {
if (textlength <= countryCode[i].length()) {
if (etsearch
.getText()
.toString()
.equalsIgnoreCase(
(String) countryCode[i].subSequence(0,
textlength))) {
text_sort.add(countryCode[i]);
image_sort.add(flag[i]);
condition_sort.add(condition[i]);
}
}
}
But i want to get those string also where the input string matches not only in the first characters but also any where in the string? How to do this?
You have three way to search if an string contain substring or not:
String string = "Test, I am Adam";
// Anywhere in string
b = string.indexOf("I am") > 0; // true if contains
// Anywhere in string
b = string.matches("(?i).*i am.*"); // true if contains but ignore case
// Anywhere in string
b = string.contains("AA") ; // true if contains but ignore case
I have not enough 'reputation points' to reply in the comments, but there is an error in the accepted answer. indexOf() returns -1 when it cannot find the substring, so it should be:
b = string.indexOf("I am") >= 0;
Check out the contains(CharSequence) method
Try this-
etsearch.getText().toString().contains((String) countryCode[i]);
You could use contains. As in:
myString.contains("xxx");
See also: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html, specifically the contains area.
Use the following:
public boolean contains (CharSequence cs)
Since: API Level 1
Determines if this String contains the sequence of characters in the CharSequence passed.
Parameters
cs the character sequence to search for.
Returns
true if the sequence of characters are contained in this string, otherwise false
Actually, the default Arrayadapter class in android only seems to search from the beginning of whole words but by using the custom Arrayadapter class, you can search for parts of the arbitrary string. I have created the whole custom class and released the library. It is very easy to use. You can check the implementation and usage from here or by clicking on the link provided below.
https://github.com/mohitjha727/Advanced-Search-Filter
You can refer to this simple example-
We have names " Mohit " and "Rohan" and if We put " M" only then Mohit shows up in search result, but when we put "oh" then both Mohit and Rohan show up as they have the common letter 'oh'.
Thank You.

Categories