How to represent a hex string in Red/System? - red-system

How would one represent a literal string of binary data in hex in Red/System?
It's not possible to do this:
blah: #{F0909090F02060202070F010F080F0F010F010F09090F01010F080F010F0F080F090F0F010204040F090F090F0F090F010F0F090F09090E090E090E0F0808080F0E0909090E0F080F080F0F080F08080}

For now, using a string, one can encode literal bytes with escape notation "^(00)":
blah: "^(F0)^(90)^(90)^(90)^(F0)^(20)^(60)^(20)^(20)^(70)^(F0)^(10)^(F0)^(80)^(F0)^(F0)^(10)^(F0)^(10)^(F0)^(90)^(90)^(F0)^(10)^(10)^(F0)^(80)^(F0)^(10)^(F0)^(F0)^(80)^(F0)^(90)^(F0)^(F0)^(10)^(20)^(40)^(40)^(F0)^(90)^(F0)^(90)^(F0)^(F0)^(90)^(F0)^(10)^(F0)^(F0)^(90)^(F0)^(90)^(90)^(E0)^(90)^(E0)^(90)^(E0)^(F0)^(80)^(80)^(80)^(F0)^(E0)^(90)^(90)^(90)^(E0)^(F0)^(80)^(F0)^(80)^(F0)^(F0)^(80)^(F0)^(80)^(80)"

Related

Why does typecasting a single byte to string not work in go?

I am trying to convert a single byte value to a string in golang. When I do a typecast of a byte to string like string(byte) and print the value I get "{" in response. I read other answers that the correct way to convert a byte to string would be strconv.Itoa(int(bytevalue)). Why does the former not work and why is the latter approach correct.
The expression string(bytevalue) is a conversion, not a typecast.
The specification says this about conversions from numeric types to a string:
Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer.
The expression string(byte(123)) evaluates to the string "{" because { is the the string containing the UTF-8 representation of the rune 123.
Use the strconv package to get the decimal representation of the byte. The expression strconv.Itoa(int(byte(123))) evaluates to the string "123".

Go rune literal for high positioned emojis

How do we use an emoji with a rune literal that is beyond I think
code point U+265F?
a1 := '\u2665'
this works
a2 := '\u1F3A8'
this gives error invalid character literal, more that one character.
Is there a way to represent higher positioned emojis as rune literals?
https://unicode.org/emoji/charts/full-emoji-list.html
You may use the \U sequence followed by 8 hex digits which is the hexadecimal representation of the Unicode codepoint. This is detailed in Spec: Rune literals:
There are four ways to represent the integer value as a numeric constant: \x followed by exactly two hexadecimal digits; \u followed by exactly four hexadecimal digits; \U followed by exactly eight hexadecimal digits, and a plain backslash \ followed by exactly three octal digits. In each case the value of the literal is the value represented by the digits in the corresponding base.
For example:
a1 := '\u2665'
fmt.Printf("%c\n", a1)
a2 := '\U0001F3A8'
fmt.Printf("%c\n", a2)
Which outputs (try it on the Go Playground):
♥
🎨
Note (response to #torek):
I believe the Go authors chose to require exactly 4 and 8 hex digits because this allows to use the exact same form, the exact same rune literals inside interpreted string literals. E.g. if you want a string that contains 2 runes, one having code point 0x0001F3A8 and another rune being 4, it could look like this:
s := "\U0001F3A84"
If the spec would not require exactly 8 hex digits, it would be ambiguous whether the last '4' is part of the code point or is an individual rune of the string, so you would have to break the string to a concatenation like "\U1F3A8" + "4".
Spec: String literals:
Interpreted string literals are character sequences between double quotes, as in "bar". Within the quotes, any character may appear except newline and unescaped double quote. The text between the quotes forms the value of the literal, with backslash escapes interpreted as they are in rune literals (except that \' is illegal and \" is legal), with the same restrictions. The three-digit octal (\nnn) and two-digit hexadecimal (\xnn) escapes represent individual bytes of the resulting string; all other escapes represent the (possibly multi-byte) UTF-8 encoding of individual characters. Thus inside a string literal \377 and \xFF represent a single byte of value 0xFF=255, while ÿ, \u00FF, \U000000FF and \xc3\xbf represent the two bytes 0xc3 0xbf of the UTF-8 encoding of character U+00FF.

Understanding string escape sequences

I am new to go, so lot of confusion regarding bytes concept.
While going through some go code, I came across some thing like
[]byte("\xd2\xfd\x88g\xd5\r-\xfe")
was it in hexa decimal or bytes format?
what are some chars in above like g,r-,e signifies?
And how to print it in log?
[]byte("\xd2\xfd\x88g\xd5\r-\xfe") is an interpreted string literal converted to type []byte, a byte slice. Here it is separated into byte values:
[\xd2, \xfd, \x88, g, \xd5, \r, -, \xfe]
or, expressed as hexadecimal bytes,
[d2, fd, 88, 67, d5, 0d, 2d, fe]
One way to log the value,
package main
import "log"
func main() {
b := []byte("\xd2\xfd\x88g\xd5\r-\xfe")
log.Printf("%q\n", b)
}
Playground: https://play.golang.org/p/BIh_EuvoxU-
Output:
2009/11/10 23:00:00 "\xd2\xfd\x88g\xd5\r-\xfe"
The Go Programming Language Specification
String literals
A string literal represents a string constant obtained from
concatenating a sequence of characters. There are two forms: raw
string literals and interpreted string literals.
Raw string literals are character sequences between back quotes, as in
foo. Within the quotes, any character may appear except back quote.
The value of a raw string literal is the string composed of the
uninterpreted (implicitly UTF-8-encoded) characters between the
quotes; in particular, backslashes have no special meaning and the
string may contain newlines. Carriage return characters ('\r') inside
raw string literals are discarded from the raw string value.
Interpreted string literals are character sequences between double
quotes, as in "bar". Within the quotes, any character may appear
except newline and unescaped double quote. The text between the quotes
forms the value of the literal, with backslash escapes interpreted as
they are in rune literals (except that \' is illegal and \" is legal),
with the same restrictions. The three-digit octal (\nnn) and two-digit
hexadecimal (\xnn) escapes represent individual bytes of the resulting
string; all other escapes represent the (possibly multi-byte) UTF-8
encoding of individual characters. Thus inside a string literal \377
and \xFF represent a single byte of value 0xFF=255, while ÿ, \u00FF,
\U000000FF and \xc3\xbf represent the two bytes 0xc3 0xbf of the UTF-8
encoding of character U+00FF.
After a backslash, certain single-character escapes represent special
values:
\a U+0007 alert or bell
\b U+0008 backspace
\f U+000C form feed
\n U+000A line feed or newline
\r U+000D carriage return
\t U+0009 horizontal tab
\v U+000b vertical tab
\\ U+005c backslash
\' U+0027 single quote (valid escape only within rune literals)
\" U+0022 double quote (valid escape only within string literals)

Get string with base-16 (hex) rendering of the bytes of an ASCII string

E.g.
input := "Office"
want := "4f6666696365" // Note: this is a string!!
I know that string literals are stored in UTF-8 already.
What is the easiest way to get convert this to string in UTF-8 representation?
Calling EncodeRune on each character seems too cumbersome.
What you're looking for is a string that contains the hex representation of your input string. That is not UTF-8. (Any string that's valid ASCII is also valid UTF-8.)
In any case, this is how to do what you want:
want := fmt.Sprintf("%x", []byte(input))

How encode sequence of bytes into ruby string with characters

How encode sequence of bytes from ruby string into ruby string human-readable characters?
This is input string:
"\x127\x00\x06\x00\x00\x00\x01\x00\xA2\x8F"
So how parse this string into array with bytes,
and encode every element from array to ASCII character?
P.S. However, I can't find a way to roundtrip from bytes back to an array. I tried to use Array.pack with the U* option, but that doesn't work for multibyte characters.
You can try something like:
"string\xaa".each_byte.map {|b| "%c(%x)" % [ b, b ] }.join( ' ' )
# => "s(73) t(74) r(72) i(69) n(6e) g(67) ª(aa)"

Resources