英文:
Encrypt two given strings
问题
以下是翻译好的代码部分:
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main
{
    public static String StringToBinary(String str) 
    {
        StringBuilder result = new StringBuilder();
        char[] ch = str.toCharArray();
        for (char character : ch) 
        {
            result.append(String.format("%8s", Integer.toBinaryString(character)).replaceAll(" ", "0"));
        }
        return result.toString();
    }
    
    public static String XOR(String message, String password)
    {
        String tmp = "";
        for (int i = 0; i < message.length(); i++) 
        { 
            if (message.charAt(i) == password.charAt(i)) 
                tmp += "0"; 
            else
                tmp += "1"; 
        } 
        return tmp;
    }
    
    public static String convertBinaryToHexadecimal(String binaryStr) 
    {
        return new BigInteger(binaryStr, 2).toString(16);
    }
    
    public static void main(String[] args) 
    {
        int idx = 0;
        String msg = "poiuytrewq123";
        String pwd = "asdfghjkl";
        for (char m : msg.toCharArray())
        {
            idx = (idx < pwd.length()) ? idx : 0;
            char p = pwd.charAt(idx);
            idx++;
            String stringOfMessageCharArray = String.valueOf(m);
            String stringOfPasswordCharArray = String.valueOf(p);
            String messageBinaryString = StringToBinary(stringOfMessageCharArray);
            String passwordBinaryString = StringToBinary(stringOfPasswordCharArray);
            String XorString = XOR(messageBinaryString, passwordBinaryString);
            String cipherText = convertBinaryToHexadecimal(XorString);
            System.out.print(cipherText);
        }
    }
}
英文:
I'm trying to encrypt a message by the following code above.
- Compare 2 string length and refill the short one with cycle back.
 - Convert that 2 string into a binary string.
 - XOR that 2 binary string to get another binary string.
 - convert the last binary string to hexadecimal.
 
Issue is on the last step. The output should be 111c0d131e1c180e1b10425655 instead of 111cd131e1c18e1b10425655.
Why my output missing two letters 0?
Can anyone help me to fix, please?
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main
{
public static String StringToBinary(String str) 
{
StringBuilder result = new StringBuilder();
char[] ch = str.toCharArray();
for (char character : ch) 
{
result.append(String.format("%8s", Integer.toBinaryString(character)).replaceAll(" ", "0"));
}
return result.toString();
}
public static String XOR(String message, String password)
{
String tmp="";
for (int i = 0; i < message.length(); i++) 
{ 
if (message.charAt(i) == password.charAt(i)) 
tmp += "0"; 
else
tmp += "1"; 
} 
return tmp;
}
public static String convertBinaryToHexadecimal(String binaryStr) 
{
return new BigInteger(binaryStr, 2).toString(16);
}
public static void main(String[] args) 
{
int idx=0;
String msg = "poiuytrewq123";
String pwd = "asdfghjkl";
for(char m:msg.toCharArray())
{
idx = (idx < pwd.length()) ? idx : 0;
char p = pwd.charAt(idx);
idx++;
String stringOfMessageCharArray = String.valueOf(m);
String stringOfPasswordCharArray = String.valueOf(p);
String messageBinaryString = StringToBinary(stringOfMessageCharArray);
String passwordBinaryString = StringToBinary(stringOfPasswordCharArray);
String XorString = XOR(messageBinaryString,passwordBinaryString);
String cipherText = convertBinaryToHexadecimal(XorString);
System.out.print(cipherText);
}
}
}
答案1
得分: 0
问题在于你尝试将二进制转换为十六进制的数字太小,只需要2个字符,因此它只输出一个字符。
public static String convertBinaryToHexadecimal(String binaryStr)
{
    String hex = new BigInteger(binaryStr, 2).toString(16);
    return hex.length() < 2 ? "0" + hex : hex;
}
如果你将函数更改为这样,如果长度小于2,它将在十六进制前面添加一个0,并返回正确的十六进制值。
英文:
The problem is that the number you are trying to convert from binary to hex is too small to require 2 characters, so it outputs just one.
public static String convertBinaryToHexadecimal(String binaryStr)
{
    String hex = new BigInteger(binaryStr, 2).toString(16);
    return hex.length() < 2 ? "0" + hex : hex;
}
If you change the function to this, it will put a 0 at the front of the hex if the length is less than 2, and return the right hex.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论