Le lundi 24 octobre 2016 à 21:44 -0700, Chris Stook a écrit :
> I'm trying to parse a text file which contains some floating point
> numbers.  The number 2.5 is represented by the string
> "\x002\0.\x005\0".  Parse will not convert this to a Float64.  Print
> works (prints "2.5") in Atom and Jupyter, but not in the REPL.
> 
> _
> _       _ _(_)_     |  A fresh approach to technical computing
> (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
> _ _   _| |_  __ _   |  Type "?help" for help.
> | | | | | | |/ _` |  |
> | | |_| | | | (_| |  |  Version 0.5.0 (2016-09-19 18:14 UTC)
> _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
> |__/                   |  x86_64-w64-mingw32
> 
> julia> print("\x002\0.\x005\0")
> �2�.�5�
> julia> parse(Float64,"\x002\0.\x005\0")
> ERROR: ArgumentError: invalid number format "\x002\0.\x005\0" for
> Float64
> in parse(::Type{Float64}, ::String) at .\parse.jl:167
> 
> julia>
> 
> I am not familiar with Unicode.  Is the Unicode valid?  How should I
> convert this to a Float?  I do not have control over the input file.
"Unicode" doesn't refer to a specific encoding. Julia expects UTF-8,
but this appears to be UTF16-BE (which is often abusively called
Unicode in the Windows world).

You can use my StringEncodings package to decode the file to a Julia
string (see the README):

julia> using StringEncodings

julia> parse(Float64, decode("\x002\0.\x005\0".data, "UTF-16BE"))
2.5


Regards

Reply via email to