I created a program that takes user input and converts it into a char array. It then turns each char in the array to it's corresponding byte value, and prints all of them in a single line.
我创建了一个程序,它接受用户输入并将其转换为char数组。然后它将数组中的每个char转换为相应的字节值,并将它们全部打印在一行中。
I am now creating a second program that I want to be able to paste that line of bytes into it as user input, and then have it display the original message in ASCII (English)
我现在正在创建第二个程序,我希望能够将该行字节作为用户输入粘贴到其中,然后让它以ASCII格式显示原始消息(英语)
Here is my code so far:
这是我到目前为止的代码:
import java.util.Scanner;
public class MessageDecoder
{
public static void main(String[] args)
{
System.out.println("Please enter your encrypted message and press Enter: ");
Scanner reader = new Scanner(System.in);
byte b = reader.nextByte();
}
}
I have tried concatenating the line of bytes into a String, like this
我尝试将字节行连接成一个字符串,就像这样
System.out.println(b + "");
But that (probably obviously) didn't work.
但那(可能显然)没有用。
What do I need to do?
我需要做什么?
3 个解决方案
#1
1
just try
System.out.println((char)b);
However from you comments as far as I can understand you have "encrypted your char" in numbers, hence an A has become 65 (2 bytes 6 and 5). The problem of "decrypting" this is that you have lost information es. AA = 6565 (and this could also be char 6 char 5 ecc). So what you need is a system to understand how it was grouped.
但是根据你的评论,据我所知,你已经用数字“加密了你的字符”,因此A变为65(2字节6和5)。 “解密”这个问题就是你丢失了信息。 AA = 6565(这也可以是char 6 char 5 ecc)。所以你需要的是一个系统来理解它是如何分组的。
Solution A is to set fixed with (at least 3 chars since a byte max is 256) this means that the A becomes 065
解决方案A是固定的(至少3个字符,因为字节最大值为256),这意味着A变为065
Solution B use a seperatore (that is not present in file es. EOT (char)5.
解决方案B使用seperatore(文件es.EOT(char)5中不存在)。
If solution A to decrpyt you read the input (also as string) then you loop 3 chars by 3 char, cast to Integer --> cast to char and you got your original char.
如果解决方案A解读你输入(也作为字符串),那么你将3个字符循环3个字符,转换为整数 - >强制转换为字符,然后你得到了你的原始字符。
Solution B, maybe simpler you can use the split function on the input string
解决方案B,可能更简单,您可以在输入字符串上使用split函数
String[] stringArray = input.split(char)5);
Then loop this and on every string
然后循环这个和每个字符串
int result = Integer.parseInt(stringArray[i]).
char c = (char)result;
I did not give you all the code, but I hope it helps
我没有给你所有的代码,但我希望它有所帮助
#2
0
maybe you need this:
也许你需要这个:
System.out.println("Please enter your encrypted message and press Enter: ");
Scanner reader = new Scanner(System.in);
String val = reader.nextLine();
char[] buffer = val.toCharArray();
byte[] b = new byte[buffer.length];
for (int i = 0; i < b.length; i++) {
System.out.println(Integer.toBinaryString((byte) buffer[i] & 255 | 256).substring(1));
}
#3
0
Here is how I would go about it
这是我如何去做
- Read the bytes (keep them 2 digits, HEX to keep this simple, in other words, 9 is entered as 09) as a String
- Start with an empty String(Buffer) as your decrypted String.
- In a while loop, simply substring 2 characters at a time and use Byte.valueOf with radix 16 as the convertor to byte and append that as char to your decrypted String.
读取字节(保持2位数,HEX保持这个简单,换句话说,9输入为09)作为字符串
以空字符串(Buffer)作为解密的String开始。
在while循环中,简单地一次子串2个字符,并使用Byte.valueOf和radix 16作为转换器到byte并将其作为char附加到解密的String。
Hope this helps!
希望这可以帮助!
#1
1
just try
System.out.println((char)b);
However from you comments as far as I can understand you have "encrypted your char" in numbers, hence an A has become 65 (2 bytes 6 and 5). The problem of "decrypting" this is that you have lost information es. AA = 6565 (and this could also be char 6 char 5 ecc). So what you need is a system to understand how it was grouped.
但是根据你的评论,据我所知,你已经用数字“加密了你的字符”,因此A变为65(2字节6和5)。 “解密”这个问题就是你丢失了信息。 AA = 6565(这也可以是char 6 char 5 ecc)。所以你需要的是一个系统来理解它是如何分组的。
Solution A is to set fixed with (at least 3 chars since a byte max is 256) this means that the A becomes 065
解决方案A是固定的(至少3个字符,因为字节最大值为256),这意味着A变为065
Solution B use a seperatore (that is not present in file es. EOT (char)5.
解决方案B使用seperatore(文件es.EOT(char)5中不存在)。
If solution A to decrpyt you read the input (also as string) then you loop 3 chars by 3 char, cast to Integer --> cast to char and you got your original char.
如果解决方案A解读你输入(也作为字符串),那么你将3个字符循环3个字符,转换为整数 - >强制转换为字符,然后你得到了你的原始字符。
Solution B, maybe simpler you can use the split function on the input string
解决方案B,可能更简单,您可以在输入字符串上使用split函数
String[] stringArray = input.split(char)5);
Then loop this and on every string
然后循环这个和每个字符串
int result = Integer.parseInt(stringArray[i]).
char c = (char)result;
I did not give you all the code, but I hope it helps
我没有给你所有的代码,但我希望它有所帮助
#2
0
maybe you need this:
也许你需要这个:
System.out.println("Please enter your encrypted message and press Enter: ");
Scanner reader = new Scanner(System.in);
String val = reader.nextLine();
char[] buffer = val.toCharArray();
byte[] b = new byte[buffer.length];
for (int i = 0; i < b.length; i++) {
System.out.println(Integer.toBinaryString((byte) buffer[i] & 255 | 256).substring(1));
}
#3
0
Here is how I would go about it
这是我如何去做
- Read the bytes (keep them 2 digits, HEX to keep this simple, in other words, 9 is entered as 09) as a String
- Start with an empty String(Buffer) as your decrypted String.
- In a while loop, simply substring 2 characters at a time and use Byte.valueOf with radix 16 as the convertor to byte and append that as char to your decrypted String.
读取字节(保持2位数,HEX保持这个简单,换句话说,9输入为09)作为字符串
以空字符串(Buffer)作为解密的String开始。
在while循环中,简单地一次子串2个字符,并使用Byte.valueOf和radix 16作为转换器到byte并将其作为char附加到解密的String。
Hope this helps!
希望这可以帮助!