# Parse fixed length strings with elm/parser

**URL:** https://discourse.elm-lang.org/t/parse-fixed-length-strings-with-elm-parser/4311
**Category:** Learn
**Created:** [September 13, 2019, 7:17am UTC](https://discourse.elm-lang.org/t/parse-fixed-length-strings-with-elm-parser/4311 "2019-09-13T07:17:11Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![knutel](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@knutel](https://discourse.elm-lang.org/u/knutel)
#### Post date: [September 13, 2019, 7:17am UTC](https://discourse.elm-lang.org/t/parse-fixed-length-strings-with-elm-parser/4311/1 "2019-09-13T07:17:11Z")

</div>

Hi! I’m trying to use elm/parser to parse fixed length strings. This is what I have come up with so far:

```
cString : Int -> Parser String
cString length =
    getChompedString <|
        loop 0 (helper length)

helper : Int -> Int -> Parser (Step Int Int)
helper length count =
    if length == count then
        succeed () |> map (\_ -> Done count)

    else
        succeed (Loop (count + 1))
            |. chompIf (\_ -> True)

```

Is there a better way? If the name “cString” didn’t give at away already, I’m abusing elm/parser to parse a binary file. The file contents sit in a String, where I have made sure every Char code is below 256. So far it works very well.

---

<div class="post-metadata">

### Author: ![folkertdev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/folkertdev/32/133_2.png) [@folkertdev](https://discourse.elm-lang.org/u/folkertdev)
#### Post date: [September 13, 2019, 8:24am UTC](https://discourse.elm-lang.org/t/parse-fixed-length-strings-with-elm-parser/4311/2 "2019-09-13T08:24:52Z")

</div>

I think your way is the correct one for parsing a known-length string.

But there is the [`elm/bytes`](https://package.elm-lang.org/packages/elm/bytes/latest/) package that is made for decoding binary data. Maybe that is a better fit for your problem? (the general approach would still be the same: use `loop` to count the correct number of characters)

---

<div class="post-metadata">

### Author: ![knutel](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@knutel](https://discourse.elm-lang.org/u/knutel)
#### Post date: [September 14, 2019, 8:59am UTC](https://discourse.elm-lang.org/t/parse-fixed-length-strings-with-elm-parser/4311/3 "2019-09-14T08:59:32Z")

</div>

Thanks for the pointer. I actually ended up using [zwilias/elm-bytes-parser](https://package.elm-lang.org/packages/zwilias/elm-bytes-parser/latest/), which gives mostly the same functionality as elm/parser. It does not have the nice `|.` and `|=` operators, but those I can add myself.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)
#### Post date: [September 24, 2019, 8:59am UTC](https://discourse.elm-lang.org/t/parse-fixed-length-strings-with-elm-parser/4311/4 "2019-09-24T08:59:35Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
