加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net – Enocde / Decode字符串

发布时间:2020-12-17 07:15:03 所属栏目:百科 来源:网络整理
导读:(使用vb.net) 嗨, 我有一个ini文件,我需要在ini文件中将RTF文件作为单行发布 – 比如… [my section]rtf_file_1=bla bla bla (the content of the rtf file) 为了避免RTF文件中的换行符,特殊代码等包装在ini文件中,如何将其编码(并解码)为单个字符串? 如果
(使用vb.net)

嗨,

我有一个ini文件,我需要在ini文件中将RTF文件作为单行发布 – 比如…

[my section]
rtf_file_1=bla bla bla (the content of the rtf file)

为了避免RTF文件中的换行符,特殊代码等包装在ini文件中,如何将其编码(并解码)为单个字符串?

如果有一个函数将字符串(在我的情况下是RTF文件的内容)转换为一行数字然后解码回来,我就是这个东西?

你会怎么做?

谢谢!

解决方法

您可以使用base64编码对它们进行编码.就像那样,内容被处理为二进制 – >它可以是任何类型的文件.
但是当然在配置文件中你将无法读取文件的内容.

这里是一个Snodpet到Base64的编码/解码

//Encode
string filePath = "";
string base64encoded = null;
using (StreamReader r = new StreamReader(File.OpenRead(filePath)))
{
    byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(r.ReadToEnd());
    base64encoded = System.Convert.ToBase64String(data);
}

//decode --> write back
using(StreamWriter w = new StreamWriter(File.Create(filePath)))
{
    byte[] data = System.Convert.FromBase64String(base64encoded);

    w.Write(System.Text.ASCIIEncoding.ASCII.GetString(data));
}

在VB.NET中:

Dim filePath As String = ""
    Dim base64encoded As String = vbNull

    'Encode()
    Using r As StreamReader = New StreamReader(File.OpenRead(filePath))
        Dim data As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(r.ReadToEnd())
        base64encoded = System.Convert.ToBase64String(data)
    End Using

    'decode --> write back
    Using w As StreamWriter = New StreamWriter(File.Create(filePath))
        Dim data As Byte() = System.Convert.FromBase64String(base64encoded)
        w.Write(System.Text.ASCIIEncoding.ASCII.GetString(data))
    End Using

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读