# Converting Char type to Int type

**URL:** <https://discourse.julialang.org/t/converting-char-type-to-int-type/123320>\
**Category:** New to Julia\
**Created:** [December 1, 2024, 7:13am UTC](https://discourse.julialang.org/t/converting-char-type-to-int-type/123320 "2024-12-01T07:13:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 1, 2024, 7:13am UTC](https://discourse.julialang.org/t/converting-char-type-to-int-type/123320/1 "2024-12-01T07:13:02Z")

</div>

Hello,  
I have a file similar to the following:

```julia
1 2 3 4 5 6 7 8 9 10

```

I wrote the following program to read this file character by character and convert its numbers to `Int` type:

```julia
function summer()
    inn = open("input.txt","r")
    c = 0 
    while !eof(inn)
        c = read(inn, Char)
        if c != ' '
             println(parse(Int, c))
        end
    end
end

summer()

```

I have two questions:  
1- Is this program correct?

2- I got the following error:

```julia
ERROR: LoadError: ArgumentError: invalid digit: '\n'
Stacktrace:
  [1] parse(::Type{Int64}, c::Char; base::Int64)
    @ Base ./parse.jl:44
  [2] parse
    @ ./parse.jl:41 [inlined]
  [3] summer()
    @ Main /Download/file.jl:341
  [4] top-level scope
    @ /Download/file.jl:345
  [5] include(fname::String)
    @ Base.MainInclude ./client.jl:489
  [6] run(debug_session::VSCodeDebugger.DebugAdapter.DebugSession, error_handler::VSCodeDebugger.var"#3#4"{String})
    @ VSCodeDebugger.DebugAdapter ~/.vscode-oss/extensions/julialang.language-julia-1.127.2-universal/scripts/packages/DebugAdapter/src/packagedef.jl:122
  [7] startdebugger()
    @ VSCodeDebugger ~/.vscode-oss/extensions/julialang.language-julia-1.127.2-universal/scripts/packages/VSCodeDebugger/src/VSCodeDebugger.jl:45
  [8] top-level scope
    @ ~/.vscode-oss/extensions/julialang.language-julia-1.127.2-universal/scripts/debugger/run_debugger.jl:12
  [9] include(mod::Module, _path::String)
    @ Base ./Base.jl:495
 [10] exec_options(opts::Base.JLOptions)
    @ Base ./client.jl:318
 [11] _start()
    @ Base ./client.jl:552

```

Why?

Thank you.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [December 1, 2024, 7:28am UTC](https://discourse.julialang.org/t/converting-char-type-to-int-type/123320/2 "2024-12-01T07:28:08Z")

</div>

> [@hack3rcon](#):
>
> 1- Is this program correct?

That kind of depneds on what you want to do and how to treat (2) depends on that answer.  
Do you want to convert `Char`s to the number they represent (so `'1'` becomes `1`) or to their value if you interpreted the actual bits of the char as number (i.e. it’s codepoint IIRC such that `'1'` becomes 49)?

The error (2) is caused because to try to convert the final newline character `'\n'` to the numeric value it represents. That ofc fails because that char does not represent a number.  
If you want to former option (convert to value the `Char` represents), then you need to skip newlines as well (in addition to the spaces you already skip). If you want the codepoints, then you need to use `convert(Int, char)` instead of `parse(Int, char)`

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [December 1, 2024, 7:45am UTC](https://discourse.julialang.org/t/converting-char-type-to-int-type/123320/3 "2024-12-01T07:45:00Z")

</div>

If the file is not enormous such that memory is not a problem, just read it as a single string, split and parse it…

---

<div class="post-metadata">

**Author:** ![AMJ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amj/32/214096_2.png) [@AMJ](https://discourse.julialang.org/u/AMJ)\
**Post date:** [December 1, 2024, 8:50am UTC](https://discourse.julialang.org/t/converting-char-type-to-int-type/123320/4 "2024-12-01T08:50:30Z")

</div>

Adding to already great explanations, assuming you want to convert `string` numbers to `Int` numbers you can slightly modify your code and do this:

```julia
function summer()
           inn = open("input.txt","r")
           c = 0 
           while !eof(inn)
               c = readuntil(inn, Char(' '))
               println(parse(Int, c))
           end
       end

```

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 1, 2024, 9:19am UTC](https://discourse.julialang.org/t/converting-char-type-to-int-type/123320/5 "2024-12-01T09:19:33Z")

</div>

Hello,  
Thanks for all the answers.  
I changed the program as below and it worked:

```julia
function summer()
    inn = open("input.txt","r")
    c = 0 
    while !eof(inn)
        c = read(inn, Char)
        if c != ' ' && c != '\n'
             println(parse(Int, c))
        end
    end
end

summer()

```

---

<div class="post-metadata">

**Author:** ![magister-ludi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magister-ludi/32/4003_2.png) [@magister-ludi](https://discourse.julialang.org/u/magister-ludi)\
**Post date:** [December 1, 2024, 9:51am UTC](https://discourse.julialang.org/t/converting-char-type-to-int-type/123320/6 "2024-12-01T09:51:25Z")

</div>

Depending on what you’re really trying to do, you might want to look at [`isdigit`](https://docs.julialang.org/en/v1/base/strings/#Base.Unicode.isdigit) or [`isnumeric`](https://docs.julialang.org/en/v1/base/strings/#Base.Unicode.isnumeric).

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 1, 2024, 10:09am UTC](https://discourse.julialang.org/t/converting-char-type-to-int-type/123320/7 "2024-12-01T10:09:00Z")

</div>

And `isspace`
