PHP base64_decode equivalente C #
Domanda
Sto cercando di imitare uno script php che procede come segue:
- sostituisci ogni spazio GET vaiable con un segno + ($ var = preg_replace (" / \ s / ", " + ", $ _ GET ['var']);)
- decodifica in base64: base64_decode ($ var);
1 ° ho aggiunto un metodo per eseguire una decodifica base64:
public string base64Decode(string data)
{
try
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(data);
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
return result;
}
catch (Exception e)
{
throw new Exception("Error in base64Decode" + e.Message);
}
}
ma sembra che UTF-8 non stia facendo il lavoro, quindi ho provato lo stesso metodo ma con un UTF-7
public string base64Decode(string data)
{
try
{
System.Text.UTF7Encoding encoder = new System.Text.UTF7Encoding();
System.Text.Decoder utf7Decode = encoder.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(data);
int charCount = utf7Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf7Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
return result;
}
catch (Exception e)
{
throw new Exception("Error in base64Decode" + e.Message);
}
}
un'ultima cosa da dire, la corretta decodifica php contiene segni speciali, come il segno registrato e il segno del marchio ma la versione C # no!
inoltre, php base64_decode è interessato dalla lingua del server?
Soluzione
È molto improbabile che UTF-7 sia quello che vuoi. Devi davvero sapere cosa sta usando la codifica PHP. potrebbe utilizzare la codifica predefinita per il tuo sistema. Fortunatamente è molto più facile decodificare di quello che stai realizzando:
public static string base64Decode(string data)
{
byte[] binary = Convert.FromBaseString(data);
return Encoding.Default.GetString(binary);
}
Non è necessario scherzare esplicitamente con Encoder
:)
Un'altra possibilità è che PHP stia usando ISO Latin 1, che è la code page 28591:
public static string base64Decode(string data)
{
byte[] binary = Convert.FromBaseString(data);
return Encoding.GetEncoding(28591).GetString(binary);
}
Il manuale di PHP dice inutilmente solo: " Prima di PHP 6, un carattere è uguale a un byte. Cioè, ci sono esattamente 256 caratteri diversi possibili. & Quot; Peccato che non dica cosa significa effettivamente ogni ...