<p>This can be done with <code>Data.Binary</code>. See <a href="https://stackoverflow.com/a/2284193/10069673">https://stackoverflow.com/a/2284193/10069673</a></p>
<p>Another possibility would be to convert the <code>int</code> to a <code>string</code> and then convert the <code>string</code> to a <code>ByteString</code>.</p>
<ul>
<li><code>int</code> to <code>string</code>: <a href="https://stackoverflow.com/a/2784280/10069673">https://stackoverflow.com/a/2784280/10069673</a></li>
<li><code>string</code> to <code>ByteString</code>: <a href="https://stackoverflow.com/a/40491001/10069673">https://stackoverflow.com/a/40491001/10069673</a></li>
</ul>

CC BY-SA 4.0
2021-07-06T21:04:16.000Z
Hmm. I got the same error with `Data.Binary.encode` as I got with `Data.Aeson.encode`. Seems like it would work :/ I got Chrismo's suggestion to work though. Thanks!
Mitchell Turner
CC BY-SA 4.0
2021-07-06T05:48:14.303Z
2021-07-06T05:48:14.303Z
eddex
<p>I've just run this in the REPL with success. Not sure if it is the best way, though.</p>
<pre><code>TokenName { unTokenName = Data.ByteString.Char8.pack $ show (toInteger (getSlot 1)) }
</code></pre>

CC BY-SA 4.0
2021-07-06T21:05:04.777Z
This worked. Thanks. I agree there might be a more direct way of doing it, but I'm not picky for now. Can always improve it.
Mitchell Turner
CC BY-SA 4.0
2021-07-06T12:54:21.993Z
2021-07-06T12:54:21.993Z
Chrismo
<p>I feel like this should be pretty easy, but I'm stumped. How can I use a slot number as a token name?</p>
<p>I'm currently trying to <code>encode</code> the slot <code>integer</code> as a <code>ByteString</code>:</p>
<pre><code>slotNumberTokenName :: TokenName
slotNumberTokenName = TokenName { unTokenName = encode $ getSlot slot }
</code></pre>
<p>but I get the error</p>
<pre><code>    • Couldn't match expected type ‘ByteString’
                  with actual type ‘bytestring-0.10.12.0:Data.ByteString.Lazy.Internal.ByteString’
      NB: ‘bytestring-0.10.12.0:Data.ByteString.Lazy.Internal.ByteString’
            is defined in ‘Data.ByteString.Lazy.Internal’
          ‘ByteString’ is defined in ‘Data.ByteString.Internal’
    • In the ‘unTokenName’ field of a record
      In the expression: TokenName {unTokenName = encode $ getSlot slot}
      In an equation for ‘slotNumberTokenName’:
          slotNumberTokenName
            = TokenName {unTokenName = encode $ getSlot slot}
    |
153 |     slotNumberTokenName = TokenName { unTokenName = encode $ getSlot slot }
    | 
</code></pre>
<p>I am using <code>Data.Aeson.encode</code>, so maybe there is a different function to do that conversion.</p>
<p>Anyhow, any help would be appreciated. Been looking at this for longer than I'd like to admit.</p>

<plutus><haskell>
How can I convert `Slot` to `TokenName`
# How can I convert `Slot` to `TokenName`

(Asked by: username_0 on 2021-07-06)

I feel like this should be pretty easy, but I'm stumped. How can I use a slot number as a token name?
I'm currently trying to encode the slot integer as a ByteString:
slotNumberTokenName :: TokenName
slotNumberTokenName = TokenName { unTokenName = encode $ getSlot slot }

but I get the error
    • Couldn't match expected type ‘ByteString’
                  with actual type ‘bytestring-0.10.12.0:Data.ByteString.Lazy.Internal.ByteString’
      NB: ‘bytestring-0.10.12.0:Data.ByteString.Lazy.Internal.ByteString’
            is defined in ‘Data.ByteString.Lazy.Internal’
          ‘ByteString’ is defined in ‘Data.ByteString.Internal’
    • In the ‘unTokenName’ field of a record
      In the expression: TokenName {unTokenName = encode $ getSlot slot}
      In an equation for ‘slotNumberTokenName’:
          slotNumberTokenName
            = TokenName {unTokenName = encode $ getSlot slot}
    |
153 |     slotNumberTokenName = TokenName { unTokenName = encode $ getSlot slot }
    | 

I am using Data.Aeson.encode, so maybe there is a different function to do that conversion.
Anyhow, any help would be appreciated. Been looking at this for longer than I'd like to admit.

**Tags:** plutus, haskell

---

## 2 answers

### Answer by username_2 on 2021-07-06. Score: 3 | Accepted answer

I've just run this in the REPL with success. Not sure if it is the best way, though.
TokenName { unTokenName = Data.ByteString.Char8.pack $ show (toInteger (getSlot 1)) }

> **Comments:**
> **username_0**: This worked. Thanks. I agree there might be a more direct way of doing it, but I'm not picky for now. Can always improve it.  

---

### Answer by username_1 on 2021-07-06. Score: 1

This can be done with `Data.Binary`. See <https://stackoverflow.com/a/2284193/10069673>

Another possibility would be to convert the `int` to a `string` and then convert the `string` to a `ByteString`.

* `int` to `string`: <https://stackoverflow.com/a/2784280/10069673>
* `string` to `ByteString`: <https://stackoverflow.com/a/40491001/10069673>

> **Comments:**
> **username_0**: Hmm. I got the same error with `Data.Binary.encode` as I got with `Data.Aeson.encode`. Seems like it would work :/ I got Chrismo's suggestion to work though. Thanks!  

---

