educative.io

Educative

Storing hexadecimal and octal values in golang bytes / chars

First Question

In this lesson, the following explantation is provided:

\x is always followed by exactly two hexadecimal digits. Another possible notation is a \ followed by exactly 3 octal digits, e.g., \377.

I wrote the following code snippet:

  b1 := '\xff'
  b2 := '\377'
  fmt.Println(b1, b2)

Given the following:

  • A byte is 8 bits
  • A hexadecimal digit is 4 bits => 2 hex digits are 8 bits => a byte can store exactly 2 such digits
  • An octal digit is 3 bits => 3 octal digits are 9 bits

It just seems a little weird that a \ MUST be followed by 3 octal values, even though ‘\777’ would end up being an invalid value that cannot be stored in a single byte.

Second Question

On an unrelated note, I have another question about content on the same page.

The package unicode has some useful functions for testing characters. Suppose we have a character named ch . Following are some main functions of this package:

I tried to write a code snippet for this, but learn that the type of the value provided needs to be a rune and not a char. Am I mis-interpreting the text in the lesson?

  var ch rune = 'A'
  fmt.Println(unicode.IsDigit(ch), unicode.IsLetter(ch), unicode.IsSpace(ch))

Type your question above this line.

Course: https://www.educative.io/collection/10370001/6289391964127232
Lesson: https://www.educative.io/collection/page/10370001/6289391964127232/5136807660355584