From random integers to an actual String

拟墨画扇 提交于 2019-12-25 18:58:35

问题


This is a code that takes in an array of all the printing characters of the ASCII table. I am trying to make it so that any String message in the form of integers (e.g. String "aba" that is converted 97098097 can be put back into its original String form. 100101101 can be taken and made back into "dee". I've really tried hard with this method but it does not seem to be working, especially when it comes to numbers and such please help me. It is in Java by the way and I am using Eclipse.

public static String IntToString (){


int n = 0;
String message = "";
String message2 = null;
String [] ASCII = {" ","!","\"","#","$","%","&","\'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","{","|","}","~"};
String IntMessage = result.toString();
String firstChar = IntMessage.substring(0,2);
if (IntMessage.substring(0,1)=="1" && IntMessage.length()%3==0)
{
    for (int x = (IntMessage.length() % 3 - 3) % 3; x < IntMessage.length()-2; x += 3)
        n = Integer.parseInt(IntMessage.substring(Math.max(x, 0), x + 3));
        message=message.concat(ASCII[n-31]);
return message;
}
else if (IntMessage.length()%3==2)
message2=ASCII[(Integer.parseInt(firstChar))-31];
        for (int x = 2; x < IntMessage.length()-2; x += 3)
            n = Integer.parseInt(IntMessage.substring(x, x + 3));
            message=message2+=ASCII [n - 31];
return message;

回答1:


It would seem that your encoding scheme is, er, crazy.

First, you take the ASCII value of a string, then take the character representation of that ASCII value, then store it as a string.

So "abc" => {97, 98, 99} => "979899".

But since you are using ASCII, which can have values of 100 or more, you are padding your ints with 0 if they are under 100:

"abc" => {97, 98, 99} => {"097", "098", "099"} => "097098099"

But you decide to do this only sometimes, because somehow

"aba" => "97098097"

That is, the first "a" is turned into "97", but the last "a" is turned into "097".

I'd say you should fix your encoding scheme first.

Also, these are hopefully not "random integers" because you are trying to turn them into sensible strings. Otherwise a simple mapping such as base64 would easily map any integers to strings, they just might not make much sense.

In fact, they aren't even really integers. You're storing your encoded strings as strings.




回答2:


public static void main(String[] srgs){
    String aaa = "100101101";
    String[] a = split(aaa, 3);

    String s = "";

    for(int i=0;i<a.length;i++){
        char c = (char)Integer.parseInt(a[i]);
        s += Character.toString(c);
    }
    System.out.println(s);
}

public static String[] split(String str, int groupIndex){
    int strLength = str.length();
    int arrayLength = strLength/groupIndex;
    String[] splitedArray = new String[strLength/groupIndex];

    for(int i=0;i<arrayLength;i++){
        String splitedStr = str.substring(0, groupIndex);
        str = str.substring(groupIndex, str.length());
        arrayLength = str.length();
        splitedArray[i] = splitedStr;
    }
    return splitedArray;
}

The most important is that ASCII string covert to Char value, than turn it to real Character value in the string. The ASCII code length need be fix by 3 can be helpful in this case.



来源:https://stackoverflow.com/questions/7591858/from-random-integers-to-an-actual-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!