用 Java 对 Char 进行编码

发布于 2025-01-02 12:33:25 字数 863 浏览 0 评论 0原文

所以我试图简单地将一个字符编码到一个文件中,但由于某种原因它不起作用。 这里找到了示例代码,这是我的代码:

public class derp : MonoBehaviour{
    public Char[] chars = new Char[] {'a', 'b', 'c', 'r', 't', 'h'};

    void Start(){
        UTF8Encoding utf8 = new UTF8Encoding();
        Byte[] encodedBytes = utf8.GetBytes(chars);

        FileStream stream = new FileStream(Application.dataPath+"/testing.wld", FileMode.Create);
        for(int i = 0; i < chars.Length; i++){
            stream.WriteByte(encodedBytes[i]);
        }
        stream.Close();
    }
}

我运行了脚本,打开了它,然后 this是什么文件看起来像:

我不希望任何老人能够打开文件并编辑其中的数据。然而图片清楚地表明这是可能的。我的代码与示例中的代码没有任何不同,但由于某种原因该文件没有编码数据。那么为什么这不起作用呢?为什么数据没有被编码?

So I'm trying to simply encode a character into a file but for some reason it's not working. Here where I've found the example code and here is my code:

public class derp : MonoBehaviour{
    public Char[] chars = new Char[] {'a', 'b', 'c', 'r', 't', 'h'};

    void Start(){
        UTF8Encoding utf8 = new UTF8Encoding();
        Byte[] encodedBytes = utf8.GetBytes(chars);

        FileStream stream = new FileStream(Application.dataPath+"/testing.wld", FileMode.Create);
        for(int i = 0; i < chars.Length; i++){
            stream.WriteByte(encodedBytes[i]);
        }
        stream.Close();
    }
}

I ran the script, opened it and this is what the file looks like:

I don't want any old person to be able to open the file and edit the data that's in it. However the picture clearly shows it's possible. My code isn't any different than the code in the example but for some reason the file doesn't have encoded data. So why isn't this working? Why isn't the data encoded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴情换悲伤 2025-01-09 12:33:25

好吧,数据已被编码,但听起来您希望它被加密。 UTF-8 不适用于密码学;这只是一个字符集。 UTF-8 编码看起来与 ASCII 完全相同,这就是记事本可以读取该文件的原因。

如果要加密文件,一种选择是 RSA 加密。查看 RSACryptoServiceProvider 类:

http://msdn.microsoft .com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx

Well, the data is encoded, but it sounds like you wanted it to be encrypted. UTF-8 isn't for cryptography; it's just a character set. UTF-8 encoding looks exactly like ASCII, which is why Notepad can read the file.

If you want to encrypt the file, one option is RSA encryption. Check out the RSACryptoServiceProvider class:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx

梦里寻她 2025-01-09 12:33:25

它将被编码为 UTF8...

It will be encoded - in UTF8...

伴随着你 2025-01-09 12:33:25

数据正在被编码,一切都按照文档记录工作,并且正如每个了解 UTF8 的人所期望的那样。也许你自己的期望与现实不符?您认为“UTF8”是什么以及“编码”是什么意思?

The data is being encoded, and everything is working as documented, and as expected by everyone who knows UTF8. Perhaps your own expectations don't match reality? What did you think "UTF8" was and what "encoding" meant?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文