I am learning to understand the bits/bytes concept for String. I tried to change the byte[] array for given string however I am surprised to see that even when I changed byte [] the string representation is same. Following is my code sample. Can someone help to understand why and also if possible please share tutorial having more insight on byte and Sting relation.
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));
}
}
Following is output when I ran this program:
Original String: I like coding :-)
String after adding pad: I like coding :-)
String after removing pad: I like coding :-)
Surprisingly all output are same. Curious why?