infused / dbf

DBF is a small, fast Ruby library for reading dBase, xBase, Clipper, and FoxPro database files.
http://rdoc.info/projects/infused/dbf
MIT License
262 stars 88 forks source link

How is the default encoding chosen? #88

Closed rywall closed 5 years ago

rywall commented 5 years ago

For this DBF file Tit.dbf.zip the header encoding is nil and I am not passing in an encoding, so I would expect the encoding to be the default encoding (UTF-8) but instead it is ASCII-8BIT. Why is this the case?

> DBF::Table.new('Tit.dbf').encoding
=> nil
> DBF::Table.new('Tit.dbf').record(0).attributes.first.first.encoding
=> #<Encoding:ASCII-8BIT>
infused commented 5 years ago

Since a dbf file can contain binary data we open the file with 'rb' options which sets the default encoding to ASCII-8BIT. If you know that your file is encoded as UTF-8, set the encoding when opening the file:

 DBF::Table.new('Tit.dbf', nil, 'UTF-8').encoding
 => "UTF-8"
rywall commented 5 years ago

Makes sense, thanks!