将文件中的十六进制值转换为行终止符 [英] Converting hex value in a file to a line terminator

查看:137
本文介绍了将文件中的十六进制值转换为行终止符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从客户导入文件.当我在Ultra Edit中打开它时,会看到一个十六进制85,它对于行终止符显示为….我正在尝试将其转换为十六进制0A. Windows理解的任何内容都可以,但是0A在我们的许多其他通信中很常见.我在StreamReader上使用.Replace替换值,但是输出不是我期望的.

将"\ u0085"替换为"\ u000a"会导致…被�替换,该字符在十六进制中表示为EF BF BD.

用"\ x0a"替换"\ x85"具有相同的结果.

对我在做什么错有任何想法吗?非常感谢您的帮助!

//File =原始文件的路径
字符串文件=文件;

//存档=放置新文件的路径
字符串存档=存档

FileInfo fi =新的FileInfo(file);
字符串名称= fi.Name;

StreamReader sr =新的StreamReader(file);
StreamWriter sw =新的StreamWriter(存档+名称);

//下面两行都将产生一个十六进制的EF BF BD,该文本在文本中显示为…
//字符串输入= sr.ReadToEnd().Replace("\ x85","\ x0a");
字符串输入= sr.ReadToEnd().Replace("\ u0085","\ u000a");

sw.Write(input);
sr.Close();
sw.Close();

I''m importing a file from a customer. When I open it up in Ultra Edit is see a hex 85 which shows up as … for the line terminator. I''m trying to convert it to a hex 0A. Anything that windows understands would be fine but 0A is common in many of our other communications. I''m using a .Replace on a StreamReader to replace the value but the output is not what I expect.

Replacing "\u0085" with "\u000a" results in … being replace by � which is represented in the hex as EF BF BD.

Replacing "\x85" with "\x0a" has the same results.

Any thoughts about what I''m doing wrong? Help is greatly appreciated!

// File = Path of original file
String file = File;

// Archive = Path where new file is to be placed
String archive = Archive

FileInfo fi = new FileInfo(file);
String name = fi.Name;

StreamReader sr = new StreamReader(file);
StreamWriter sw = new StreamWriter(archive + name);

// Both lines below result in a hex EF BF BD which shows up as … in the text
// String input = sr.ReadToEnd().Replace("\x85", "\x0a");
String input = sr.ReadToEnd().Replace("\u0085", "\u000a");

sw.Write(input);
sr.Close();
sw.Close();

推荐答案

成员3551437写道:
Member 3551437 wrote:

//两者下面的行导致一个十六进制的EF BF BD,在文本中显示为…

// Both lines below result in a hex EF BF BD which shows up as … in the text



EF BF BD是UTF-8(MBCS)字节流的签名.您可能会发现更容易将文件读入内存,然后通过手动搜索Unicode''\ u0085''字符进行替换.



EF BF BD is the signature of a UTF-8 (MBCS) byte stream. You may find it easier to read the file into memory and then do the replacements by searching for the Unicode ''\u0085'' characters manually.


应为:
字符串输入= sr.ReadToEnd().Replace("\ uFFFD","\ x0A");
It should be:
String input = sr.ReadToEnd().Replace("\uFFFD", "\x0A");


这篇关于将文件中的十六进制值转换为行终止符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆