Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

October 29, 2007

Byte Array Declaration with Initializers in Python, Perl, Ruby, Java, C#, VB.NET, etc.

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)


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.