Tag: utf 8

从UTF-8字符串中删除控制字符

我发现了这个问题,但它也删除了所有有效的utf-8字符(返回一个空字符串,而有效的utf-8字符加上控制字符)。 当我读到utf-8 , control characters没有特定的范围,每个字符集都有自己的control characters 。 如何修改上述解决方案只删除control characters ?

在C#字符串/字符编码中GetBytes(),GetString()和Convert()之间的区别是什么?

我们无法将Unicode字符串转换为UTF-8字符串以通过网络发送: // Start with our unicode string. string unicode = “Convert: \u10A0”; // Get an array of bytes representing the unicode string, two for each character. byte[] source = Encoding.Unicode.GetBytes(unicode); // Convert the Unicode bytes to UTF-8 representation. byte[] converted = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, source); // Now that we have converted the bytes, save them to […]

Process.StartInfo.Arguments是否支持UTF-8字符串?

你可以使用UTF-8字符串作为StartInfo的参数吗? 我试图将UTF-8(在本例中为日语字符串)作为控制台参数传递给应用程序。 像这样的东西(这只是一个例子!(cmd.exe将是一个自定义应用程序)) var process = new System.Diagnostics.Process(); process.StartInfo.Arguments = “/K \”echo これはテストです\””; process.StartInfo.FileName = “cmd.exe”; process.StartInfo.UseShellExecute = true; process.Start(); process.WaitForExit(); 执行此操作似乎松开了UTF-8字符串,所有目标应用程序看到的都是“echo ?????????” 直接在命令行上执行此命令(通过粘贴参数),目标应用程序正确接收字符串,即使命令行本身似乎没有正确显示它。 我是否需要做一些特殊的事情才能在参数中启用UTF-8支持,或者这是不支持的?

.net中的转换:本机Utf-8 托管字符串

我创建了这两个方法来将Native utf-8字符串(char *)转换为托管字符串,反之亦然。 以下代码完成了这项工作: public IntPtr NativeUtf8FromString(string managedString) { byte[] buffer = Encoding.UTF8.GetBytes(managedString); // not null terminated Array.Resize(ref buffer, buffer.Length + 1); buffer[buffer.Length – 1] = 0; // terminating 0 IntPtr nativeUtf8 = Marshal.AllocHGlobal(buffer.Length); Marshal.Copy(buffer, 0, nativeUtf8, buffer.Length); return nativeUtf8; } string StringFromNativeUtf8(IntPtr nativeUtf8) { int size = 0; byte[] buffer = {}; do { […]

如何将xml作为UTF-8而不是UTF-16返回

我正在使用序列化的例程。 它有效,但下载到浏览器后,我看到一个空白页面。 我可以在文本编辑器中查看页面源或打开下载,我看到xml,但它是UTF-16,我认为这是为什么浏览器页面显示空白? 如何修改序列化程序例程以返回UTF-8而不是UTF-16? XML源返回: January February March April May June July August September October November December 调用序列化程序的示例: DateTimeFormatInfo dateTimeFormatInfo = new DateTimeFormatInfo(); var months = dateTimeFormatInfo.MonthNames.ToList(); string SelectionId = “1234567890”; return new XmlResult<List>(SelectionId) { Data = months }; 串行器: public class XmlResult : ActionResult { private string filename = DateTime.Now.ToString(“ddmmyyyyhhss”); public T Data { […]

从UTF8转换为ASCII

我从以UTF8编码存储的XML文件中读取文本。 C#读取它完美,我用调试器检查,但是当我尝试将其转换为ASCII以将其保存在另一个文件中时,我得到了一个? char在角色冲突的地方。 例如,本文: string s = “La introducción masiva de las nuevas tecnologías de la información”; 将保存为 “La introducci?n masiva de las nuevas tecnolog?as de la informaci?n” 我不能只为他们的拉丁语(a,e,i,o,u)元音取代它们,因为西class牙语中的某些单词会错过这种感觉。 我已经尝试了这个问题并没有成功。 所以我希望有人可以帮助我。 第二个选择的答案甚至没有编译……! 如果有人想看看,我的代码是这样的: private void WriteInput( string input ) { byte[] byteArray = Encoding.UTF8.GetBytes(input); byte[] asciiArray = Encoding.Convert(Encoding.UTF8, Encoding.ASCII, byteArray); string finalString = Encoding.ASCII.GetString(asciiArray); string […]

在SQL Server 2008中的NVarChar中存储UTF-8时遇到问题

我正在从网站上使用System.Net.WebClient提取数据,当数据返回时,除了带重音的字母外,所有内容都会解析并且看起来很好。 例如,当它返回é ,SQL Server 2008将其保存为é 。 只需要弄清楚如何将这些UTF-8字符转换为SQL Server可以读取的内容。 我将它存储在NVARCHAR(MAX)数据类型中。 如果你好奇,我正在使用Linq-to-SQL插入数据库。 有什么想法,我可以做到将其转换为正确的格式?

如何强制XDocument在声明行中输出“UTF-8”?

以下代码生成此输出: Jim Smith 如何让它生成encoding=”utf-8″而不是encoding=”utf-16″ ? using System; using System.Collections.Generic; using System.IO; using System.Xml.Linq; namespace test_xml2 { class Program { static void Main(string[] args) { List customers = new List { new Customer {FirstName=”Jim”, LastName=”Smith”, Age=27}, new Customer {FirstName=”Hank”, LastName=”Moore”, Age=28}, new Customer {FirstName=”Jay”, LastName=”Smythe”, Age=44}, new Customer {FirstName=”Angie”, LastName=”Thompson”, Age=25}, new Customer {FirstName=”Sarah”, LastName=”Conners”, Age=66} […]

在Windowsapp store应用中使用httpclient获取UTF-8响应

我正在构建一个Windowsapp store应用,但我坚持从API获取UTF-8响应。 这是代码: using (HttpClient client = new HttpClient()) { Uri url = new Uri(BaseUrl + “/me/lists”); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url); request.Headers.Add(“Accept”, “application/json”); HttpResponseMessage response = await client.SendRequestAsync(request); response.EnsureSuccessStatusCode(); string responseString = await response.Content.ReadAsStringAsync(); response.Dispose(); } reponseString总是包含奇怪的字符,这些字符应该是é等重音符号,我尝试使用流,但是我在Windows RT中找不到某些示例中的API。 编辑:改进代码,仍然是同样的问题

如何修复空格的UTF编码?

在我的C#代码中,我从PDF文档中提取文本。 当我这样做时,我得到一个UTF-8或Unicode编码的字符串(我不确定哪个)。 当我使用Encoding.UTF8.GetBytes(src); 要将它转换为字节数组,我注意到空格实际上是两个字符值为194和160的字符。 例如,字符串“CLE action”看起来像 [67, 76, 69, 194 ,160, 65 ,99, 116, 105, 111, 110] 在一个字节数组中,空格是194和160 ……并且因为这个src.IndexOf(“CLE action”); 当我需要它返回1时返回-1。 如何修复字符串的编码?