Golang strconv.FormatUint Function

The strconv.FormatUint function in Golang is part of the strconv package and is used to convert an unsigned integer value into its string representation in a specified numerical base. This function is particularly useful when you need to format unsigned integers as strings for output, logging, or data serialization, with control over the numerical base (e.g., binary, octal, decimal, hexadecimal).

Table of Contents

  1. Introduction
  2. strconv.FormatUint Function Syntax
  3. Examples
    • Basic Usage
    • Formatting with Different Bases
    • Combining with Other Formatting Functions
  4. Real-World Use Case Example
  5. Conclusion

Introduction

The strconv.FormatUint function allows you to convert unsigned integers into their string representations with control over the numerical base. This is especially useful in situations where you need to display or store unsigned integer values in different bases, such as binary, octal, decimal, or hexadecimal.

strconv.FormatUint Function Syntax

The syntax for the strconv.FormatUint function is as follows:

func FormatUint(i uint64, base int) string

Parameters:

  • i uint64: The unsigned integer value to be formatted.
  • base int: The numerical base (2 to 36) used to format the unsigned integer as a string.

Returns:

  • string: The string representation of the unsigned integer in the specified base.

Behavior:

  • Converts the unsigned integer to a string: The function converts the unsigned integer value to its string representation in the specified base, ranging from binary (base 2) to hexadecimal (base 16) and beyond.

Examples

Basic Usage

This example demonstrates how to use strconv.FormatUint to convert an unsigned integer to its string representation in base 10.

Example

package main

import (
	"fmt"
	"strconv"
)

func main() {
	num := uint64(12345)
	str := strconv.FormatUint(num, 10)
	fmt.Println("Formatted unsigned integer:", str)
}

Output:

Formatted unsigned integer: 12345

Explanation:

  • The strconv.FormatUint function converts the unsigned integer 12345 into the string "12345" in base 10.

Formatting with Different Bases

This example shows how to use strconv.FormatUint to format unsigned integers in different numerical bases.

Example

package main

import (
	"fmt"
	"strconv"
)

func main() {
	num := uint64(255)

	binaryStr := strconv.FormatUint(num, 2)  // Base 2 (binary)
	octalStr := strconv.FormatUint(num, 8)   // Base 8 (octal)
	hexStr := strconv.FormatUint(num, 16)    // Base 16 (hexadecimal)

	fmt.Println("Binary:", binaryStr)
	fmt.Println("Octal:", octalStr)
	fmt.Println("Hexadecimal:", hexStr)
}

Output:

Binary: 11111111
Octal: 377
Hexadecimal: ff

Explanation:

  • The strconv.FormatUint function converts the unsigned integer 255 into its string representation in binary, octal, and hexadecimal formats.

Combining with Other Formatting Functions

This example demonstrates how to combine strconv.FormatUint with other formatting functions to create complex outputs.

Example

package main

import (
	"fmt"
	"strconv"
)

func main() {
	id := uint64(42)
	price := float64(19.99)

	idStr := strconv.FormatUint(id, 10)
	priceStr := strconv.FormatFloat(price, 'f', 2, 64)

	output := "ID: " + idStr + ", Price: $" + priceStr
	fmt.Println(output)
}

Output:

ID: 42, Price: $19.99

Explanation:

  • The strconv.FormatUint function is used to convert the unsigned integer 42 to a string, and strconv.FormatFloat formats the floating-point number 19.99. The result is a formatted string that combines both values.

Real-World Use Case Example: Logging Network Packet Sizes

A practical use case for strconv.FormatUint is logging network packet sizes or other metrics that are represented as unsigned integers.

Example: Logging Packet Sizes

package main

import (
	"fmt"
	"strconv"
)

func main() {
	packetSize := uint64(512)
	logMessage := "Packet size: " + strconv.FormatUint(packetSize, 10) + " bytes."
	fmt.Println(logMessage)
}

Output:

Packet size: 512 bytes.

Explanation:

  • The strconv.FormatUint function is used to convert the unsigned integer representing the packet size into a string, which is then included in a log message.

Conclusion

The strconv.FormatUint function in Go is used for converting unsigned integers into strings with control over the numerical base. It is particularly useful in scenarios where you need to format unsigned integers in different bases, such as binary, octal, decimal, or hexadecimal. Whether you’re logging metrics, preparing data for output, or working with numbers in different bases, strconv.FormatUint provides a straightforward way to handle unsigned integer formatting in your Go applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top