DEV Community

Cover image for HEX 2 DEC Converter
Łukasz Wójcik
Łukasz Wójcik

Posted on

HEX 2 DEC Converter

Hex2Dec is a simple yet powerful hexadecimal (HEX) to decimal (DEC) converter.

$$\   $$\ $$$$$$$$\ $$\   $$\  $$$$$$\  $$$$$$$\  $$$$$$$$\  $$$$$$\  
$$ |  $$ |$$  _____|$$ |  $$ |$$  __$$\ $$  __$$\ $$  _____|$$  __$$\ 
$$ |  $$ |$$ |      \$$\ $$  |\__/  $$ |$$ |  $$ |$$ |      $$ /  \__|
$$$$$$$$ |$$$$$\     \$$$$  /  $$$$$$  |$$ |  $$ |$$$$$\    $$ |      
$$  __$$ |$$  __|    $$  $$<  $$  ____/ $$ |  $$ |$$  __|   $$ |      
$$ |  $$ |$$ |      $$  /\$$\ $$ |      $$ |  $$ |$$ |      $$ |  $$\ 
$$ |  $$ |$$$$$$$$\ $$ /  $$ |$$$$$$$$\ $$$$$$$  |$$$$$$$$\ \$$$$$$  |
\__|  \__|\________|\__|  \__|\________|\_______/ \________| \______/ 
Enter fullscreen mode Exit fullscreen mode

🛠️ HEX to DEC Converter

📚 Contents

🚀 Application

HEX to DEC Converter is a simple but powerful command line tool written in Golang that converts hexadecimal (HEX) values to decimal values ​​(DEC). Supports both single HEX values ​​and batch processing from a file. The tool is intended for programmers, engineers and anyone working with numerical systems.

🔑 Main features:

  • ✅ Convert single HEX values ​​to DEC.
  • ✅ Process multiple HEX values ​​from a file.
  • ✅ Saving results to an output file.
  • ✅ Easy-to-use command line interface.
  • ✅ Lightweight and fast.
  • ✅ Runs in the console (CMD/CLI).

💡 Use

🤖 Automation:

The program eliminates the need to manually convert HEX values ​​to DEC, which saves time and reduces the risk of errors.

🎛️ Flexibility:

Supports both single values ​​and files with multiple HEX values.

🛠️ Simplicity:

Ease of use with a clean text interface and flag support.

🔒 Security:

Includes error handling for invalid input.


📥 Installation

Prerequisites

  • Go (Golang) installed on your system. You can download it here.

Steps

  1. Clone the repository:
git clone https://github.com/lukaszwojcikdev/hex2dec_converter.git
Enter fullscreen mode Exit fullscreen mode
  1. Go to the project directory:
cd hex2dec_converter
Enter fullscreen mode Exit fullscreen mode
  1. Build the project:
go build -o hex2dec_converter
Enter fullscreen mode Exit fullscreen mode

or

go build hex2dec_converter.go
Enter fullscreen mode Exit fullscreen mode
  1. Run the executable:
./hex2dec_converter
Enter fullscreen mode Exit fullscreen mode

🛠️ Usage

Convert a single HEX value

To convert a single HEX value to DEC, use -dh flag:

./hex2dec_converter -dh 583DFD
Enter fullscreen mode Exit fullscreen mode

Output:

5787133
Enter fullscreen mode Exit fullscreen mode

Convert HEX values ​​from file

To convert multiple HEX values ​​from file, use the -dhf flag:

./hex2dec_converter -dhf input.txt
Enter fullscreen mode Exit fullscreen mode
  • Input file format: HEX values ​​should be separated by commas (,), e.g. 108AB2,1BBBFE,6386DE.
  • Output: The results will be written to a file named input_dec.txt.

Show help

To show the help menu, use the -h flag:

./hex2dec_converter -h
Enter fullscreen mode Exit fullscreen mode

Show program information

To show program information, use the -i flag:

./hex2dec_converter -i
Enter fullscreen mode Exit fullscreen mode

📄 Documentation

Flags:

-h - Show help.
-i - Show program information.
-dh - Convert a single HEX value to DEC.
-dhf - Convert HEX values ​​from a file to DEC and save the result to a new file.
Enter fullscreen mode Exit fullscreen mode

Input file format:
HEX values ​​in the file should be separated by commas (,) without spaces.

Example:

108AB2,1BBBFE,6386DE
Enter fullscreen mode Exit fullscreen mode

For detailed documentation, visit the official project website:
https://lukaszwojcik.eu/hex2dec.html


🛠️ Tools

During the development of HEX2DEC and the creation of this README.md file, I used the following tools and software:

  • IrfanView - Used to resize images.
  • TinyPNG - Used to compress images to reduce file size without losing quality.
  • Notepad++ - Code editor used to write the project.
  • Go (Golang) - The basic programming language used to develop the HEX2DEC converter.
  • Carbon - Used to create beautiful source code syntax as a PNG image.
  • GitHub - Used for version control and hosting the project repository.
  • MD5 - Used to generate an MD5 hash file.
  • ASCII Generator - Used to create an ASCII logo for the project.
  • Shields.io - Used to generate badges for the README.md file (e.g. version, license, etc.).
  • Markdown Online - Used to write and format the README.md file.
  • Unicode Icons - Unicode icon set used for ## or ### paragraphs.
  • Stable Diffusion - Used to generate the cover image.
  • 🎵 Music by ZHU - Relaxing music listened to while designing Hex2Dec.

🐛 Bugs

If you encounter any bugs or issues, please report them here


👨‍ Author


‍💻 Source code

packagemain

import (
 "flag"
 "fmt"
 "log"
 "axis"
 "strconv"
 "strings"
)

func hexToDec(hex string)(string, error) {
 dec, err := strconv.ParseInt(hex, 16, 64)
 if err != nil {
 return "", fmt.Erorf("Invalid HEX value: %s", hex)
 }
 return strconv.FormatInt(dec, 10), nil
}

func hexToDecFile(fileName string) error {
 data, err := os.ReadFile(fileName)
 if err != nil {
 return fmt.Errorf("failed to read file: %w", err)
 }
 hexValues ​​:= strings.Split(strings.TrimSpace(string(data)), ","")
 var decValues ​​[]string
 for _, hex := range hexValues ​​{
 dec, err := hexToDec(hex)
 if err != nil {
 return fmt.Errorf("conversion error: %w", err)
 }
 decValues ​​= append(decValues, dec)
 }
 outputFileName := strings.TrimSuffix(fileName, ".txt") + "_dec.txt"
 outputData := []byte(strings.Join(decValues, ","))
 err = os.WriteFile(outputFileName, outputData, 0644)
 if err != nil {
 return fmt.Errorf("failed to write file: %w", err)
 }
 return nil
}

func printHelp() {
 fmt.Println("HEX to DEC Converter v1.0c")
 fmt.Println("░█░█░█▀▀░█░█░▀▀▄░█▀▄░█▀▀░█▀▀")
 fmt.Println("░█▀█░█▀▀░▄▀▄░▄▀░░█░█░█▀▀░█░░")
 fmt.Println("░▀░▀░▀▀▀░▀░▀░▀▀▀░▀▀░░▀▀▀░▀▀▀")
 fmt.Println("")
 fmt.Println("(c) by Lukasz Wojcik 2024, 2025")
 fmt.Println("Use: hex2dec [flag] [argument]")
 fmt.Println("")
 fmt.Println("Flags:")
 fmt.Println(" -h Display help")
 fmt.Println(" -i Displays program information")
 fmt.Println(" -dh Converts a HEX value to DEC ")
 fmt.Println(" -dhf Converts HEX values ​​from file and writes DEC to file")
 fmt.Println("")
 fmt.Println("Examples of use:")
 fmt.Println(" hex2dec -dh 583DFD")
 fmt.Println(" hex2dec -dhf input.txt")
 fmt.Println("")
 fmt.Println("HEX values ​​in the file should be separated by a comma (,) with no 'space'.")
 fmt.Println("Example of the HEX system arrangement in the 'inside.txt' file:")
 fmt.Println("108AB2,1BBBFE,6386DE,...,...")
}

func printProgramInfo() {
 fmt.Println("Hex2Dec - HEX to DEC Converter")
 fmt.Println("Author: Lukasz Wojcik")
 fmt.Println("Version: 1.0c")
 fmt.Println("Year: 2025")
 fmt.Println("Site: https://lukaszwojcik.eu/hex2dec.html")
 fmt.Println("Source Code: https://github.com/lukaszwojcikdev/hex2dec_converter.git")
}

func main() {
 convertHex := flag.String("dh", "", "Converts HEX value to DEC")
 help := flag.Bool("h", false, "Displays help")
 info := flag.Bool("i", false, "Displays information about the program")
 convertHexFile := flag.String("dhf", "", "Converts HEX values ​​from a file and writes DEC to the file")
 flag.Parse()

 if *help {
 printHelp()
 return
 }

 if *info {
 printProgramInfo()
 return
 }

 if *convertHex != "" {
 dec, err := hexToDec(*convertHex)
 if err != nil {
 log.Fatal(err)
 }
 fmt.Println(dec)
 return
 }

 if *convertHexFile != "" {
 err := hexToDecFile(*convertHexFile)
 if err != nil {
 log.Fatal(err)
 }
 fmt.Println("Conversion completed successfully. Results saved to file: input_dec.txt")
 return
 }
 printHelp()
}
Enter fullscreen mode Exit fullscreen mode

📝 Description

  1. hexToDec Function converts a single HEX value to DEC.
func hexToDec(hex string) (string, error) {
dec, err := strconv.ParseInt(hex, 16, 64)
if err != nil {
return "", fmt.Errorf("Invalid HEX value: %s", hex)
}
return strconv.FormatInt(dec, 10), nil
}
Enter fullscreen mode Exit fullscreen mode

Parameters:

hex: a string representing a hexadecimal number.

Returns:
A decimal value as a string.
Error if the HEX value is invalid.

Action:
Uses strconv.ParseInt function to convert HEX to DEC.

If an error occurs, it returns an appropriate message.

  1. hexToDecFile The function converts HEX values ​​stored in a file to DEC values ​​and writes the result to a new file.
func hexToDecFile(fileName string) error {
 data, err := os.ReadFile(fileName)
 if err != nil {
 return fmt.Errorf("failed to read file: %w", err)
 }
 hexValues ​​:= strings.Split(strings.TrimSpace(string(data)), ","")
 var decValues ​​[]string
 for _, hex := range hexValues ​​{
 dec, err := hexToDec(hex)
 if err != nil {
 return fmt.Errorf("conversion error: %w", err)
 }
 decValues ​​= append(decValues, dec)
 }
 outputFileName := strings.TrimSuffix(fileName, ".txt") + "_dec.txt"
outputData := []byte(strings.Join(decValues, ","))
err = os.WriteFile(outputFileName, outputData, 0644)
if err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
return nil
}
Enter fullscreen mode Exit fullscreen mode

Parameters:
fileName: the name of the input file containing HEX values, separated by commas.

Returns:
error: error if the read, convert or write operation fails.

Action:
Reads the contents of the file.
Separates the HEX values ​​by commas.
Converts any HEX value to DEC using the hexToDec function.
Saves the results to a new file named filename_dec.txt.

  1. printHelp The function displays help for the user.
func printHelp() {
fmt.Println("HEX to DEC Converter v1.0c")
...
}
Enter fullscreen mode Exit fullscreen mode

What it does:
Displays detailed information about available flags, arguments, and examples of program usage. It tells you what the input data in the file should look like.

  1. printProgramInfo The function displays information about the program.
func printProgramInfo() {
fmt.Println("Hex2Dec - HEX to DEC Converter")
...
}
Enter fullscreen mode Exit fullscreen mode

Action:
Displays the program name, author, version, year of release, and links to the source code page and repository.

  1. main The main function of the program, handling control logic based on flags passed by the user.
func main() {
 convertHex := flag.String("dh", "", "Converts HEX value to DEC")
 help := flag.Bool("h", false, "Displays help")
 info := flag.Bool("i", false, "Displays information about the program")
 convertHexFile := flag.String("dhf", "", "Converts HEX values ​​from a file and writes DEC to the file")
 flag.Parse()

 if *help {
 printHelp()
 return
 }

 if *info {
 printProgramInfo()
 return
 }

 if *convertHex != "" {
 dec, err := hexToDec(*convertHex)
 if err != nil {
 log.Fatal(err)
 }
 fmt.Println(dec)
 return
 }

 if *convertHexFile != "" {
 err := hexToDecFile(*convertHexFile)
 if err != nil {
 log.Fatal(err)
 }
 fmt.Println("Conversion completed successfully. Results saved to file: input_dec.txt")
 return
 }
 printHelp()
}
Enter fullscreen mode Exit fullscreen mode

Operation:

Defines flags:

-dh: converts a single HEX value to DEC.

-h: prints help (printHelp).

-i: prints program information (printProgramInfo).

-dhf: converts HEX values ​​from a file to DEC and writes them to a file.

Based on the given flags, performs the appropriate operations:

➡️Displays help or program information.
➡️Converts a single HEX value to DEC.
➡️Processes a file with HEX values.

If no flag is given, prints help.


📝 Summary

HEX to DEC Converter is a versatile tool for converting hexadecimal values ​​to decimal values. It is designed to be simple, powerful and easy to use, making it ideal for programmers, engineers and students working with number systems.


⚠️ Disclaimer

By using this software, you agree to these terms. The software is provided “as is” and you use it at your own risk. The author is not responsible for any damages or problems caused by using this software.


📥 Download

You can download the latest version of the project HERE 👇


📜 License

This project is licensed under the MIT License


Enjoy using the HEX to DEC converter! 🚀

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay