Golang: Print String as Byte, Char, Integer

By Xah Lee. Date: . Last updated: .

Print One Byte in String

When you use a string index, e.g. "abc"[0], the value is a Byte.

You may print it as Hexadecimal , or integer (Unicode Codepoint), or as a char.

Print as Hexadecimal

fmt.Printf("%#v", str[n])

Sample output: 0x61

Print as Integer

fmt.Printf("%d", str[n])

Sample output: 97

Print as Raw Byte

fmt.Printf("%q", str[n])

Sample output: 'a'.

(May display as gibberish if the byte is a control character or non-printable ASCII Character.)

package main

import "fmt"

// ways to print 1 byte in string

func main() {

	var x = "abc"

	// print as hexadecimal
	fmt.Printf("%#v\n", x[0])
	// 0x61

	// print as int
	fmt.Printf("%d\n", x[0])
	// 97

	// print as byte
	fmt.Printf("%q\n", x[0])
	// 'a'

}

🛑 WARNING: a index in string may not be a valid character.

package main

import "fmt"

// a index in string may not be a valid character

func main() {

	// utf8 encoding for FACE WITH TEARS OF JOY char is 4 bytes: F0 9F 98 82
	var y = "😂"

	// print 1st byte
	fmt.Printf("%#v\n", y[0]) // 0xf0
	fmt.Printf("%d\n", y[0])  // 240
	fmt.Printf("%q\n", y[0])  // 'ð'

}

Print Whole String

Because string is Byte sequence, you may want to print them in different ways.

Say you have a string like this: "♥\t😂", unicode heart followed by invisible Horizontal Tab (codepoint 9) followed by a emoji.

you may:

The fmt.Printf function has several placeholders to help.

Print String as raw bytes

%s

🛑 WARNING: If string contain ASCII Control Characters or byte sequence that is not a valid character, this can cause problem in the output terminal.

Print String as string syntax

%q

Output in golang string syntax, using backslash escape sequence for non-printable characters. (e.g. "♥\t😂")

Print String as String Syntax, Escape All Non-Ascii Chars

%+q

Output in golang string syntax, using backslash escape for anything that's not printable ASCII. (e.g. "\u2665\t\U0001f602")

Print String as Hexadecimals

% x

Hexadecimal, with space between each 2 digits. (e.g. e2 99 a5 09 f0 9f 98 82) This is best if you want to know the byte values.

〔see ASCII Characters〕

package main

import "fmt"

func main() {
	var x = "♥\t😂" // with a tab (U+0009) in middle

	// print as raw bytes
	fmt.Printf("%s\n", x)
	// ♥ 😂

	// print in string syntax
	fmt.Printf("%q\n", x)
	// "♥\t😂"

	// print in string syntax, escape all
	fmt.Printf("%+q\n", x)
	// "\u2665\t\U0001f602"

	// print in hexadecimal
	fmt.Printf("% x\n", x)
	// e2 99 a5 09 f0 9f 98 82

}

Print String in Unicode Notation

To print string in unicode notation, e.g. U+2665, first convert the string into Rune Slice , then print it with %U

package main

import "fmt"

func main() {
	// with a tab (U+0009) in middle
	var x = "♥\t😂"

	// turn the string into rune slice, then print it with %U
	fmt.Printf("%U\n", []rune(x)) // [U+2665 U+0009 U+1F602]
}

Golang, Print

Golang, String