Chilkat provides example code in many different programming languages, and if you haven’t noticed, there are a lack of examples involving byte arrays. That’s going to change soon. We’re sharing our handy cheat-sheet of how to declare a byte array with initialization in many different programming languages.
The Chilkat libs use the CkByteArray object as an efficient container for byte data in C++, Ruby, Python, Perl, and Java. This post also demonstrates how to load the contents of CkByteData with the native byte array.
Ruby:
a2_bytes = [ 0x01, 0xAA, 3, 4 ]
a2 = Chilkat::CkByteData.new()
a2.append(a2_bytes.pack("C4"), 4)
Perl:
$a2_bytes = pack "C4", 0x01, 0xAA, 3, 4;
$a2 = new chilkat::CkByteData();
$a2->append($a2_bytes,4);
Python:
# In Python, the bytes are simply embedded in a string:
a2 = chilkat.CkByteData()
a2.append('\x01\xAA\x03\x04',4)
Java:
// The Java byte data type is signed, so any value greater than or equal to 0x80 must
// be cast to (byte).
byte[] a2_bytes = { (byte) 0x01, (byte) 0xAA, 3, 4};
CkByteData a2 = new CkByteData();
a2.appendByteArray(a2_bytes);
C#:
// Chilkat does not use CkByteArray in C#.
// Instead, byte arrays are passed directly to Chilkat methods/properties.
byte[] a2 = { 0x01, 0xAA, 3, 4};
VB.NET and Visual Basic 6.0:
' Chilkat does not use CkByteArray in VB.NET.
' Instead, byte arrays are passed directly to Chilkat methods/properties.
Dim a2() As Byte = {&H01, &HAA, 3, 4}
C++:
unsigned char a2_bytes[] = { 0x01, 0xAA, 3, 4};
CkByteData a2;
a2.append(a2_bytes, 4);
ASP and VBScript:
' Chilkat does not use CkByteArray in ASP or VBScript.
' Instead, byte arrays are passed directly to Chilkat methods/properties.
Dim a2
a2 = a2 & ChrB(&H01)
a2 = a2 & ChrB(&HAA)
a2 = a2 & ChrB(3)
a2 = a2 & ChrB(4)