reschu
reschu

Reputation: 1105

What's the best way to convert an int to a char in Julia?

I'm new to Julia and I was wondering what the best, convenient, readable, fastest way of doing it is.

Example: 5 -> '5'

My best approach for now is:

c = string(i)[1]

or

c = char('0'+i)

Upvotes: 6

Views: 1908

Answers (2)

rickhg12hs
rickhg12hs

Reputation: 11942

If you want a Julia function that will replace digits within an integer, here's one way to do it. There's really no error checking so this is fragile to nutty inputs.

julia> function replace_int_digs{T <: Union(BigInt, Unsigned, Signed)}(x::T, locs::Vector{Int}, digs::Vector{Int})                                        
           x_str_array = split(string(x), "")                                                                                                             
           for (k, loc) in enumerate(locs)                                                                                                                
               x_str_array[loc] = string(digs[k])                                                                                                         
           end                                                                                                                                            
           return parseint(T, join(x_str_array))                                                                                                          
       end                                                                                                                                                
replace_int_digs (generic function with 1 method)                                                                                                         

julia> replace_int_digs(12345,[3],[9])                                                                                                                    
12945                                                                                                                                                     

julia> replace_int_digs(big(12345),[3],[9])                                                                                                               
12945                                                                                                                                                     

julia> replace_int_digs(big(12345),[3,1],[9,7])                                                                                                           
72945                                                                                                                                                     

julia> replace_int_digs(int32(12345),[3,1],[9,7])                                                                                                         
72945                                                                                                                                                     

Upvotes: 1

Sebastian Good
Sebastian Good

Reputation: 6360

Your proposed second answer is easy to understand, correct (for latin numerals!), and fast.

digittochar(d) = Char('0' + d)

Depending on the expected cleanliness of the upstream data, you might add some checks to make sure you don't get something unexpected; after all

digittochar(6002) = អ

Upvotes: 2

Related Questions