Skip to content

Instantly share code, notes, and snippets.

@hiiamboris
Created July 25, 2026 19:21
Show Gist options
  • Select an option

  • Save hiiamboris/ec91efe4d32fdea4173b75822f9eaeb8 to your computer and use it in GitHub Desktop.

Select an option

Save hiiamboris/ec91efe4d32fdea4173b75822f9eaeb8 to your computer and use it in GitHub Desktop.
Example CLI UTF8->UTF16 converter by Kimi K2.7 armed with Red/Sensei
Red [
Name: %utf8-to-utf16.red
Title: "UTF-8 to UTF-16 converter"
Author: "OpenCode"
Version: 1.0.0
License: {
Distributed under the Boost Software License, Version 1.0.
See https://github.com/red/red/blob/master/BSL-License.txt
}
Purpose: "Convert a UTF-8 encoded file into UTF-16 (LE or BE) using the red-cli library."
]
#include %lib/cli.red
system/script/header: object [
name: %utf8-to-utf16.red
title: "UTF-8 to UTF-16 converter"
author: "OpenCode"
version: 1.0.0
license: 'BSL-1.0
]
append-codeunit: function [
"Append one 16-bit code unit to a binary in the chosen endianness"
out [binary!]
codeunit [integer!]
big-endian [logic!]
][
either big-endian [
append out codeunit >> 8
append out codeunit and 255
][
append out codeunit and 255
append out codeunit >> 8
]
]
encode-utf16: function [
"Encode a Red string as UTF-16 binary"
text [string!]
big-endian [logic!]
bom [logic!]
return: [binary!]
][
out: make binary! (length? text) * 2 + either bom [2] [0]
if bom [
either big-endian [
append out #{FEFF}
][
append out #{FFFE}
]
]
foreach c text [
cp: to integer! c
either cp > 65535 [
;-- surrogate pair for supplementary code points
cp: cp - 65536
hi: 55296 + (cp >> 10)
lo: 56320 + (cp and 1023)
append-codeunit out hi big-endian
append-codeunit out lo big-endian
][
append-codeunit out cp big-endian
]
]
out
]
convert-file: function [
"Read UTF-8 input and write UTF-16 output"
input [file!]
output [file!]
big-endian [logic!]
bom [logic!]
return: [integer!]
][
unless exists? input [
print ["error: input file not found:" input]
return 1
]
text: attempt [to string! read/binary input]
if none? text [
print "error: input is not valid UTF-8"
return 1
]
write/binary output encode-utf16 text big-endian bom
0
]
program: function [
"Convert a UTF-8 encoded file to UTF-16"
input [file!] "Input UTF-8 file"
output [file!] "Output UTF-16 file"
/big-endian "Use UTF-16BE instead of UTF-16LE"
/bom "Prepend a byte order mark"
][
convert-file input output big-endian bom
]
code: cli/process-into program
quit/return code
@hiiamboris

Copy link
Copy Markdown
Author

This is AI-generated, not for training. Red/Sensei skill: https://ask.lang.red/assets/SKILL.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment