Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String to buffer converter API #10251

Open
gus-ghielec opened this issue Oct 31, 2024 · 1 comment
Open

String to buffer converter API #10251

gus-ghielec opened this issue Oct 31, 2024 · 1 comment

Comments

@gus-ghielec
Copy link

There is no way to convert a string to a buffer. An example use is when someone is sending a string over I2C, to a display for example. UART today has the option to take a string directly, I2C does not. We could just send a buffer to I2C but there is no way to convert a string to a buffer.

So, to send a string to an I2C display:

  1. There is no I2C API to take strings.
  2. There is no API to convert a string to a buffe, to then send the buffer to I2C.

The only way today is to manually loop and create a buffer from the string. This is a difficult use for teachers/students, plus this string to buffer can be used in other cases.

Btw, I believe the system has buffer to string, but not a string to buffer!

@eanders-ms
Copy link
Contributor

As mentioned on the forum here, it looks like we have a gap in our documentation. Buffer has a static method called fromUTF8 that does this.

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

Here is a small example that exposes the function above as a block in the Text category.
https://makecode.microbit.org/63003-32829-24965-36494
Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants