35

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

When I try:

a = 9500
b = convert(String,a)

I get the 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 am not sure why Int64's cannot be converted to strings.

I have tried defining a as different types, for example a = UInt64(9500), but get similar errors.

I know this is very basic and have tried looking for the correct way to do this here, but couldn't figure it out.

sophros
  • 14,672
  • 11
  • 46
  • 75
Mike
  • 619
  • 1
  • 5
  • 13
  • [any use](https://en.wikibooks.org/wiki/Introducing_Julia/Strings_and_characters)? – daycaster Mar 10 '17 at 22:24
  • Duplicate of [How to convert any type into String in Julia](http://stackoverflow.com/questions/41929009/how-to-convert-any-type-into-string-in-julia). Voting to close. – Colin T Bowers Mar 10 '17 at 23:52

1 Answers1

41

You should use:

b = string(a)

or

b = repr(a)

string function can be used to create string from any value using print and repr uses showall. In case of Int64 this is equivalent.

And actually this is probably the reason why convert does not work - as there are many ways to convert integer to string depending on the choice of base.

EDIT

For integers you could convert them in the past version of Julia to string also using bin, dec, hex, oct or base.

Past Julia 1.0 you can do the conversions in different bases using string function with base keyword argument for integers. Also you have bitstring function that gives the literal bit representation of a number. Here are some examples:

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"
Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107
  • 1
    Small correction: `string` can be used to convert any value to a subtype of `AbstractString`, i.e. the output of `string` is not *always* of type `String`. – Colin T Bowers Mar 10 '17 at 23:56
  • 1
    `bin`, `hex`, `base`, etc. were deprecated. The new way to print in a different base is: `string(42, base = 2)`. It also supports padding. – cmc Jul 18 '19 at 01:14