`

ruby textfile vs binaryfile

阅读更多

The Difference Between Binary and ASCII Files; Converting them

At heart all files are binary files -- that is, a collection of 1s and 0s. But there's a subset of binary files we call ASCII, or plain text files. ASCII is short for American Standard Code for Information Interchange, which allocates a number to each letter, digit and symbol. A plain text file contains no formatting codes whatsoever, no fonts, bold, italics or underlines, headers, footers or graphics. The only 'formatting' that can be applied is to use spaces to pad lines out so that they are centered or right justified, or to add extra blank lines.

Let's look at some example - different file types containing the word 'hello' followed by a new line. To see the differences, we will use a hex display, to show us exactly what is in each file, and highlight the word hello in the output so we can see where it appears. A hex display shows us the offset, ASCII code and the actual characters contained in a file (where those characters are printable - it shows a period where they are not). These hex displays were generated by TextPipe Pro (Filters Menu\Convert\Hex dump).

Plain Text File - hello.txt (7 bytes long)

This is the simplest file - the ASCII codes for the letters 'hello' followed by the ASCII codes for a carriage return and line feed.

00000000 68 65 6C 6C 6F 0D 0A                            hello..

Rich Text Format (RTF) File - hello.rtf (168 bytes long)

You can see that an RTF file includes lots of extra guff. Generally, all the letters of the word will be together. However, if you have two or more words, other codes can appear between the words, making them difficult to locate.

00000000 7B 5C 72 74 66 31 5C 61 6E 73 69 5C 61 6E 73 69 {\rtf1\ansi\ansi 
00000010 63 70 67 31 32 35 32 5C 64 65 66 66 30 5C 64 65 cpg1252\deff0\de 
00000020 66 6C 61 6E 67 33 30 38 31 7B 5C 66 6F 6E 74 74 flang3081{\fontt 
00000030 62 6C 7B 5C 66 30 5C 66 73 77 69 73 73 5C 66 63 bl{\f0\fswiss\fc 
00000040 68 61 72 73 65 74 30 20 41 72 69 61 6C 3B 7D 7D harset0 Arial;}} 
00000050 0D 0A 7B 5C 2A 5C 67 65 6E 65 72 61 74 6F 72 20 ..{\*\generator 
00000060 4D 73 66 74 65 64 69 74 20 35 2E 34 31 2E 31 35 Msftedit 5.41.15 
00000070 2E 31 35 30 33 3B 7D 5C 76 69 65 77 6B 69 6E 64 .1503;}\viewkind 
00000080 34 5C 75 63 31 5C 70 61 72 64 5C 66 30 5C 66 73 4\uc1\pard\f0\fs 
00000090 32 30 20 68 65 6C 6C 6F 5C 70 61 72 0D 0A 5C 70 20 hello\par..\p 
000000A0 61 72 0D 0A 7D 0D 0A 00                         ar..}... 

Microsoft Word Document - hello.doc (19,968 bytes long)

The file below, even without any formatting, is huge, so we've removed large sections of it for clarity. A major point we have to make here is that Word relies on the exact position of various aspects of the file being fixed, such as font tables, symbol tables and other internal references. If these positions are changed (e.g. by searching for 'hello' and replacing it with a shorter string such as 'bye' or a longer string such as 'hello there') then the document will be corrupted and MS Word will not be able to load the document again. Recovery may not be possible. This is why you CANNOT use a text editor or text tool on Word documents. You must use a specific tool that knows how to maintain the correct offsets, such as WordPipe for MS Word, ExcelPipe for MS Excel or PowerPointPipe for MS PowerPoint.

An additional point to note is that the word 'Symbol' is stored in the Word document in Unicode format (see below), so a text editor or text tool will not find it. Since this file contains mixed sections of ASCII and Unicode, it is crucial that the file positions are left unchanged.

00000000 D0 CF 11 E0 A1 B1 1A E1 00 00 00 00 00 00 00 00 ÐÏ.ࡱ.á........ 
00000010 00 00 00 00 00 00 00 00 3E 00 03 00 FE FF 09 00 ........>...þÿ.. 
00000020 06 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 ................ 
...
000009F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 
00000A00 68 65 6C 6C 6F 0D 0D 00 00 00 00 00 00 00 00 00 hello........... 
00000A10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 
...
00001A40 00 53 00 79 00 6D 00 62 00 6F 00 6C 00 00 00 33 .S.y.m.b.o.l...3 
...
00004DF0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 

Unicode Plain Text File - hello.txt (16 bytes long)

ASCII is being replaced in many applications by Unicode, which uses 16 bits (2 bytes) per character to represent non-Roman alphabets like Japanese, Chinese, and Cyrillic. A text editor or text tool won't find 'hello' in this file. TextPipe Pro provides Unicode search and replace facilities, in addition to ASCII search and replace, so it can find both forms of 'hello'.

00000000 FF FE 68 00 65 00 6C 00 6C 00 6F 00 0D 00 0A 00 ÿþh.e.l.l.o..... 

Convert binary files to text files

Now, to convert a binary file to a useful text form, you need to strip out all the binary characters - the formatting, control and other gobbledygook stuff.  TextPipe Pro provides a simple filter for this under Filters\Remove\Binary characters.

Converting a binary file to a text file

You can also generate your own custom filter that only removes the binary characters you specify by using Filters\Maps\New map.

 

You may freely link to this page, but you may not copy its content.

 

 

-------------------------------------------------------------------------------------------------------------------------------

 

from http://book.77169.org/ask2/ask112678.htm

从文件编码的方式来看,文件可分为ASCII码文件和二进制码文件两种。

  ASCII文件也称为文本文件,这种文件在磁盘中存放时每个字符对应一个字节,用于存放对应的ASCII码。例如,数5678的存储形式为:
ASC码:  00110101 00110110 00110111 00111000
     ↓     ↓    ↓    ↓
十进制码: 5     6    7    8 共占用4个字节。ASCII码文件可在屏幕上按字符显示, 例如源程序文件就是ASCII文件,用DOS命令TYPE可显示文件的内容。 由于是按字符显示,因此能读懂文件内容。

  二进制文件是按二进制的编码方式来存放文件的。 例如, 数5678的存储形式为: 00010110 00101110只占二个字节。二进制文件虽然也可在屏幕上显示, 但其内容无法读懂。

1949存储为079D(对应二进制为0000 0111 1001 1101,即十进制1949的等值数)

 

分享到:
评论

相关推荐

    writeexcel:ruby gem用于写入跨平台Excel二进制文件

    platform Excel binary file. Multiple worksheets can be added to a workbook and formatting can be applied to cells. Text, numbers, formulas, hyperlinks, images and charts can be written to the cells. T...

    mimer:Mimer 尝试使用 unix 的 `file` 命令查找文件的 MIME 类型。 从不使用文件扩展名来标识文件

    charset=binary"mimer.text?=> falsemimer.image?=> true获取代码关于补丁/拉取请求的说明分叉项目。 进行功能添加或错误修复。 为其添加测试。 这很重要,所以我不会在未来的版本中无意中破坏它。 提交,不要弄乱 ...

    hadoop_the_definitive_guide_3nd_edition

    Ruby 37 Python 40 iii www.it-ebooks.info Hadoop Pipes 41 Compiling and Running 42 3. The Hadoop Distributed Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 ...

    基于C51单片机Proteu仿真实例及软件源码+文档说明 之-电子琴.zip

    基于C51单片机Proteu仿真实例及软件源码+文档说明 之-电子琴.zip

    #_ssm_144_mysql_在线投票系统wlw_.zip

    均包含代码,文章,部分项目包含ppt

    基于java的智乐健身后台管理系统设计与实现.docx

    基于java的智乐健身后台管理系统设计与实现.docx

    基于java的鲜花销售管理系统设计与实现.docx

    基于java的鲜花销售管理系统设计与实现.docx

    数字孪生技术在智能制造的应用与展望

    内容概要:本文回顾了近年来关于数字孪生(Digital Twin)的概念和技术的发展历程,并详尽介绍了这种技术在未来车间中的潜在应用场景和需克服的难关。数字孪生是一种用于集成物理和虚拟系统的技术手段,它能够在产品设计、智能生产和预防性维护等多个领域发挥作用。 适合人群:研究人员和工程师们希望更好地理解数字孪生的基本理论与具体应用实践。 使用场景及目标:涵盖了从产品的设计、制造过程到服役阶段故障预测的全过程,并探讨了基于DT的具体实施路径。 其他说明:强调了信息物理系统的交互融合是DT成功部署的基础,而建立高准确度的多维虚拟模型、孪生数据驱动和实现动态实时交互将是推进这一前沿概念向前发展的关键。

    java-ssm+vue线上旅行信息管理系统实现源码(项目源码-说明文档)

    管理员能够查看、添加、修改和删除景点分类信息,景点分类管理 用户在系统前台可查看系统信息,包括首页、景点信息、酒店信息以及客房信息等 用户要登录界面可输入用户名、密码,点击登录按钮进行登录系统 项目关键技术 开发工具:IDEA 、Eclipse 编程语言: Java 数据库: MySQL5.7+ 后端技术:ssm 前端技术:Vue 关键技术:springboot、SSM、vue、MYSQL、MAVEN 数据库工具:Navicat、SQLyog

    网络管理是什么 网络管理做什么 网络管理长什么样 1.带机量(支持用户数) 2.端口数 3.吞吐量,并发连接数 4.

    关键字:UTM/企业防火墙/NGFW 功能:vpn,sdwan,无线控制器,ldap,双因子认证,nat/透明,终端控制,av防病毒,ips入侵检测,应用控制,web过滤, FortiGate UTM/NGFW产品 ①端口:8口、16口、24口、48口 ②层数:二层(接入层交换机、傻瓜式交换机)、三层(核心交换机、汇聚交换机) ③是否网管 网管: 非网管(傻瓜式交换机,即插即用) WEB网管(S1720GW系列) 全网管(S1720GFR系列,S2700以上的型号都为全网管) ④速率:百兆、千兆、万兆等 1.带机量(支持用户数) 2.端口数 3.吞吐量,并发连接数 4.功能 功能差异须知 NG-xxxx产品特有功能 TG-xxxx&NG-xxxx共有功能 TG-xxxx产品特有功能 FW:网络接入、安全防护、高可用性、身份认证、系统服务、系统管理 ●应用层DDOS攻击防护 负载均衡:多链路负载均衡与备份、多链路智能选路技术、多算法的服务器负载均衡 ●异常行为分析:内置统计只能算法,智能学习

    让数据库设计变得更简单!立即使用我们的SQLite3数据库设计工具,提升您的开发效率,轻松应对各种数据库管理挑战!

    简化设计,轻松管理 一键设计:快速创建和管理SQLite3数据库,简化数据库设计流程。 高效查询:强大的查询功能,帮助您轻松处理复杂的SQL语句。 功能全面,灵活便捷 可视化界面:直观的图形界面,让您无需编写繁琐的SQL代码,即可完成数据库设计。 数据同步:支持实时数据同步,确保您的数据库始终保持最新状态。 高度兼容,广泛适用 跨平台支持:适用于Windows、Mac和Linux等多种操作系统,满足不同开发环境的需求。 多语言集成:与C、C++等主流编程语言无缝对接,提高开发效率。 安全可靠,稳定运行 数据备份:内置数据备份与恢复功能,保障数据安全。 性能优化:优化数据库结构,提升查询速度和存储效率。

    小程序-滴滴作业.zip

    小程序-滴滴作业.zip 小程序-滴滴作业.zip 小程序-滴滴作业.zip

    地线贴标_复制.ezd

    地线贴标_复制.ezd

    Chrome 开阔绿地草地主题UrosDornik.zip

    Chrome主题-开阔绿地草地主题

    基于java校园兼职平台设计与实现.docx

    基于java校园兼职平台设计与实现.docx

    基于java的旅游管理系统设计与实现.docx

    基于java的旅游管理系统设计与实现.docx

    基于java的技术大健康综合咨询问诊平台的设计与实现.docx

    基于java的技术大健康综合咨询问诊平台的设计与实现.docx

    #_ssm_140_mysql_银发在线教育云平台_.zip

    均包含代码,文章,部分项目包含ppt

    基于SSM+Vue的计算机公共基础课程mooc教学平台的设计和实现.zip(毕设&课设&实训&大作业&竞赛&项目)

    项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。

Global site tag (gtag.js) - Google Analytics