Le samedi 03 octobre 2015 à 23:27 -0700, Roman Sinayev a écrit :
> I am using julia version 0.3.11 from the Ubuntu ppa.
>
> When I try to convert an integer to a char, it fails with
>
> julia> Char(120)
> ERROR: type cannot be constructed
>
> This is an example from
> http://julia.readthedocs.org/en/latest/manual/strings/#characters
This is because the "latest manual" is about Julia 0.4 (granted, this
is not super conspicuous).
> The reason I'm trying to do that is because I have an array of bytes
> such as
> Array{Uint8,1}
>
> One of the things I'm doing is converting those to chars to find some
> characters such as slashes ("/").
>
> # read in file with the data
> julia> s = open(readbytes, "a.dat");
>
> # These are bytes so Uint8
> julia> typeof(s)
> Array{Uint8,1}
>
> First byte
> julia> s[1]
> 0x2f
>
> julia> char(s[1])
> '/'
>
> # so we can't compare a byte and a character from ASCIIString
> julia> s[1] == "/"
> false
"/" is not a character from ASCIIString, it is an ASCIIString. Use '/'
instead. This is equivalent to "/"[1].
Regards
> # we also can't compare characters and individual characters from
> ASCIIString
> julia> char(s[1]) == "/"
> false
>
> julia> Char(s[1])
> ERROR: type cannot be constructed
>
> # And we cannot get individual chars from ASCIIString this way
> julia> char("/")
> ERROR: `convert` has no method matching convert(::Type{Char},
> ::ASCIIString)
> in char at char.jl:1
>
> julia> convert(Uint8, "/")
> ERROR: `convert` has no method matching convert(::Type{Uint8},
> ::ASCIIString)
> in convert at base.jl:13
>
>
>
> What I ended up doing was
>
> julia> slash = 0x2f;
>
> julia>cnt = 0
> for c in s
> if c == slash
> cnt += 1
> end
> end
>
>
> So I have 2 questions:
>
> 1. Should Char conversion work or is it being phased out?
> 2. How would I compare a string element with a slash in Julia
> assuming I don't know the hex code?
> Or how would I make the above code more idiomatic?
>