• Re: print character array (string) as bytes

    From Christian Gollwitzer@21:1/5 to All on Thu May 19 13:06:26 2022
    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.

    Christian

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christoph Kukulies@21:1/5 to All on Thu May 19 03:49:04 2022
    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)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christoph Kukulies@21:1/5 to Christoph Kukulies on Thu May 19 05:59:46 2022
    Christoph Kukulies schrieb am Donnerstag, 19. Mai 2022 um 14:23:21 UTC+2:
    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.

    Christian
    Thanks. 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] ]

    Correction: [ llength $result]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christoph Kukulies@21:1/5 to Christian Gollwitzer on Thu May 19 05:23:18 2022
    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.

    Christian
    Thanks. 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] ]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to Christoph Kukulies on Thu May 19 13:16:48 2022
    Christoph Kukulies <kuku@physik.rwth-aachen.de> wrote:
    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.

    Christian
    Thanks. This worked out so far. Excuse my lack of knowledge of tcl.
    BTW, what is the c* ?

    That is an easy answer when looking it up in the manual: https://tcl.tk/doc/

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Gollwitzer@21:1/5 to All on Thu May 19 16:00:47 2022
    Am 19.05.22 um 15:24 schrieb Christoph Kukulies:
    Rich schrieb am Donnerstag, 19. Mai 2022 um 15:16:53 UTC+2:
    Christoph Kukulies <ku...@physik.rwth-aachen.de> wrote:
    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.

    Christian
    Thanks. This worked out so far. Excuse my lack of knowledge of tcl.
    BTW, what is the c* ?
    That is an easy answer when looking it up in the manual: https://tcl.tk/doc/ >>
    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.
    Thanks. This is my outcome:

    #!/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"]

    That works. Another easy wy to turn a list into a comma-separated list
    is "join":


    (Tests) 59 % set result "1 2 3 4"
    1 2 3 4
    (Tests) 60 % join $result ,
    1,2,3,4

    Also, you should read the response from Rich as well depending on the
    data you feed (if it is not 7-bit ASCII).

    (Tests) 66 % binary scan Äü c* result; puts $result
    -60 -4

    vs.

    (Tests) 67 % binary scan Äü cu* result; puts $result
    196 252

    and

    (Tests) 71 % binary scan ζ cu* result; puts $result
    182

    (the greek zeta is at 0x03B6 and this method only converts the lowest 8
    bits)

    Christian

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christoph Kukulies@21:1/5 to Rich on Thu May 19 06:24:15 2022
    Rich schrieb am Donnerstag, 19. Mai 2022 um 15:16:53 UTC+2:
    Christoph Kukulies <ku...@physik.rwth-aachen.de> wrote:
    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.

    Christian
    Thanks. This worked out so far. Excuse my lack of knowledge of tcl.
    BTW, what is the c* ?
    That is an easy answer when looking it up in the manual: https://tcl.tk/doc/

    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.
    Thanks. This is my outcome:

    #!/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"]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)