在Julia中将整数转换为字符串

Mik*_*ike 14 julia

我想在Julia中将整数转换为字符串.

当我尝试:

a = 9500
b = convert(String,a)
Run Code Online (Sandbox Code Playgroud)

我收到错误:

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
Run Code Online (Sandbox Code Playgroud)

我不确定为什么Int64不能转换为字符串.

a例如a = UInt64(9500),我尝试定义为不同的类型,但得到类似的错误.

我知道这是非常基本的,并试图在这里寻找正确的方法,但无法弄明白.

Bog*_*ski 20

你应该使用:

b = string(a)
Run Code Online (Sandbox Code Playgroud)

要么

b = repr(a)
Run Code Online (Sandbox Code Playgroud)

stringfunction可用于从任何值使用printrepr使用创建字符串showall.在Int64这种情况下是等效的.

对于整数你也可以使用它们转换为字符串bin,dec,hex,octbase.

实际上这可能是转换不起作用的原因 - 因为根据基数的选择,有很多方法可以将整数转换为字符串.