Replacing characters in String

I am trying to create an Android application that converts regular hexadecimal code to inverted, which is used in smali. Everything works fine, except when I use the replace or replaceAll method for String, it even replaces characters that have already been replaced

For example,

String stringInvert = string.replace("F", "0")
    .replace("E" , "1")
    .replace("D" , "2")
    .replace("C" , "3")
    .replace("B" , "4")
    .replace("A" , "5")
    .replace("9" , "6")
    .replace("8" , "7")
    .replace("7" , "8")
    .replace("6" , "9")
    .replace("5" , "A")
    .replace("4" , "B")
    .replace("3" , "C") 
    .replace("2" , "D")
    .replace("1" , "E")
    .replace("0" , "F"); 

As you can see, the first F changes to 0, and similarly the other letters also change, but later 0 changes to F, which also changes the already changed F to F. Thus, in general, only letters / numbers that before 7 are addressed ( how they are replaced in the code), while others remain the same due to double inversion. I even tried the replaceAll method, it gives the same result. So is there any other way or work on this issue?

- http://pastebin.com/dB23JmQG

, , AIDE

+4
5

Map<Character, Character>, .

String.

Map<Character, Character> m = new HashMap<>();
m.put('F','0');
....

StringBuilder sb = new StringBuilder();
for(char c : originalString.toCharArray()){
   sb.append(map.get(Character.toUpperCase(c)));
} 
String finalString = sb.toString();
+5

:

String stringInvert = string
.replace("F", "null")
.replace("E" , "one")
.replace("D" , "two")
.replace("C" , "three")
.replace("B" , "four")
.replace("A" , "five")
.replace("9" , "six")
.replace("8" , "seven")
.replace("7" , "8")
.replace("6" , "9")
.replace("5" , "A")
.replace("4" , "B")
.replace("3" , "C") 
.replace("2" , "D")
.replace("1" , "E")
.replace("0" , "F")
.replace("null", "0")
.replace("one","1")
.replace("two","2" )
.replace("three","3" )
.replace("four","4" )
.replace("five","5" )
.replace("six","6" )
.replace("seven","7" );

, :)

+1

, char, .

. , : FF00F0​​p >

  • F . '*' . ** 00 * 0
  • 0 F : ** FF * F
  • , * 0.

, .

+1

, , , , char/int, :

static char convert(char c){
    int n = c;
    n = n - 70; // Adjust for capital F
    n = n * -1; // So earlier letters become larger numbers
    n += 48; // Adjust back to zero
    return (char)n;
}

( ), .

, , char .

0

Integer API:

String stringToInvert = "A1F56C";

StringBuilder sb = new StringBuilder();
for (char c : stringToInvert.toCharArray()) {
    sb.append(Integer.toHexString(15 - Integer.valueOf(c + "", 16)));
}

System.out.println(sb.toString().toUpperCase());

:

5E0A93

0

Source: https://habr.com/ru/post/1526910/


All Articles