Given I have a string in tcl:
set s1"Hello"
I'd like to produce the following result from that string:
.byte 5,72,101,108,108,111
(5 is the length of the string)
Christian Gollwitzer schrieb am Donnerstag, 19. Mai 2022 um 13:06:31 UTC+2:
Am 19.05.22 um 12:49 schrieb Christoph Kukulies:
Given I have a string in tcl:
set s1"Hello"
I'd like to produce the following result from that string:
.byte 5,72,101,108,108,111
(5 is the length of the string)Use binary scan to get the unicode code points from your string:
(Tests) 58 % binary scan Hallo c* result
1
(Tests) 59 % puts $result
72 97 108 108 111
Then you have the code points in a list and can use usual list
processing to format them into your desired output.
ChristianThanks. This worked out so far. Excuse my lack of knowledge of tcl.
BTW, what is the c* ?
$ cat char.tcl
#!/usr/bin/tclsh
set s1 "Hello"
puts $s1
binary scan $s1 c* result
puts $result
$ Hello
72 101 108 108 111
So far so good. But $result is a list with a variable number of elements.
How do I iterate through this variable length list?
#!/usr/bin/tclsh
set s1 "Hello"
puts $s1
binary scan $s1 c* result
puts [format ".byte %d," [ string length $result] ]
Am 19.05.22 um 12:49 schrieb Christoph Kukulies:Thanks. This worked out so far. Excuse my lack of knowledge of tcl.
Given I have a string in tcl:
set s1"Hello"
I'd like to produce the following result from that string:
.byte 5,72,101,108,108,111
(5 is the length of the string)Use binary scan to get the unicode code points from your string:
(Tests) 58 % binary scan Hallo c* result
1
(Tests) 59 % puts $result
72 97 108 108 111
Then you have the code points in a list and can use usual list
processing to format them into your desired output.
Christian
Christian Gollwitzer schrieb am Donnerstag, 19. Mai 2022 um 13:06:31 UTC+2:
Am 19.05.22 um 12:49 schrieb Christoph Kukulies:Thanks. This worked out so far. Excuse my lack of knowledge of tcl.
Given I have a string in tcl:Use binary scan to get the unicode code points from your string:
set s1"Hello"
I'd like to produce the following result from that string:
.byte 5,72,101,108,108,111
(5 is the length of the string)
(Tests) 58 % binary scan Hallo c* result
1
(Tests) 59 % puts $result
72 97 108 108 111
Then you have the code points in a list and can use usual list
processing to format them into your desired output.
Christian
BTW, what is the c* ?
So far so good. But $result is a list with a variable number of elements. How do I iterate through this variable length list?
Rich schrieb am Donnerstag, 19. Mai 2022 um 15:16:53 UTC+2:
Christoph Kukulies <ku...@physik.rwth-aachen.de> wrote:Thanks. This is my outcome:
Christian Gollwitzer schrieb am Donnerstag, 19. Mai 2022 um 13:06:31 UTC+2: >>>> Am 19.05.22 um 12:49 schrieb Christoph Kukulies:That is an easy answer when looking it up in the manual: https://tcl.tk/doc/ >>
Thanks. This worked out so far. Excuse my lack of knowledge of tcl.Given I have a string in tcl:Use binary scan to get the unicode code points from your string:
set s1"Hello"
I'd like to produce the following result from that string:
.byte 5,72,101,108,108,111
(5 is the length of the string)
(Tests) 58 % binary scan Hallo c* result
1
(Tests) 59 % puts $result
72 97 108 108 111
Then you have the code points in a list and can use usual list
processing to format them into your desired output.
Christian
BTW, what is the c* ?
https://tcl.tk/man/tcl8.6/TclCmd/binary.htm:
under 'binary scan'
c
The data is turned into count 8-bit signed integers and stored in
the corresponding variable as a list. If count is *, then all of the
remaining bytes in string will be scanned. If count is omitted, then
one 8-bit integer will be scanned.
Do note that is you are looking for "unicode code points" outside of
the basic ASCII Unicode set, that using binary c* will not give you the
expected answer:
% set s1 Hello\u1e85
Hello_ľ
% binary scan $s1 c* result
1
% set result
72 101 108 108 111 -123
Instead you want to use %c from 'scan' to get the unicode code points
out if you have any Unicode characters that are also not ASCII
characters:
% set s1 Hello\u1e85
Hello_ľ
% lmap c [split $s1 ""] {scan $c %c}
72 101 108 108 111 7813
So far so good. But $result is a list with a variable number of elements. >>> How do I iterate through this variable length list?Look up 'foreach' in the manual linked above.
#!/usr/bin/tclsh
set s1 "Hello"
puts $s1
binary scan $s1 c* result
set separator ""
puts -nonewline [format ".byte %d" [ llength $result] ]
foreach item $result {
puts -nonewline [format ",%d" $item]
}
set separator "\n"
puts [format "; .align 2"]
Christoph Kukulies <ku...@physik.rwth-aachen.de> wrote:Thanks. This is my outcome:
Christian Gollwitzer schrieb am Donnerstag, 19. Mai 2022 um 13:06:31 UTC+2:That is an easy answer when looking it up in the manual: https://tcl.tk/doc/
Am 19.05.22 um 12:49 schrieb Christoph Kukulies:Thanks. This worked out so far. Excuse my lack of knowledge of tcl.
Given I have a string in tcl:Use binary scan to get the unicode code points from your string:
set s1"Hello"
I'd like to produce the following result from that string:
.byte 5,72,101,108,108,111
(5 is the length of the string)
(Tests) 58 % binary scan Hallo c* result
1
(Tests) 59 % puts $result
72 97 108 108 111
Then you have the code points in a list and can use usual list
processing to format them into your desired output.
Christian
BTW, what is the c* ?
https://tcl.tk/man/tcl8.6/TclCmd/binary.htm:
under 'binary scan'
c
The data is turned into count 8-bit signed integers and stored in
the corresponding variable as a list. If count is *, then all of the remaining bytes in string will be scanned. If count is omitted, then
one 8-bit integer will be scanned.
Do note that is you are looking for "unicode code points" outside of
the basic ASCII Unicode set, that using binary c* will not give you the expected answer:
% set s1 Hello\u1e85
Hello_ľ
% binary scan $s1 c* result
1
% set result
72 101 108 108 111 -123
Instead you want to use %c from 'scan' to get the unicode code points
out if you have any Unicode characters that are also not ASCII
characters:
% set s1 Hello\u1e85
Hello_ľ
% lmap c [split $s1 ""] {scan $c %c}
72 101 108 108 111 7813
So far so good. But $result is a list with a variable number of elements. How do I iterate through this variable length list?Look up 'foreach' in the manual linked above.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 493 |
Nodes: | 16 (0 / 16) |
Uptime: | 169:55:49 |
Calls: | 9,703 |
Calls today: | 3 |
Files: | 13,735 |
Messages: | 6,178,352 |