📄 form1.cs
字号:
using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
namespace Base64example
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(192, 144);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(536, 349);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//将gb2312转成base64
private String tobase64(String source)
{
byte [] buf = Encoding.GetEncoding(936).GetBytes(source);
char [] cBase = {'A', 'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z','0','1',
'2','3','4','5','6','7','8','9','+','/'};
String szStr = Convert.ToBase64String( buf);
String Result="";
int ntmp;
for ( int n = 0; n < buf.Length;)
{
#region 填充代码
if ( buf.Length - n == 2)
{
ntmp = (0xfc & buf[n]) >> 2;
Result = String.Format("{0}{1}", Result, cBase[ntmp]);
ntmp = ((0x03 & buf[n]) << 4) | (buf[n+1] >> 4) ;
Result = String.Format("{0}{1}", Result, cBase[ntmp]);
ntmp = (0x0f & buf[n+1]) << 2;
Result = String.Format("{0}{1}=", Result, cBase[ntmp]);
break;
}
if ( buf.Length - n == 1)
{
ntmp = (0xfc & buf[n]) >> 2;
Result = String.Format("{0}{1}", Result, cBase[ntmp]);
ntmp = ((0x03 & buf[n]) << 4) ;
Result = String.Format("{0}{1}==", Result, cBase[ntmp]);
break;
}
#endregion
ntmp = (0xfc & buf[n]) >> 2;
Result = String.Format("{0}{1}", Result, cBase[ntmp]);
ntmp = ((0x03 & buf[n]) << 4) | (buf[n+1] >> 4) ;
Result = String.Format("{0}{1}", Result, cBase[ntmp]);
ntmp = ((0x0f & buf[n+1]) << 2) | ((0xc0 & buf[n+2]) >> 6);
Result = String.Format("{0}{1}", Result, cBase[ntmp]);
ntmp = (0x3f & buf[n+2]);
Result = String.Format("{0}{1}", Result, cBase[ntmp]);
n += 3;
}
return Result;
}
//将base64转成gb2312
private String frombase64(String Source)
{
char [] buf = Source.ToCharArray();
String szBase = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
MemoryStream sb=new MemoryStream();
byte btmp;
for (int n = 0; n < buf.Length; )
{
#region 填充代码
if (buf.Length - n == 4)
{
if ('=' == buf[n+2])
{
btmp = (byte)(((szBase.IndexOf(buf[n]) << 2) | ((szBase.IndexOf(buf[n+1]) & 0x30) >> 4)));
sb.WriteByte(btmp);
break;
}
if ('=' == buf[n+3])
{
btmp = (byte)(((szBase.IndexOf(buf[n]) << 2) | ((szBase.IndexOf(buf[n+1]) & 0x30) >> 4)));
sb.WriteByte(btmp);
btmp = (byte)((szBase.IndexOf(buf[n+1]) & 0x0f) << 4 | ((szBase.IndexOf(buf[n+2]) & 0x3c) >> 2));
sb.WriteByte(btmp);
break;
}
}
#endregion
btmp = (byte)(((szBase.IndexOf(buf[n]) << 2) | ((szBase.IndexOf(buf[n+1]) & 0x30) >> 4)));
sb.WriteByte(btmp);
btmp = (byte)((szBase.IndexOf(buf[n+1]) & 0x0f) << 4 | ((szBase.IndexOf(buf[n+2]) & 0x3c) >> 2));
sb.WriteByte(btmp);
btmp = (byte)(((szBase.IndexOf(buf[n+2]) & 0x03) << 6) | szBase.IndexOf(buf[n+3]));
sb.WriteByte(btmp);
n += 4;
}
return Encoding.GetEncoding(936).GetString(sb.ToArray());
}
//转成utf8
private byte[] toutf8(String source)
{
StringBuilder buf = new StringBuilder(source);
byte [] dest = Encoding.UTF8.GetBytes(source);
MemoryStream sb=new MemoryStream();
for ( int n = 0; n < buf.Length; n++)
{
if (0x7F >= buf[n])
sb.WriteByte((byte)buf[n]);
else if (buf[n] <= 0x07ff)
{
sb.WriteByte((byte)(((buf[n] & 0x7c0) >> 6) | 0xc0));
sb.WriteByte((byte)((buf[n] & 0x3f) | 0x80));
}
else if (buf[n] <= 0xffff)
{
sb.WriteByte((byte)(((buf[n] & 0xf000) >> 12) | 0xe0));
sb.WriteByte((byte)(((buf[n] & 0xfc0) >> 6) | 0x80));
sb.WriteByte((byte)((buf[n] & 0x3f) | 0x80));
}
}
return sb.ToArray();
}
private void button1_Click(object sender, System.EventArgs e)
{
//String base64 = tobase64("测试工!");
//String gb2312 = frombase64(base64);
byte [] utf8 = toutf8("as测sa试!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -