- ////////////////////////////////////////////////////////////////////
- //////////////Compilation environment:Win8+VS2012+.Net2.0///////////
- //////////////////////////////////////////////////// ///// ///////////
- //////////////Nane:ChangeCode///////////////////// ///// ////////////
- /////////////////////////////////////////////////// //// /////////////
- //////////////Code by:Ntsafe-hkk////////////////// /// //////////////
- /////////////////////////////////////////////////// /// //////////////
- ///////////////Time:2013/04/10/////////////////// ////// ////////////
- ////////////////////////////////////////////////// //////// ////////
- namespace 编码转换大全
- {
- public partial class frmMain : Form
- {
- public frmMain()
- {
- InitializeComponent();
- }
- private void frmMain_Load(object sender, EventArgs e)
- {
- }
- /// <字符转16进制>
- /// Text to Hex
- /// </summary>
- /// <param name="s"></param>
- /// <param name="encode"></param>
- /// <returns></returns>
- private string StringToHexString(string s, Encoding encode)
- {
- byte[] b = encode.GetBytes(s);//按照指定编码将string编程字节数组
- string result = string.Empty;
- for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符
- {
- result += Convert.ToString(b[i], 16);
- }
- return "0x" + result;
- }
- /// <16进制转字符>
- /// Hex to Text
- /// </summary>
- /// <param name="hs"></param>
- /// <param name="encode"></param>
- /// <returns></returns>
- private string HexStringToString(string hs, Encoding encode)
- {
- string strTemp = "";
- byte[] b = new byte[hs.Length / 2];
- for (int i = 0; i < hs.Length / 2; i++)
- {
- strTemp = hs.Substring(i * 2, 2);
- b[i] = Convert.ToByte(strTemp, 16);
- }
- //按照指定编码将字节数组变为字符串
- return encode.GetString(b);
- }
- /// <字符转ASC>
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- private string TextToAsc(string str)
- {
- string newtext = "";
- foreach (char c in str)
- {
- newtext += Convert.ToString((int)c) + " ";
- }
- newtext = newtext.Substring(0, newtext.Length-1);
- return newtext;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1409201513686.html
来源: http://www.codesnippet.cn/detail/1409201513686.html