Post Reply 
(Free42) Converting Unicode code point <--> C octal escape
Yesterday, 07:27 PM (This post was last modified: Yesterday 07:32 PM by Thomas Okken.)
Post: #1
(Free42) Converting Unicode code point <--> C octal escape
This is a program I wrote to convert between Unicode code points, that is, numbers between 0 and 1,114,111 and the equivalent escape sequences you can use in C code. The escape sequence is the UTF-8 representation of the code point, encoded as octal escape sequences.

Example:

To convert U+2221 to C:

BASE A...F 2221 XEQ "CP2OCT"
X: "\342\210\241"


Convert \342\210\241 to code point:

XSTR "\342\210\241" XEQ "OCT2CP"
Y: 8,737
X: "U+2221"


The code uses the recently added GETLI and PUTLI functions, so you need at least Free42 3.1.11 or Plus42 1.1.16. Or actually, if you want to be able to paste the listing into the app without having to fix it up, you need Free42 3.1.12 or Plus42 1.1.17, because pasting those new functions didn't work in 3.1.11 / 1.1.16. There are three occurrences of those functions, on lines 88, 90, and 187.

Code:
00 { 562-Byte Prgm }
01▸LBL "CP2OCT"
02 FUNC 11
03 REAL?
04 SKIP
05 RTNERR 4
06 IP
07 X<0?
08 RTNERR 5
09 1114112
10 X≤Y?
11 RTNERR 5
12 FS? 00
13 GTO 00
14 CLX
15 55296
16 X>Y?
17 GTO 00
18 CLX
19 57344
20 X>Y?
21 RTNERR 5
22▸LBL 00
23 EXITALL
24 CLX
25 128
26 X≤Y?
27 GTO 01
28 XSTR "\"
29 RCL ST Z
30 →OCT
31 APPEND
32 RTN
33▸LBL 01
34 CLX
35 2048
36 X>Y?
37 GTO 02
38 32
39 ×
40 X>Y?
41 GTO 02
42 32
43 ×
44▸LBL 02
45 LSTO "B"
46 R↓
47 LSTO "P"
48 NEWLIST
49 LSTO "E"
50▸LBL 03
51 RCL "E"
52 RCL "P"
53 64
54 MOD
55 128
56 +
57 APPEND
58 STO "E"
59 RCL "P"
60 64
61 ÷
62 IP
63 STO "P"
64 RCL "B"
65 64
66 ÷
67 IP
68 STO "B"
69 X≠0?
70 GTO 03
71 RCL "E"
72 REV
73 STO "E"
74 LENGTH
75 2
76 RCL ST Y
77 1
78 -
79 Y↑X
80 1
81 -
82 2
83 8
84 RCL- ST T
85 Y↑X
86 ×
87 1
88 GETLI "E"
89 RCL+ ST Z
90 PUTLI "E"
91 XSTR ""
92▸LBL 04
93 HEAD "E"
94 SKIP
95 RTN
96 →OCT
97 XSTR "\"
98 X<>Y
99 APPEND
100 APPEND
101 GTO 04
102▸LBL "OCT2CP"
103 FUNC 12
104 STR?
105 SKIP
106 RTNERR 4
107 HEAD ST X
108 SKIP
109 RTNERR 5
110 XSTR "\"
111 X≠Y?
112 RTNERR 5
113 R↓
114 APPEND
115 LSTO "E"
116 NEWLIST
117 LSTO "L"
118▸LBL 05
119 RCL "E"
120 DUP
121 DUP
122 XSTR "\"
123 POS
124 1
125 +
126 SUBSTR
127 STO "E"
128 CLX
129 LASTX
130 1
131 -
132 0
133 X<>Y
134 SUBSTR
135 ENTER
136 LENGTH
137 X=0?
138 RTNERR 5
139 3
140 X<Y?
141 RTNERR 5
142 R↓
143 R↓
144▸LBL 06
145 DUP
146 XSTR "0"
147 POS
148 X≠0?
149 GTO 07
150 R↓
151 1
152 SUBSTR
153 GTO 06
154▸LBL 07
155 R↓
156 XSTR ""
157 X<>Y
158 X=Y?
159 XSTR "0"
160 ENTER
161 SF 25
162 S→N
163 FC?C 25
164 RTNERR 5
165 ENTER
166 N→S
167 X≠? ST Z
168 RTNERR 5
169 R↓
170 SF 25
171 →DEC
172 FC?C 25
173 RTNERR 5
174 RCL "L"
175 X<>Y
176 APPEND
177 STO "L"
178 RCL "E"
179 XSTR ""
180 X≠Y?
181 GTO 05
182 1
183 RCL "L"
184 LENGTH
185 X≠Y?
186 GTO 08
187 GETLI "L"
188 128
189 X≤Y?
190 RTNERR 5
191 R↓
192 GTO 10
193▸LBL 08
194 X=0?
195 RTNERR 5
196 4
197 X<Y?
198 RTNERR 5
199 CLX
200 2
201 RCL ST Y
202 Y↑X
203 1
204 -
205 2
206 8
207 RCL- ST T
208 Y↑X
209 ×
210 DUP
211 2
212 ÷
213 128
214 +
215 HEAD "L"
216 AND
217 X≠Y?
218 RTNERR 5
219 LASTX
220 X<>Y
221 -
222▸LBL 09
223 HEAD "L"
224 SKIP
225 GTO 10
226 DUP
227 192
228 AND
229 128
230 X≠Y?
231 RTNERR 5
232 R↓
233 R↓
234 64
235 MOD
236 X<>Y
237 LASTX
238 ×
239 +
240 GTO 09
241▸LBL 10
242 ENTER
243 HEXM
244 N→S
245 DECM
246 4096
247 X>? ST Z
248 GTO 11
249 R↓
250 GTO 12
251▸LBL 11
252 R↓
253 XSTR "000"
254 X<>Y
255 APPEND
256 -4
257 SUBSTR
258▸LBL 12
259 XSTR "U+"
260 X<>Y
261 APPEND
262 END
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)