# How to write a file with a "Byte Order Mark"?

**URL:** <https://discourse.nodered.org/t/how-to-write-a-file-with-a-byte-order-mark/86464>\
**Category:** General\
**Created:** [15 March 2024 16:30 UTC](https://discourse.nodered.org/t/how-to-write-a-file-with-a-byte-order-mark/86464 "2024-03-15T16:30:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yury](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yury](https://discourse.nodered.org/u/Yury)\
**Post date:** [15 March 2024 16:30 UTC](https://discourse.nodered.org/t/how-to-write-a-file-with-a-byte-order-mark/86464/1 "2024-03-15T16:30:49Z")

</div>

Hello.  
How to use the "file" node to write a file with a "Byte Order Mark" ?  
Or how to do it using fs.writeFileSync() ?  
I am grateful in advance.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 March 2024 16:33 UTC](https://discourse.nodered.org/t/how-to-write-a-file-with-a-byte-order-mark/86464/2 "2024-03-15T16:33:58Z")

</div>

Have you tried simply sending a buffer containing the BOM (and any other data) to a file write node.

---

<div class="post-metadata">

**Author:** ![Yury](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yury](https://discourse.nodered.org/u/Yury)\
**Post date:** [15 March 2024 19:50 UTC](https://discourse.nodered.org/t/how-to-write-a-file-with-a-byte-order-mark/86464/3 "2024-03-15T19:50:23Z")

</div>

Thank you for responding!  
And how can a text string be converted to a buffer string to add '0hEF 0hBB 0hBF' (for UTF-8) to it ?  
Or how do I convert '0hEF 0hBB 0hBF' to add to a text string ?

---

<div class="post-metadata">

**Author:** ![Yury](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yury](https://discourse.nodered.org/u/Yury)\
**Post date:** [20 March 2024 08:45 UTC](https://discourse.nodered.org/t/how-to-write-a-file-with-a-byte-order-mark/86464/4 "2024-03-20T08:45:49Z")

</div>

Using '0hEF 0hBB 0hBF' results in adding a string, not bytes.  
How do I add exactly 3 bytes?

---

<div class="post-metadata">

**Author:** ![Yury](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yury](https://discourse.nodered.org/u/Yury)\
**Post date:** [25 March 2024 09:18 UTC](https://discourse.nodered.org/t/how-to-write-a-file-with-a-byte-order-mark/86464/5 "2024-03-25T09:18:29Z")

</div>

In the script below, I read the file from the BOM as a sample.  
Then I try to create exactly the same Buffer.  
But instead of a Buffer, I get an Object.  
What is my mistake?

```auto
msg.payload = msg.payload; // read file with BOM as binary // => buffer[5] Good !

var buffer = new ArrayBuffer(5); //5 bytes
msg.payload_1 = buffer; // => object empty Why object ?

var intView = new Int8Array(buffer); //8 bits
//intView = [0xEF,0xBB,0xBF,0x31,0x32]; //for UTF8 BOM=#EFBBBF 0x31,0x32=12
intView[0] = 0xEF; //1byte BOM
intView[1] = 0xBB; //2byte BOM
intView[2] = 0xBF; //3byte BOM
intView[3] = 0x31; //'1'
intView[4] = 0x32; //'2'
msg.payload_2 = intView; // => object: {"0":-17,"1":-69,"2":-65,"3":49,"4":50}

//buffer = new Uint8Array([0xEF,0xBB,0xBF,0x31,0x32]);
msg.payload_3 = buffer; // => object empty Why object ?

msg.filename = '/opt/12.txt';

return msg;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 March 2024 09:26 UTC](https://discourse.nodered.org/t/how-to-write-a-file-with-a-byte-order-mark/86464/6 "2024-03-25T09:26:51Z")

</div>

Use a node [Buffer](https://nodejs.org/docs/latest-v18.x/api/buffer.html).

```auto
// using from
const buf1 = Buffer.from([0xEF,0xBB,0xBF,0x31,0x32])

// using alloc
const buf2 = Buffer.alloc(5)
buf2.writeUint8(0xEF,0)
buf2.writeUint8(0xBB,1)
buf2.writeUint8(0xBF,2)
buf2.writeUint8(0x31,3)
buf2.writeUint8(0x32,4)

```

Alternatively, use `node-red-contrib-buffer-parser` (it has a node named `buffer-maker` so you can do this in a low-code visual manner

---

<div class="post-metadata">

**Author:** ![Yury](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yury](https://discourse.nodered.org/u/Yury)\
**Post date:** [25 March 2024 09:51 UTC](https://discourse.nodered.org/t/how-to-write-a-file-with-a-byte-order-mark/86464/7 "2024-03-25T09:51:31Z")

</div>

> [@Steve-Mcl](#):
>
> Use a node [Buffer](https://nodejs.org/docs/latest-v18.x/api/buffer.html)

It's very important to me. Thank you the first time

> [@Steve-Mcl](#):
>
> // using from

It's important to me. Thank you the second time

> [@Steve-Mcl](#):
>
> // using alloc

It's important to me too. Thank you the third time

> [@Steve-Mcl](#):
>
> use `node-red-contrib-buffer-parser`

It's Useful to me. Thank you the fourth time

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [8 April 2024 09:52 UTC](https://discourse.nodered.org/t/how-to-write-a-file-with-a-byte-order-mark/86464/8 "2024-04-08T09:52:01Z")

</div>

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