我必须开发一个程序。这是编码系统。
我有这样的日文字符:
つれづれなるまゝに,日暮らし,砚にむかひて,心にうつりゆくよしなし事を,そこはかとなく书きつくれば,あやしうこそものぐるほしけれ
我想将此字符串转换为如下编码:
%26%2312388%3B%26%2312428%3B%26%2312389%3B%26%2312428%3B%26%2312394%3B%26%2312427%3B%26%2312414%3B%26%2312445%图3B%26%2312395%3B%26%2312289%3B%26%2326085%3B%26%2326286%3B%26%2312425%3B%26%2312375%3B%26%2312289%3B%26%2330831%3B% 26%2312395%3B%26%2312416%3B%26%2312363%3B%26%2312402%3B%26%2312390%3B%26%2312289%3B%26%2324515%3B%26%2312395%3B%26% 2312358%3B%26%2312388%3B%26%2312426%3B%26%2312422%3B%26%2312367%3B%26%2312424%3B%26%2312375%3B%26%2312394%3B%26%2312375%图3B%26%2320107%3B%26%2312434%3B%26%2312289%3B%26%2312381%3B%26%2312371%3B%26%2312399%3B%26%2312363%3B%26%2312392%3B% 26%2312394%3B%26%2312367%3B%26%2326360%3B%26%2312365%3B%26%2312388%3B%26%2312367%3B%26%2312428%3B%26%2312400%3B%26% 2312289%3B%26%2312354%3B%26%2312420%3B%26%2312375%3B%26%2312358%3B%26%2312371%3B%26%2312381%3B%26%2312418%3B%26%2312398%图3B%26%2312368%3B%26%2312427%3B%26%2312411%3B%26%2312375%3B%26%2312369%3B%26%2312428%3B%26%2312290%3B。
我该怎么做?
答案 0 :(得分:8)
我相信您正在寻找HttpUtility.UrlEncode
,无法找出编码以获得与您显示的完全相同的输出。
var testString = "つれづれなるまゝに、日暮らし、硯にむかひて、心にうつりゆくよしなし事を、そこはかとなく書きつくれば、あやしうこそものぐるほしけれ。";
var encodedUrl = HttpUtility.UrlEncode(testString, Encoding.UTF8);
您可能想要更改您的问题,因为您实际上不需要将Unicode转换为ASCII,这是不可能的。您需要Persent编码或URL编码Percent-encoding。
<强> [编辑] 强>
我明白了:
var testString = "つれづれなるまゝに、日暮らし、硯にむかひて、心にうつりゆくよしなし事を、そこはかとなく書きつくれば、あやしうこそものぐるほしけれ。";
var htmlEncoded = string.Concat(testString.Select(arg => string.Format("&#{0};", (int)arg)));
var result = HttpUtility.UrlEncode(htmlEncoded);
结果将与您提供的编码完全匹配。 一步一步:
var inputChar = 'つ';
var charValue = (int)inputChar; // 12388
var htmlEncoded = "&#" + charValue + ";"; // つ
var ulrEncoded = HttpUtility.UrlEncode(htmlEncoded); // %26%2312388%3b
答案 1 :(得分:3)
这是不可能的。 Unicode比ASCII大得多,您无法在ASCII中查找Unicode中的每个字符。虽然ASCII只有256个字符(带控制字符),但Unicode数以万计(我猜)。
答案 2 :(得分:1)
这是一个似乎有效的功能:
public static string UrlDoubleEncode(string text)
{
if (text == null)
return null;
StringBuilder sb = new StringBuilder();
foreach (int i in text)
{
sb.Append('&');
sb.Append('#');
sb.Append(i);
sb.Append(';');
}
return HttpUtility.UrlEncode(sb.ToString());
}