C#如何解码和编码? [英] C# How to decode and encode?

查看:87
本文介绍了C#如何解码和编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决!!!!伙计们发生了一些奇怪的事情,它开始起作用我不知道为什么但我很高兴它终于有效了。谢谢你的帮助!



大家好。需要你的帮助。如何使用变音符号和空格编码和解码字符串?我正在寻找解决方案的时间,我现在所拥有的只是:

编辑:好的,所以感谢解决方案我得到了这个代码:



SOLVED!!!! Guys something strange happened and it started to work I don't know why but I'am glad it finally works. Thanks for helping me!

Hi guys. Need your help. How can I encode and decode string with diacritic and spaces? I was looking for hours for solution and all I have now is:
Ok, so thanks to solutions I've get this code:

public static string encode(string text)
{
    byte[] mybyte = System.Text.Encoding.UTF8.GetBytes(text);
    string returntext = System.Convert.ToBase64String(mybyte);
    return returntext;
}

public static string decode(string text)
{
    byte[] mybyte = System.Convert.FromBase64String(text);
    string returntext = System.Text.Encoding.UTF8.GetString(mybyte);
    return returntext;
}





但是我在System.Convert中收到错误Base-64 char数组的长度无效。 FromBase64String(text);

而且我也不确定这是否会占用字符串空格。



怎么办?感谢您的每一个回复。



But I'am getting error "Invalid length for a Base-64 char array" in System.Convert.FromBase64String(text);
And I'am also not sure if this will take spaces in strings.

What to do? Thanks for every reply.

推荐答案

您的错误是使用ASCII。使用任何Unicode UTF,最好是UTF-8;它会起作用。当然,如果您将文本保存在纯文本文件中,请在匹配编码中进行。所有现代编辑都可以做到这一点,包括Visual Studio。



一个常见的错误观念是基于拉丁语的语言不使用Unicode。这不是真的。即使是纯美式英语也使用ASCII子集之外的Unicode代码点,印刷字符正确,引号等等。



请参阅: - - ...'和©还有更多。



你真的需要对Unicode有所了解。

请参阅:

http://en.wikipedia.org/wiki/Unicode [ ^ ],

http:// unicode.org/ [ ^ ],

http://unicode.org/faq/utf_bom.html [ ^ ]。



-SA
Your mistake is using ASCII. Use any of Unicode UTFs, preferably UTF-8; and it will work. Of course, if you save text in plain text file, do it in matching encoding. All modern editor can do it, including Visual Studio.

One pretty usual misconception is that Latin-based languages don't use Unicode. This is not true. Even pure American English uses Unicode code point outside the ASCII subset, in typographically correct dash characters, quotation marks, etc.

See: — – … " " ' © and a lot more.

You really need some understanding of Unicode.
Please see:
http://en.wikipedia.org/wiki/Unicode[^],
http://unicode.org/[^],
http://unicode.org/faq/utf_bom.html[^].

—SA


这是因为您使用的ASCII编码非常有限,并且不支持变音字符。尝试其他编码,如UTF-8。 ( http://gnidesign.blogspot.com/2011 /04/c-how-to-encode-or-decode-string-to.html [ ^ ])



祝你好运!
It is because you use ASCII Encoding which is very limited and doesn't support diacritic characters. Try another encoding, like UTF-8. (http://gnidesign.blogspot.com/2011/04/c-how-to-encode-or-decode-string-to.html[^])

Good luck!


我发现这个有用的文章:



HTML DECODE ENCODE [ ^ ]
i found this useful article on this topic:

HTML DECODE ENCODE[^]


这篇关于C#如何解码和编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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