我正在学习理解String的位/字节概念。我试图改变给定字符串的byte []数组但是我很惊讶地发现,即使我改变了byte [],字符串表示也是一样的。以下是我的代码示例。有人可以帮助理解为什么,如果可能的话,请分享有关字节和Sting关系的更多见解的教程。
public class HelloWorld{
//Add padding to input value to
public static byte[] addPadding(byte[] inData, int offset, int len) {
byte[] bp = null;
int padChars = 8; // start with max padding value
int partial = (len + 1) % padChars; // calculate how many extra bytes exist
if (partial == 0) padChars = 1; // if none, set to only pad with length byte
else padChars = padChars - partial + 1; // calculate padding size to include length
bp = new byte[len + padChars];
bp[0] = Byte.parseByte(Integer.toString(padChars));
System.arraycopy(inData, offset, bp, 1, len);
return bp;
}
//remove padding added while decryption
public static byte[] removePadding(byte[] inData) {
byte[] bp = null;
int dataLength = 0;
int padLength = 0;
padLength = inData[0];
dataLength = inData.length - padLength;
bp = new byte[dataLength];
System.arraycopy(inData, 1, bp, 0, dataLength);
return bp;
}
public static void main(String []args){
String inputString = "I like coding :-)";
byte[] byteArrayOfString = inputString.getBytes();
System.out.println("Original String: " + new String (byteArrayOfString));
byteArrayOfString = addPadding(byteArrayOfString, 0, byteArrayOfString.length);
//Add padding
System.out.println("String after adding pad: " + new String (byteArrayOfString));
//remove padding
byteArrayOfString = removePadding(byteArrayOfString);
System.out.println("String after removing pad: " + new String (byteArrayOfString));
}
}
运行此程序时输出以下内容:
Original String: I like coding :-)
String after adding pad: I like coding :-)
String after removing pad: I like coding :-)
令人惊讶的是所有输出都相同。好奇为什么?
答案 0 :(得分:3)
它确实打印了额外的字符;你只是没有看到他们。我在每个字符串打印之前和之后添加了引号字符,这里的输出在IntelliJ中显示。
Original String: "I like coding :-)"
String after adding pad: " I like coding :-) "
String after removing pad: "I like coding :-)"
然而,当我在终端窗口中运行此代码时,我得到了这个,就像你做的那样:
Original String: "I like coding :-)"
String after adding pad: "I like coding :-)"
String after removing pad: "I like coding :-)"
另外,重要的是,我听到一声哔哔声。
填充是使用字符编号7,a.k.a。\u0007
完成的,这是铃声字符。 IntelliJ显然只是打印一个空间,但是我的PuTTY终端正确地发出了哔哔声。尾随空格似乎也消失了。
在返回之前打印addPadding
中的字节数组内容会显示字节数组内容。
System.out.println(Arrays.toString(bp));
这是输出:
[7, 73, 32, 108, 105, 107, 101, 32, 99, 111, 100, 105, 110, 103, 32, 58, 45, 41, 0, 0, 0, 0, 0, 0]
首先是贝尔字符7
,后跟字符串的字符,后跟空字符0
(\u0000
)。 IntelliJ也必须将它们打印为空格; PuTTY终端不打印它们。
总之,填充物在那里;你只是没有看到正在打印的字符。根据您的输出方法,您可能会听到其中一个。
答案 1 :(得分:1)
他们不一样。填充字符串只包含非打印字符。
String inputString = "I like coding :-)";
byte[] byteArrayOfString = inputString.getBytes();
String original = new String (byteArrayOfString);
System.out.println("Original String: " + original);
byteArrayOfString = addPadding(byteArrayOfString, 0, byteArrayOfString.length);
//Add padding
String addPadding = new String (byteArrayOfString);
System.out.println("String after adding pad: " + addPadding);
//remove padding
byteArrayOfString = removePadding(byteArrayOfString);
String removePadding = new String (byteArrayOfString);
System.out.println("String after removing pad: " + removePadding);
System.out.println(Arrays.toString(original.toCharArray()));
System.out.println(Arrays.toString(addPadding.toCharArray()));
System.out.println(Arrays.toString(removePadding.toCharArray()));
打印
Original String: I like coding :-)
String after adding pad: I like coding :-)
String after removing pad: I like coding :-)
[I, , l, i, k, e, , c, o, d, i, n, g, , :, -, )]
[, I, , l, i, k, e, , c, o, d, i, n, g, , :, -, ), , , , , , ]
[I, , l, i, k, e, , c, o, d, i, n, g, , :, -, )]