从随机整数到实际字符串 [英] From random integers to an actual String

查看:100
本文介绍了从随机整数到实际字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个code,它发生在ASCII表的所有打印字符数组。我试图让这个在整数形式的字符串消息(如字符串ABA即转换97098097可放回原来的字符串形式。100101101可拍摄并取得进民退嘀我'真的已经用这种方法很努力,但似乎并不奏效,尤其是当它涉及到数字和这种请帮助我。这是在Java中的方式,我使用Eclipse。

 公共静态字符串IntToString(){
INT N = 0;
字符串消息=;
字符串消息2 = 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\",\"{\",\"|\",\"}\",\"~\"};
串IntMessage = result.toString();
串firstChar = IntMessage.substring(0,2);
如果(IntMessage.substring(0,1)==1和安培;&安培; IntMessage.length()%3 == 0)
{
    为(中间体X =(IntMessage.length()%3 - 3)%3; X&下; IntMessage.length() - 2; X + = 3)
        N =的Integer.parseInt(IntMessage.substring(Math.max(X,0)中,x + 3));
        消息= message.concat(ASCII [N-31]);
返回消息;
}
否则如果(IntMessage.length()%3 == 2)
消息2 = ASCII [(的Integer.parseInt(firstChar)) - 31];
        为(中间体X = 2; X&下; IntMessage.length() - 2; X + = 3)
            N =的Integer.parseInt(IntMessage.substring(X,X + 3));
            消息=消息2 + = ASCII [N - 31]。
返回消息;


解决方案

这似乎是你的编码方案就是,呃,疯了。

首先,你需要一个字符串的ASCII值,然后乘坐的字符重新presentation 的是ASCII值,然后将其存储为一个字符串。

所以ABC=&GT; {97,98,99} =&GT; 979899。

不过,由于您使用的是ASCII,如果他们是100下可以有100个或更多,您正在使用0填充你的整数值:

 ABC=&GT; {97,98,99} =&GT; {097,098,099} =&GT; 097098099

但你决定这样做只是有时,因为不知何故

 ABA=&GT; 97098097

即,第一个a被变成97,而最后的a被变成097。

我说你应该先解决您的编码方案。

此外,这些希望不是随机整数,因为你试图把他们变成明智的字符串。否则,一个简单的映射,如BASE64很容易映射任何整数转换为字符串,他们只是可能没有多大意义。

在事实上,他们甚至不真正的整数。你存储的连接codeD字符串作为字符串。

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;

解决方案

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.

这篇关于从随机整数到实际字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆