Skip to content

Commit 309c80c

Browse files
committed
main
1 parent afd733d commit 309c80c

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed

Makefile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Define the name of the executable
2+
EXECUTABLE=dec2ascii
3+
4+
# Define the source files
5+
SRC=main.go
6+
7+
# Define the output directory
8+
OUTPUT_DIR=bin
9+
10+
# Define the installation directory
11+
INSTALL_DIR=/usr/local/bin
12+
13+
# Define the compiler
14+
GO=go
15+
16+
# Define the build flags
17+
BUILD_FLAGS=-v
18+
19+
# Define the install flags
20+
INSTALL_FLAGS=
21+
22+
# Define the clean flags
23+
CLEAN_FLAGS=
24+
25+
# Build the executable
26+
build:
27+
@echo "Building $(EXECUTABLE)..."
28+
@mkdir -p $(OUTPUT_DIR)
29+
@$(GO) build $(BUILD_FLAGS) -o $(OUTPUT_DIR)/$(EXECUTABLE) $(SRC)
30+
31+
# Install the executable
32+
install: build
33+
@echo "Installing $(EXECUTABLE)..."
34+
@mkdir -p $(INSTALL_DIR)
35+
@cp $(OUTPUT_DIR)/$(EXECUTABLE) $(INSTALL_DIR)/$(EXECUTABLE) $(INSTALL_FLAGS)
36+
37+
# Clean the build artifacts
38+
clean:
39+
@echo "Cleaning build artifacts..."
40+
@rm -rf $(OUTPUT_DIR) $(CLEAN_FLAGS)

README.MD

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# 🔢 dec2ascii 🔤
2+
3+
**dec2ascii** is a command-line tool for converting decimal numbers to ASCII text. It can read decimal numbers from the command-line arguments or from a file, and can write the output to the console or to a file. It's written in [Go](https://golang.org/) and can be compiled to run on various operating systems.
4+
5+
## 🚀 Getting Started
6+
7+
### Prerequisites
8+
9+
To compile and run **dec2ascii**, you need to have [Go](https://golang.org/) installed on your system. You can download and install Go from the official website: https://golang.org/dl/
10+
11+
## 🐹 Installation
12+
13+
1. Clone this repository:
14+
```bash
15+
git clone https://github.com/your-username/dec2ascii.git
16+
```
17+
2. Navigate to the project directory:
18+
```bash
19+
cd dec2ascii
20+
```
21+
22+
3. Compile the source code:
23+
```bash
24+
go build -o dec2ascii main.go
25+
```
26+
27+
OR
28+
29+
## 💾 Makefile
30+
31+
`dec2ascii` comes with a Makefile to simplify building and installing the program. Here are the available Makefile targets:
32+
33+
- `build` - Compiles the `dec2ascii` program.
34+
- `install` - Compiles and installs the `dec2ascii` program.
35+
- `clean` - Cleans up the build artifacts.
36+
37+
To build and install `dec2ascii`, simply run the following command:
38+
```bash
39+
sudo make install
40+
```
41+
42+
This will compile the program and install it to the system-wide `bin` directory (`/usr/local/bin` by default).
43+
44+
To customize the installation directory, modify the `INSTALL_DIR` variable in the Makefile.
45+
46+
Note that the `install` target requires root privileges, as it installs the executable to the system-wide `bin` directory.
47+
48+
49+
### Usage
50+
```bash
51+
dec2ascii [-i input-file] [-o output-file]
52+
dec2ascii decimal-numbers
53+
```
54+
Here's an overview of the command-line arguments:
55+
```bash
56+
-i input-file: specify an input file to read decimal numbers from
57+
58+
-o output-file: specify an output file to write the converted text to
59+
60+
decimal-numbers...: one or more decimal numbers to convert to ASCII text
61+
```
62+
### If the `-i` flag is not specified, the program will read decimal numbers from the command-line arguments. If the `-o` flag is not specified, the program will print the converted text to the console.
63+
64+
<br>
65+
66+
# 💡 Examples
67+
68+
Here are some examples of how to use **dec2ascii**:
69+
70+
### Convert decimal numbers from the command-line
71+
```bash
72+
dec2ascii 72 101 108 108 111 32 87 111 114 108 100
73+
```
74+
75+
76+
This will print the ASCII text equivalent of the decimal numbers to the console:
77+
```bash
78+
Hello World
79+
```
80+
81+
82+
### Convert decimal numbers from a file and write the output to a file
83+
84+
85+
This will read the decimal numbers from the `input.txt` file and write the converted text to the `output.txt` file.
86+
87+
```bash
88+
dec2ascii -i input.txt -o output.txt
89+
```
90+
This will read the decimal numbers from the `input.txt` file and write the converted text to the `output.txt` file.
91+
92+
## 📝 License
93+
94+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module dec2ascii
2+
3+
go 1.19

main.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"flag" // package for command-line flag parsing
5+
"fmt" // package for console output
6+
"os" // package for interacting with the operating system
7+
"strconv" // package for string to integer conversion
8+
"strings" // package for string manipulation
9+
)
10+
11+
func main() {
12+
// define command-line flags
13+
inputFile := flag.String("i", "", "input file")
14+
outputFile := flag.String("o", "", "output file")
15+
flag.Parse()
16+
17+
var decimalNumbers []string
18+
19+
if *inputFile != "" {
20+
// read decimal numbers from input file
21+
fileContent, err := os.ReadFile(*inputFile)
22+
if err != nil {
23+
// print error message to stderr and exit with non-zero status code
24+
fmt.Fprintf(os.Stderr, "error reading file: %v\n", err)
25+
os.Exit(1)
26+
}
27+
// split the file content into whitespace-separated fields (i.e., decimal numbers)
28+
decimalNumbers = strings.Fields(string(fileContent))
29+
} else {
30+
// read decimal numbers from command-line arguments
31+
decimalNumbers = flag.Args()
32+
}
33+
34+
// convert decimal numbers to ASCII text
35+
var asciiText string
36+
for _, decimalStr := range decimalNumbers {
37+
// convert the decimal number string to an integer
38+
decimal, err := strconv.Atoi(decimalStr)
39+
if err != nil {
40+
// print error message to stderr and skip the invalid number
41+
fmt.Fprintf(os.Stderr, "invalid decimal number: %s\n", decimalStr)
42+
continue
43+
}
44+
// convert the decimal number to its ASCII text equivalent and append it to the output string
45+
asciiText += string(decimal)
46+
}
47+
48+
if *outputFile != "" {
49+
// write the output string to the specified output file
50+
err := os.WriteFile(*outputFile, []byte(asciiText), 0644)
51+
if err != nil {
52+
// print error message to stderr and exit with non-zero status code
53+
fmt.Fprintf(os.Stderr, "error writing file: %v\n", err)
54+
os.Exit(1)
55+
}
56+
} else {
57+
// print the output string to the console
58+
fmt.Println(asciiText)
59+
}
60+
}

0 commit comments

Comments
 (0)