public static unsafe byte[] Decode( char[] chars, bool allowOddChars ) {
if ( chars == null ) {
throw new ArgumentException( "chars" );
}
int len = chars.Length;
if ( len == 0 ) {
return new byte[0];
}
byte[] bytes = new byte[ ( len + 1 ) / 2 ];
int bytesDecoded, charsDecoded;
bool hasHalfByteCached = false;
byte cachedHalfByte = 0;
fixed ( char* pChars = &chars[0] ) {
fixed ( byte* pBytes = &bytes[0] ) {
Decode( pChars, pChars + len, pBytes, pBytes + bytes.Length, ref hasHalfByteCached, ref cachedHalfByte, out charsDecoded, out bytesDecoded );
}
}
if ( hasHalfByteCached && !allowOddChars ) {
throw new XmlException( Res.Xml_InvalidBinHexValueOddCount, new string( chars ) );
}
if ( bytesDecoded < bytes.Length ) {
byte[] tmp = new byte[ bytesDecoded ];
Buffer.BlockCopy( bytes, 0, tmp, 0, bytesDecoded );
bytes = tmp;
}
return bytes;
}