Wednesday, April 23, 2025
HomeGolangConfused about golang byte characters and code factors - Getting Assist

Confused about golang byte characters and code factors – Getting Assist


hi there everybody
i need assistance concerning the next code snippet
var char byte
var codepoint rune
char = ‘é’
codepoint=‘é’
fmt.Println(string(char) , string(codepoint))

this code prints out ‘é’ two occasions , when I’m anticipating it to throw an error , as a result of I’m making an attempt to assign a multibyte character to a byte kind “char” , however it’s working nice.
is my psychological mannequin incorrect or what ?
whats the catch right here ?

As a result of é appears to have the ordinal 233 which inserts a byte with out issues.

The “lengthy s” (ſ) will trigger a compilation error as its ordinal 383 doesn’t match the byte:

thanks for the reply mate however what I’m not understanding is that this that why is it 233
e.g
see the next code snippet
// On-line Go compiler to run Golang program on-line
// Print “Strive programiz.professional” message

package deal foremost
import “fmt”

func foremost() {
str:=“héllo”
fmt.Println(byte(str))
//outputs : [104 195 169 108 108 111]
var char byte
char=‘é’
fmt.Println(char)
//outputs : 233
}
within the first output the letter ’ é’ will get transformed to 2 bytes “195 and 169” however in second occasion it will get transformed to 233 why is that ?

string is the set of all strings of 8-bit bytes, conventionally however not essentially representing UTF-8-encoded textual content. A string could also be empty, however not nil. Values of string kind are immutable.

U+00E9
195 169 is utf-8 (want 2 byte)(C3A9)
233 is uncooked utf-8 (want 1 int32) (E9)

@danish: A number one little bit of 1 in UTF-8 means that there’s a following byte, so any code level larger than 0x7f should be encoded as two (or extra) bytes. UTF-8 – Wikipedia

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments