How to convert a binary! to a char! in Rebol 2?

It's easy to convert a char to a binary:

>> c: #"^(52)"
== #"R"

>> type? c
== char!

>> b: to-binary c
== #{52}

But what if I want to go the other way?

>> c: to-char b
** Script Error: Invalid argument: #{52}
** Where: to-char
** Near: to char! :value

This works in Rebol 3.

Upvotes: 3

Views: 196

Answers (2)

WuJian
WuJian

Reputation: 56

Your original question asked about Rebol 3, where to-char does work on binary!

    >> c: #"^(52)"
    == #"R"

    >> type? c
    == char!

    >> b: to-binary c
    == #{52}

    >> c: to-char b
    == #"R"


    >> system/version
    == 2.101.0.3.1

However, it does not work in Rebol 2.

Upvotes: 2

earl
earl

Reputation: 41755

Convert to a string and then extract the first character from the string:

>> first to string! #{c3b6}
== #"ö"

Note that this decodes the binary as UTF-8 (the default Unicode encoding used in Rebol 3).

Upvotes: 3

Related Questions