现在的位置: 首页 > 综合 > 正文

C#中一些字符串操作的常用用法,c#编码和解码

2013年09月06日 ⁄ 综合 ⁄ 共 3543字 ⁄ 字号 评论关闭

//获得汉字的区位码
  byte[] array = new byte[2];
  array = System.Text.Encoding.Default.GetBytes("啊");

 

int i1 = (short)(array[0] - ''/0'');
  int i2 = (short)(array[1] - ''/0'');

//unicode解码方式下的汉字码
  array = System.Text.Encoding.Unicode.GetBytes("啊");
  i1 = (short)(array[0] - ''/0'');
  i2 = (short)(array[1] - ''/0'');

//unicode反解码为汉字
  string str = "4a55";
  string s1 = str.Substring(0,2);
  string s2 = str.Substring(2,2);

int t1 = Convert.ToInt32(s1,16);
  int t2 = Convert.ToInt32(s2,16);

array[0] = (byte)t1;
  array[1] = (byte)t2;

string s = System.Text.Encoding.Unicode.GetString(array);

//default方式反解码为汉字
  array[0] = (byte)196;
  array[1] = (byte)207;
  s = System.Text.Encoding.Default.GetString(array);

//取字符串长度
  s = "iam方枪枪";
  int len = s.Length;//will output as 6
  byte[] sarr = System.Text.Encoding.Default.GetBytes(s);
  len = sarr.Length;//will output as 3+3*2=9

//字符串相加
  System.Text.StringBuilder sb = new System.Text.StringBuilder("");
  sb.Append("i ");
  sb.Append("am ");
  sb.Append("方枪枪");

/////////////////////////////////////////////////////////////////////

 

string --> byte array

byte[] data=Syste.Text.Encoding.ASCII.GetBytes(string);

string --> byte

byte data = Convert.ToByte(string);

byte[]-->string

string string = Encoding.ASCII.GetString( bytes, 0, nBytesSize );

 
C# URL编码与解码

--------------------------------------------------------------------------------
 
最近做了一个网站统计分析系统,特别在搜索引擎分析那一部分费了不少时间,从已编码的URL中提取搜索关键词再进行解码。主流的搜索引擎中,URL所使用的字符编码都不同,比如,baidu默认是gb2312,google默认是utf-8。而百度用户可以指定utf-8编码。以这两个搜索引擎分析,URL可能是gb2312,也可能是utf-8。头大了!经过几番琢磨。得出一个暂时可以对付的简单方法。

以下是引用片段:
//oStr是UrlEncode编码字符串
Encoding gb2312 = Encoding.GetEncoding("gb2312"); 
Encoding utf8 = Encoding.UTF8; 
//首先用utf-8进行解码
string key = HttpUtility.UrlDecode(oStr, utf8);
// 将已经解码的字符再次进行编码.
string encode = HttpUtility.UrlEncode(key, utf8).ToLower();
//与原来编码进行对比,如果不一致说明解码未正确,用gb2312进行解码
if (oStr != encode)
    key = HttpUtility.UrlDecode(oStr, gb2312);
 
用这样的方法基本上可以解决了URL的编码解码问题了。

asp.net(C#实现) 编码解码(HtmlEncode与HtmlEncode)时间:2009-10-16 10:37来源:未知 作者:admin 点击:246次我要投稿
Default.aspx:

<%@ Page ValidateRequest="false" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="test_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>asp.net(C#) 编码解码(HtmlEncode与HtmlEncode)</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblShow" runat="server" Text="Label"></asp:Label>
        <asp:TextBox ID="txtInput" runat="server" Height="194px" TextMode="MultiLine" Width="305px"></asp:TextBox>
        <asp:Button ID="btnOk" runat="server" Text="提交" OnClick="btnOk_Click" /></div>
    </form>
</body>
</html>

Default.aspx.cs:
 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/***********************编码研究***********************
 * 1.默认情况是不允许用户在TextBox中输入html标签的,
 *   如果需要输入,设置Page的ValidateRequest="false"
 * 2.可以把输入的html标签,比如<input>直接存放在数据库中,
 *   只是在输出的时候编码,防止原样输出打乱页面布局.或者呈现html元素.
 *****************************************************/
public partial class test_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        lblShow.Text = htmlEncode(txtInput.Text);
    }
    /// <summary>
    /// 对输入的html编码,同时对回车与空格进行转换
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public string htmlEncode(string str)
    {
        return Server.HtmlEncode(str).Replace("/n", "<br/>").Replace(" ", " ");
    }

}

抱歉!评论已关闭.