Example #1
0
 static char[] _Base64_Array(ReadOnlySpan <char> encoded)
 {
     char[] a = AMemoryArray.Char_(encoded.Length);
     for (int i = 0; i < encoded.Length; i++)
     {
         char c = encoded[i];
         a[i] = c switch { '_' => '/', '-' => '+', _ => c, };
     }
     return(a);
 }
Example #2
0
        /// <summary>
        /// Converts hex-encoded string to binary data as byte[].
        /// </summary>
        /// <param name="encoded">String or char[] or span of string/array/memory containing Hex-encoded data.</param>
        /// <remarks>
        /// Skips spaces and other non-hex-digit characters. Example: "01 23 46 67" is the same as "01234667".
        /// The number of hex-digit characters should be divisible by 2, else the last character is ignored.
        /// </remarks>
        public static byte[] HexDecode(ReadOnlySpan <char> encoded)
        {
            int n = encoded.Length / 2;

            fixed(byte *p = AMemoryArray.Byte_(n))
            {
                n = HexDecode(encoded, p, n);
                var r = new byte[n];

                Marshal.Copy((IntPtr)p, r, 0, n);
                return(r);
            }
        }