BitXorString (String function)

From m204wiki
Revision as of 20:46, 3 September 2015 by ELowell (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Do bitwise exclusive Or of strings (String class)

[Introduced in Sirius Mods 7.9]

BitXorString does a logical XOR (exclusive OR) of the bits in two strings.

Syntax

%result = string:BitXorString( xorBits, [Pad= c])

Syntax terms

%resultA string to receive the result of XORing together the bits in string and xorBits.
string One of the strings to be XORed.
xorBits The other string to be XORed.
Pad A single character that indicates the character with which the shorter of the two input strings is to be extended. As many copies of c are used as is required to make the shorter string as long as the longer. If both strings are of equal length, no padding is required. The default value of c is '00':x.

Usage notes

  • Using a null string or xorBits is an easy way of XORing each byte in a string with a specific byte value (the Pad value).
  • BitXorString is most useful for working with binary data, for example binary data being sent or received with Janus Sockets.

Examples

The following example compares the result of BitXorString (logical exclusive OR) versus BitOrString (logical inclusive OR):

b printText {~='Ale':stringToHex} printText {~='Lager':stringToHex} print '======================' printText {~='Ale':bitOrString('Lager', pad='40':x)} printText {~='Ale':bitOrString('Lager', pad='40':x):stringToHex} print '======================' printText {~='Ale':bitXorString('Lager', pad='40':x)} printText {~='Ale':bitXorString('Lager', pad='40':x):stringToHex} end

The result is:

'Ale':stringToHex=C19385 'Lager':stringToHex=D381878599 ====================== 'Ale':bitOrString('Lager', pad='40':x)=LlgER 'Ale':bitOrString('Lager', pad='40':x):stringToHex=D39387C5D9 ====================== 'Ale':bitXorString('Lager', pad='40':x)=???ER 'Ale':bitXorString('Lager', pad='40':x):stringToHex=121202C5D9

See also