Base64 Encoding in UnrealScript
Our example demonstrates base64 encoding and decoding in UnrealScript. UnrealScript doesn’t have built-in support for base64, so we’ll implement basic encoding and decoding functions.
class Base64Example extends Object;
var string Base64Chars;
function Init()
{
Base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
}
function string EncodeBase64(string Data)
{
local int i, j, Remainder;
local string Result;
local array<int> Bytes;
for (i = 0; i < Len(Data); i++)
{
Bytes[i] = Asc(Mid(Data, i, 1));
}
for (i = 0; i < Bytes.Length; i += 3)
{
Result $= Mid(Base64Chars, (Bytes[i] >> 2) & 0x3F, 1);
if (i + 1 < Bytes.Length)
{
Result $= Mid(Base64Chars, ((Bytes[i] & 0x3) << 4) | ((Bytes[i+1] >> 4) & 0xF), 1);
if (i + 2 < Bytes.Length)
{
Result $= Mid(Base64Chars, ((Bytes[i+1] & 0xF) << 2) | ((Bytes[i+2] >> 6) & 0x3), 1);
Result $= Mid(Base64Chars, Bytes[i+2] & 0x3F, 1);
}
else
{
Result $= Mid(Base64Chars, (Bytes[i+1] & 0xF) << 2, 1);
Result $= "=";
}
}
else
{
Result $= Mid(Base64Chars, (Bytes[i] & 0x3) << 4, 1);
Result $= "==";
}
}
return Result;
}
function string DecodeBase64(string EncodedData)
{
local int i, j, Value;
local string Result;
local array<int> Bytes;
for (i = 0; i < Len(EncodedData); i++)
{
if (Mid(EncodedData, i, 1) == "=")
break;
j = InStr(Base64Chars, Mid(EncodedData, i, 1));
if (j != -1)
Bytes[Bytes.Length] = j;
}
for (i = 0; i < Bytes.Length; i += 4)
{
Value = (Bytes[i] << 18) | (Bytes[i+1] << 12) | (Bytes[i+2] << 6) | Bytes[i+3];
Result $= Chr((Value >> 16) & 0xFF);
if (i + 2 < Bytes.Length)
Result $= Chr((Value >> 8) & 0xFF);
if (i + 3 < Bytes.Length)
Result $= Chr(Value & 0xFF);
}
return Result;
}
function Example()
{
local string Data, EncodedData, DecodedData;
Init();
// Here's the string we'll encode/decode
Data = "abc123!?$*&()'-=@~";
// Encode the string
EncodedData = EncodeBase64(Data);
`log("Encoded: " $ EncodedData);
// Decode the string
DecodedData = DecodeBase64(EncodedData);
`log("Decoded: " $ DecodedData);
}
In this example, we’ve implemented basic Base64 encoding and decoding functions. UnrealScript doesn’t have built-in Base64 support, so we’ve created our own implementation.
The EncodeBase64
function takes a string and returns its Base64 encoded version. The DecodeBase64
function does the reverse, taking a Base64 encoded string and returning the original data.
To use these functions:
- First, call the
Init
function to set up the Base64 character set. - Then you can use
EncodeBase64
to encode a string andDecodeBase64
to decode it.
The Example
function demonstrates how to use these functions. It encodes a sample string, prints the encoded version, then decodes it back and prints the result.
Note that this is a basic implementation and may not handle all edge cases or be optimized for performance. In a real game scenario, you might want to use a more robust, tested library if available, or further optimize this code for your specific needs.
To run this code, you would typically include it in an UnrealScript class in your Unreal Engine project and call the Example
function from appropriate game logic.