PHP base64_de code C#相当于 [英] PHP base64_decode C# equivalent

查看:186
本文介绍了PHP base64_de code C#相当于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图模仿了一个PHP脚本,做到以下几点:

I'm trying to mimic a php script that do the following :


  1. 替换GET vaiable与一个+号每一个空间($ VAR = preg_replace(/ \\ s /,+,$ _ GET ['变种']);)

  2. 解码为Base64:base64_de code($ VAR);

1日我添加了一个方法执行一个base64解码:

1st i added a method perform a base64 decoding :

        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);
        }
    }

但接缝是UTF-8是不是做的工作,所以我尝试同样的方法,但用UTF-7

but it seams that UTF-8 is not doing the job , so i tried the same method but with a 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);
        }
    }

最后一件事要说,成功的PHP解码包含特殊标志,如注册标志和商标的迹象,但C#版本不!

one last thing to say, the successful php decoding contains special signs , like the registered sign and trademark sign but the C# version doesn't !

此外,没有受服务器语言PHP base64_de code?

also , does php base64_decode affected by the server language ?

推荐答案

UTF-7是非常不可能是你想要的。你真的需要知道什么编码PHP使用。它的可能的是使用默认的编码系统。幸运的是它是一个更容易脱code比你让:

UTF-7 is very unlikely to be what you want. You really need to know what encoding PHP is using. It may be using the default encoding for your system. Fortunately it's a lot easier to decode than you're making it:

public static string base64Decode(string data)
{
    byte[] binary = Convert.FromBaseString(data);
    return Encoding.Default.GetString(binary);
}

有没有必要明确地与周围乱恩codeR :)

There's no need to explicitly mess around with Encoder :)

另一种可能性是,PHP是使用ISO拉丁语1,这是code页28591:

Another possibility is that PHP is using ISO Latin 1, which is code page 28591:

public static string base64Decode(string data)
{
    byte[] binary = Convert.FromBaseString(data);
    return Encoding.GetEncoding(28591).GetString(binary);
}

PHP手册帮倒忙只是说:PHP 6之前,字符是一样的字节也就是说,恰好有256种可能的不同的角色。羞耻不说什么的每个字节实际上办法 ...

这篇关于PHP base64_de code C#相当于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆