Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
pad and unpad byte arrayDemonstrates using the pad and unpad methods of CkByteArray. import com.chilkatsoft.*; public class ChilkatExample { static { try { System.loadLibrary("chilkat"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load.\n" + e); System.exit(1); } } public static void main(String argv[]) { byte[] data_bytes = { (byte)0, (byte)1, (byte)2, (byte)3, (byte)4, (byte)5, (byte)6, (byte)7, (byte)8, (byte)9}; CkByteData data = new CkByteData(); data.appendByteArray(data_bytes); // These are the padding options for the "pad" method: // 0 = Each padding byte is the pad count (16 extra added if size is already a multiple of 16) // 1 = Random bytes except the last is the pad count (16 extra added if size is already multiple of 16) // 2 = Pad with random data. (If already a multiple of 16, no padding is added). // 3 = Pad with NULLs. (If already a multiple of 16, no padding is added). // 4 = Pad with SPACE chars(0x20). (If already a multiple of 16, no padding is added). // We'll pad to a block size of 16 bytes. int blockSize; blockSize = (int) 16; int paddingScheme; paddingScheme = (int) 0; // Pad, display the padded byte data, unpad, then re-display data.pad(blockSize,paddingScheme); System.out.println(data.getEncoded("hex")); data.unpad(16,paddingScheme); System.out.println(data.getEncoded("hex")); System.out.println("----"); paddingScheme = (int) 1; // Pad, display the padded byte data, unpad, then re-display data.pad(blockSize,paddingScheme); System.out.println(data.getEncoded("hex")); data.unpad(16,paddingScheme); System.out.println(data.getEncoded("hex")); System.out.println("----"); // There is no unpadding with schemes 2,3, and 4 byte[] data2_bytes = { (byte)0, (byte)1, (byte)2, (byte)3, (byte)4, (byte)5, (byte)6, (byte)7, (byte)8, (byte)9}; CkByteData data2 = new CkByteData(); data2.appendByteArray(data2_bytes); paddingScheme = (int) 2; // Pad and display the padded byte data data2.pad(blockSize,paddingScheme); System.out.println(data2.getEncoded("hex")); System.out.println("----"); // There is no unpadding with schemes 2,3, and 4 byte[] data3_bytes = { (byte)0, (byte)1, (byte)2, (byte)3, (byte)4, (byte)5, (byte)6, (byte)7, (byte)8, (byte)9}; CkByteData data3 = new CkByteData(); data3.appendByteArray(data3_bytes); paddingScheme = (int) 3; // Pad and display the padded byte data data3.pad(blockSize,paddingScheme); System.out.println(data3.getEncoded("hex")); System.out.println("----"); // There is no unpadding with schemes 2,3, and 4 byte[] data4_bytes = { (byte)0, (byte)1, (byte)2, (byte)3, (byte)4, (byte)5, (byte)6, (byte)7, (byte)8, (byte)9}; CkByteData data4 = new CkByteData(); data4.appendByteArray(data4_bytes); paddingScheme = (int) 4; // Pad and display the padded byte data data4.pad(blockSize,paddingScheme); System.out.println(data4.getEncoded("hex")); System.out.println("----"); } } |
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.