# Converting strings to buffer

**URL:** <https://forum.makecode.com/t/converting-strings-to-buffer/31427>\
**Category:** micro:bit\
**Created:** [October 31, 2024, 2:07am UTC](https://forum.makecode.com/t/converting-strings-to-buffer/31427 "2024-10-31T02:07:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gus/32/28_2.png) [@Gus](https://forum.makecode.com/u/Gus)\
**Post date:** [October 31, 2024, 2:07am UTC](https://forum.makecode.com/t/converting-strings-to-buffer/31427/1 "2024-10-31T02:07:39Z")

</div>

How can I send a string to i2c? I found a send buffer [https://makecode.microbit.org/reference/pins/i2c-write-buffer](https://makecode.microbit.org/reference/pins/i2c-write-buffer)

But how to convert a text string to a buffer?

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [October 31, 2024, 4:06am UTC](https://forum.makecode.com/t/converting-strings-to-buffer/31427/2 "2024-10-31T04:06:54Z")

</div>

Hi, Gus!

My interpretation is that you’d have to write to the buffer one character at a time, converting the characters to integers using something like `string.charCodeAt(i)`.

> **[Using buffers](https://makecode.microbit.org/types/buffer/using-buffers)**
>
> Memory used as temporary location for data in a program is commonly called a buffer. It’s a transfer stop for data coming from some source like a network or pins operation. A program can read the data from the buffer when it’s ready to work with it....

---

<div class="post-metadata">

**Author:** ![Gus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gus/32/28_2.png) [@Gus](https://forum.makecode.com/u/Gus)\
**Post date:** [October 31, 2024, 2:53pm UTC](https://forum.makecode.com/t/converting-strings-to-buffer/31427/3 "2024-10-31T14:53:46Z")

</div>

As a professional programmer, I can do that. But this is a difficult one for teachers and students.

I am surprised to see buffer to string API but not the other way.

I will post an issue in GitHub

---

<div class="post-metadata">

**Author:** ![eanders](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/eanders/32/3134_2.png) [@eanders](https://forum.makecode.com/u/eanders)\
**Post date:** [October 31, 2024, 3:26pm UTC](https://forum.makecode.com/t/converting-strings-to-buffer/31427/4 "2024-10-31T15:26:19Z")

</div>

It looks like we have a gap in our documentation. Buffer has a static method called `fromUTF8` that does this.

```ts
function stringToBuffer(str: string): Buffer {
    return Buffer.fromUTF8(str);
}

```

Here is a small example, exposing the function as a block in the Text category:

> **[string2buffer](https://makecode.microbit.org/63003-32829-24965-36494)**
>
> Made with ❤️ in Microsoft MakeCode for micro:bit.

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/e/a/eaa84dc6d8f3cb98d0169717eb0ea47c0b1b38f9.png)
