I frequently encounter scenarios where I need to decode Base64-encoded strings while developing with UdonSharp.
Currently, Convert.FromBase64String is available, but since UdonSharp does not support try-catch blocks, handling exceptions when decoding fails is not feasible. This limitation makes error handling cumbersome and complicates the use of exception-throwing methods. Therefore, having access to the Convert.TryFromBase64String method, which allows safe decoding without throwing exceptions, would be greatly beneficial.
This method is particularly useful as it can work with byte[] instead of requiring Span<byte>. Even without access to System.Span, developers can fully utilize its functionality. However, enabling System.Span would further enhance efficiency in memory operations and provide additional benefits to the developer community.
Below is an example test code utilizing Convert.TryFromBase64String:
// Base64 encoded string
string base64String = "SGVsbG8gV29ybGQh"; // Base64 encoding of "Hello World!"
// Allocate a buffer to store the decoded result
int requiredBufferSize = base64String.Length * 3 / 4;
byte[] buffer = new byte[requiredBufferSize];
// Variable to hold the number of bytes actually written
if (Convert.TryFromBase64String(base64String, buffer, out int bytesWritten))
{
Debug.Log($"Decoding succeeded: {bytesWritten} bytes");
Debug.Log($"Result: {System.Text.Encoding.UTF8.GetString(buffer, 0, bytesWritten)}");
}
else
{
Debug.Log("Decoding failed: Invalid Base64 string");
}
Thank you for your consideration.
Please feel free to contact me if additional information or details are required.