Expecco Code Snippets/en: Unterschied zwischen den Versionen

Aus expecco Wiki (Version 2.x)
Zur Navigation springen Zur Suche springen
Zeile 1: Zeile 1:
== Code Snippets for Common Tasks ==
== Code Snippets for Common Tasks ==


This page is for coders of elementary actions which are to execute inside expecco (i.e. Smalltalk and JavaScript elementary actions). For bridged actions, the corresponding programming language/environment determines the syntax and set of possible operations and is outside the control of expecco.
This page provides code snippets for common tasks. You can copy-paste these snippets into the code editor and modify as appropriate. You can also paste them into a [[How_to_Program/en#Executing_Code_Fragments | Workspace window]] and execute them there. Finally, you may also find those snippets via the [[How_to_Program/en#Find_Functions_by_Example | Method Finder]], when entering the desired result value.

You will find code snippets for common tasks here.

You can copy-paste these snippets into the code editor and modify as appropriate. You can also paste them into a [[How_to_Program/en#Executing_Code_Fragments | Workspace window]] and execute them there. Finally, you may also find those snippets via the [[How_to_Program/en#Find_Functions_by_Example | Method Finder]], when entering the desired result value.


=== Bits & Bytes ===
=== Bits & Bytes ===

Version vom 18. September 2020, 14:01 Uhr

Code Snippets for Common Tasks[Bearbeiten]

This page is for coders of elementary actions which are to execute inside expecco (i.e. Smalltalk and JavaScript elementary actions). For bridged actions, the corresponding programming language/environment determines the syntax and set of possible operations and is outside the control of expecco.

You will find code snippets for common tasks here.

You can copy-paste these snippets into the code editor and modify as appropriate. You can also paste them into a Workspace window and execute them there. Finally, you may also find those snippets via the Method Finder, when entering the desired result value.

Bits & Bytes[Bearbeiten]

Set, Clear or Toggle a Bit[Bearbeiten]

Smalltalk:

|numIn result|

numIn := 0b10011001.
"/ using masks
result := numIn bitOr: 0b00110000.    "/ to set the two bits
result := numIn bitAnd: 0b11110000.   "/ to mask bits
result := numIn bitClear: 0b00000011. "/ to clear bits
result := numIn bitXor: 0b00001100.   "/ to toggle bits
result := numIn bitTest: 0b00001100.  "/ to test if masked bits are all set

"/ using bit numbers (index starts with 1 for the least significant bit)
result := numIn bitAt: 1.             "/ to extract the first bit 
result := numIn setBit: 2.            "/ set the second bit
result := numIn clearBit: 2.          "/ clear the second bit
result := numIn invertBit: 2.         "/ toggle the second bit
 

JavaScript:

var numIn, result;

numIn = 0b10011001;
// using masks
result = numIn | 0b00110000;         // to set the two bits
result = numIn & 0b11110000;         // to mask bits
result = numIn & ˜0b00000011;        // to clear bits
result = numIn ^ 0b00001100;         // to toggle bits
result = (numIn & 0b00001100) != 0;  // to test if masked bits are all set

// using bit numbers (index starts with 1 for the least significant bit)
result = numIn.bitAt(1);             // to extract the first bit 
result = numIn.setBit(2);            // set the second bit
result = numIn.clearBit(2);          // clear the second bit
result = numIn.invertBit(2);         // toggle the second bit

Counting, Highest and Lowest Bit[Bearbeiten]

Smalltalk:

|numIn result|

numIn := 0b10011001.
result := numIn highBit.              "/ index of highest bit
result := numIn lowBit.               "/ index of lowest bit
result := numIn bitCount.             "/ count ones

Shifting and Byte Access[Bearbeiten]

Smalltalk:

|numIn result|

numIn := 0xABCD.
result := numIn bitShift: 8.       "/ left shift by 8
result := numIn bitShift: -8.      "/ right shift by 8
result := numIn << 8.              "/ alternative left shift
result := numIn >> 8.              "/ alternative right shift

result := numIn digitByteAt:1.     "/ the lowest 8 bits
result := numIn digitByteAt:2.     "/ the next 8 bits
result := numIn digitBytes.        "/ bytes as ByteArray

JavaScript:

var numIn, result;

numIn = 0xABCD;
result = numIn << 8;               // left shift by 8
result = numIn >> 8;               // right shift by 8
result = numIn.digitByteAt(1);     // as above

Accessing Bytes/Ints/Floats in a ByteArray[Bearbeiten]

Typical tasks when receiving binary encoded communication protocol messages are to extract bytes, ints, floats and strings.

Smalltalk:

|bytes result|

"/ constant byte array here; 
"/ could also come from a file, stream, message or document (see below)
bytes := #[ 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 ].

result := bytes at:1.    "/ 1 based index here
result := bytes[1].      "/ alternative syntax; also 1 based
 
result := bytes signedInt16At:2.             "/ signed int16 in machine's native byte order
result := bytes signedInt16At:3 MSB:false.   "/ in explicit lsb-order
result := bytes signedInt16At:3 MSB:true.    "/ in explicit msb-order
result := bytes signedInt32At:2.             "/ same options as above
result := bytes signedInt64At:2.             "/ same options as above
result := bytes signedInt128At:1.            "/ same options as above

result := bytes unsignedInt16At:2.           "/ same for unsigned ints
result := bytes unsignedInt16At:3 MSB:false.
result := bytes unsignedInt16At:3 MSB:true.
result := bytes unsignedInt32At:2.
result := bytes unsignedInt64At:2.

"/ writing into a byte array
bytes int16At:4 put:0x1234.                 "/ again in native byte order
bytes int16At:4 put:0x1234 MSB:true.        "/ or explicit order

"/ floats and doubles
result := bytes floatAt:3.                  "/ extract a 32bit float
result := bytes floatAt:3 MSB:true          "/ in given byte order
result := bytes doubleAt:3.                 "/ extract a 64bit float
result := bytes doubleAt:3 MSB:true         "/ in given byte order

"/ strings
result := bytes stringAt:2 size:4.               "/ extract bytes as a string
result := bytes zeroByteStringAt:3 maximumSize:3 "/ extract from a 0-terminated C-string

Byte Order and Sign Extension[Bearbeiten]

rslt := numberIn byteSwapped.     "/ swap pairwise
rslt := numberIn byteSwapped16.   "/ only the lowest 2 bytes
rslt := numberIn byteSwapped32.   "/ only the lowest 4 bytes
rslt := numberIn byteSwapped64.   "/ only the lowest 8 bytes

rslt := numberIn signExtendedByteValue.   "/ sign extent from bit 8
rslt := numberIn signExtendedShortValue.  "/ sign extent from bit 16
rslt := numberIn signExtended24BitValue.  "/ sign extent from bit 24
rslt := numberIn signExtendedLongValue.   "/ sign extent from bit 32

Numeric[Bearbeiten]

Smalltalk:

numberIn sin.                    "/ trigonometric I radians;
                                 "/ also cos, tan, sin, cosh, arcSin, arcCos etc.
numberIn degreesToRadians sin    "/ converting degrees first

JavaScript:

numberIn.sin();                  // similar
Math.sin(numberIn);              // alternative
numberIn.degreesToRadians().sin();

Converting Numbers to Strings and Vice Versa[Bearbeiten]

String/Text Processing[Bearbeiten]

Searching/Extracting Parts from a Document[Bearbeiten]

File I/O[Bearbeiten]

Sockets[Bearbeiten]

HTML[Bearbeiten]

XML[Bearbeiten]

JSON[Bearbeiten]



Copyright © 2014-2024 eXept Software AG