Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remove padding

 
Jorma Ikonen
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Do you have any good and effective way to remove padding at the end of byte-array, or string? Use of "PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()))" on decryption produces fixed length of byte arrays (8 bytes) and if the message is not equal to single block size, or multiplied block size, the end of byte array will be padded by byte '0'.
An example of BC's decryption generates first a String from byte array and uses a trim()-function on it to get rid of padding. It's quite obvious that this type of removing cannot be used in any other place as in example, because it also reduces orginal data, if it starts/ends by spaces.
I'm going to use traditional for-loop to get rid of byte '0', but is there any other and faster way to do it?
Aimed solution:
byte[] b = {32,65,66,67,32,0,0,0}; //for debugging
String rs = null;
for (int i = b.length-1; i>-1; i--){
if(b[i] != 0){
rs = new String(b, 0, i+1);
break;
}
}
if (rs == null){rs = new String();}
return rs:
Any idea of better/faster one? - something else?
br,
Jorma
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt there's anything faster than your for loop. Of course there could be a class that does the same with less lines of code, though. I would just go with the for loop.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic