gpt4 book ai didi

java - 尝试查看转化时出现垃圾邮件

转载 作者:行者123 更新时间:2023-12-02 05:56:47 25 4
gpt4 key购买 nike

我对这个简单的类有疑问。当我尝试通过凯撒密码转换输入字符串时,我得到的字符串比想象的要多。怎么了?我是否仅对字符串内容正确实现序列化和反序列化?为什么输出是:[C@58a1a199 而不是 DEFGcaf ..?运行测试时出现错误,如下所示:FAILED: ARRAYS FIRST DIFFERED AT ELEMENT[0];预期为 <68>,但实际为 <14>。为什么我们变换正确却不能通过这个测试?

这是我的类(class):

public class TajnyDokument implements Serializable {

public String content;

public transient int howMuchToMove = 3;
private transient String sign;

public transient char[]cypher;



public TajnyDokument(String zawartosc, String podpis) throws IOException {
this.content = zawartosc;
this.sign = podpis;
}

private void writeObject(ObjectOutputStream os) throws IOException
{
szyfruj(content);

os.writeChars(this.content);
//os.writeChars(this.sign);
// os.writeUTF(content);
os.defaultWriteObject();


}
private void readObject ( ObjectInputStream is ) throws IOException , ClassNotFoundException {

content = String.valueOf(is.readChar());
sign = String.valueOf(is.readChar());

is . defaultReadObject ( ) ;
}

public void szyfruj(String dana) throws IOException
{
System.out.println(content);
cypher = dana.toCharArray();
char tmp[] = new char[cypher.length];
char c;

for(int i = 0; i < cypher.length; i++)
{
c = cypher[i];

if((c >'Z' || c < 'A') && (c < 'a' || c > 'z'))
{
throw new IOException();
}
else
{

if(c == 'X')
{
int ilezostalo = (int)'Z' - (int)'X';

tmp[i] = (char)((int)'A' + (howMuchToMove - ilezostalo-1));
System.out.println(tmp[i]);
}
else if(c == 'Y')
{
int ilezostalo = (int)'Z' - (int)'Y';

tmp[i] = (char)((int)'A' + (howMuchToMove - ilezostalo-1));
System.out.println(tmp[i]);
}
else if(c == 'Z')
{
tmp[i] = (char)((int)'A' + (howMuchToMove-1));
System.out.println(tmp[i]);
}
else if(c == 'x')
{
int ilezostalo = (int)'z' - (int)'x';

tmp[i] = (char)((int)'a' + (howMuchToMove - ilezostalo-1));
System.out.println(tmp[i]);
}
else if(c == 'y')
{
int ilezostalo = (int)'z' - (int)'y';

tmp[i] = (char)((int)'a' + (howMuchToMove - ilezostalo-1));
System.out.println(tmp[i]);
}
else if(c == 'z')
{
tmp[i] = (char)((int)'a' + (howMuchToMove-1));
System.out.println(tmp[i]);
}

else
{
tmp[i] = (char)((int)c + howMuchToMove);
System.out.println(tmp[i]);
}
}
}
content = tmp.toString();
if(tmp.toString().equals("DEFGcaf"))
{
this.content = "DEFGcaf";
}
// super.write(tmp)
System.out.println(content);
System.out.println(content);
System.out.println(content);
}

public String getPodpis() {
return sign;
}
public String getZawartosc() {
return content;
}

public static void main(String[] arg) throws IOException
{
TajnyDokument tajny = new TajnyDokument("ABCDzxc", "Piotr Kaczyński");
tajny.szyfruj(tajny.content);

String wynik = tajny.content;
System.out.println(wynik);
}


}

我的代码的测试类:

public class Punkt2Test {

private ByteArrayOutputStream buffer;

private ObjectOutputStream testOutputStream;

private TajnyDokument testObject;

public Punkt2Test() {
}

@Before
public void setUp() throws IOException {
buffer = new ByteArrayOutputStream();
testOutputStream = new ObjectOutputStream(buffer);
testObject = new TajnyDokument("ABCDzxc", "Piotr Kaczyński");
}

@Test
public void zapisPoprawny() throws IOException, ClassNotFoundException {
testOutputStream.writeObject(testObject);
testOutputStream.flush();

ByteArrayInputStream is = new ByteArrayInputStream(buffer.toByteArray());
byte[] expectedResult = new byte[]{'D', 'E', 'F', 'G', 'c', 'a', 'f'};
byte[] result = new byte[7];
is.skip(101);
is.read(result, 0, 7);
for(int i=0; i<result.length; i++) {
System.out.println(i + " " + (char)result[i] + " " + result[i]);

// System.out.println(result[i]);
}
assertArrayEquals(expectedResult, result);
}

}

最佳答案

您正在以错误的方式将字符数组转换为字符串。尝试:

content = new String(tmp);

您的原始代码是在数组上调用.toString(),它只是调用Object.toString() 实现。因此你得到类似 [C@609a5d54:

  • [表示数组类型
  • C表示char类型
  • @ 将类型与哈希码分开
  • 609a5d54 是哈希码(每次代码运行时都不同)

参见Object.toString()Class.getName()了解更多详细信息。

关于java - 尝试查看转化时出现垃圾邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23008962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com