Convert integer to string in Julia

I want to convert an integer to a string in Julia.

When I try:

a = 9500
b = convert(String,a)

I get an error:

ERROR: LoadError: MethodError: Cannot 'convert' an object of type Int64 to an object of type String
This may have arisen from a call to the constructor String(...),
since type constructors fall back to convert methods.
 in include_from_node1(::String) at ./loading.jl:488
 in process_options(::Base.JLOptions) at ./client.jl:265
 in _start() at ./client.jl:321
while loading ..., in expression starting on line 16

I'm not sure why Int64 cannot be converted to strings.

I tried defining how different types are, for example a a = UInt64(9500), but getting similar errors.

I know that this is very simple, and tried to find the right way to do it here , but could not figure it out.

+10
source share
1 answer

You should use:

b = string(a)

or

b = repr(a)

stringThe function can be used to create a string from any value using printa repruses showall. In the case, Int64this is equivalent.

, , , - .

Julia , bin, dec, hex, oct base.

Julia 1.0 , base . bitstring . :

julia> string(100)
"100"

julia> string(100, base=16)
"64"

julia> string(100, base=2)
"1100100"

julia> bitstring(100)
"0000000000000000000000000000000000000000000000000000000001100100"

julia> bitstring(UInt8(100))
"01100100"

julia> string(100.0)
"100.0"

julia> string(100.0, base=2)
ERROR: MethodError: no method matching string(::Float64; base=2)
Closest candidates are:
  string(::Any...) at strings/io.jl:156 got unsupported keyword argument "base"
  string(::String) at strings/substring.jl:146 got unsupported keyword argument "base"
  string(::SubString{String}) at strings/substring.jl:147 got unsupported keyword argument "base"
  ...
Stacktrace:
 [1] top-level scope at none:0

julia> bitstring(100.0)
"0100000001011001000000000000000000000000000000000000000000000000"
+15

Source: https://habr.com/ru/post/1672025/


All Articles