diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..15adf93 --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +all: asciidencoder + +WARNINGS = -Wall +DEBUG = -ggdb -fno-omit-frame-pointer +OPTIMIZE = -O2 +OPTS = -lm + +asciidencoder: Makefile asciidencoder.c + $(CC) -o $@ $(WARNINGS) $(DEBUG) $(OPTIMIZE) $(OPTS) asciidencoder.c + +clean: + rm -f asciidencoder + +install: + echo "Installing is not supported" + +run: + ./asciidencoder + diff --git a/README.md b/README.md new file mode 100644 index 0000000..61de903 --- /dev/null +++ b/README.md @@ -0,0 +1,103 @@ +# asciidencoder + +## Table of Contents + +- [Introduction](#introduction) +- [Building](#building) +- [Usage](#usage) +- [Options](#options) +- [Examples](#examples) +- [Notes](#notes) + +## Introduction + +**ascii-dencoder** is a command-line utility designed to convert strings into their corresponding ASCII codes and vice versa: from ASCII codes to characters. This tool is particularly useful for developers, students, and hobbyists involved in fields requiring ASCII manipulation or understanding. + +## Building + +This tool is provided as source code and can be built using `make`. By just +typing make in the terminal within the code's directory: + +```bash +make +``` + +You should now have an executable named `ascii-dencoder`. Optionally, you can +copy it to a location in your PATH for easier access. + +## Usage + +To use `ascii-dencoder`, run the executable from the command line with the +appropriate options. + +```bash +./ascii-dencoder [OPTIONS] +``` + +## Options + +**ascii-dencoder** accepts the following options: + +- `-s`, `--string `: + - Convert the input string to its ASCII codes. + +- `-c`, `--code `: + - Convert the input ASCII code to its corresponding character. + +- `-h`, `--help`: + - Display the help message and exit. + +- `-v`, `--version`: + - Display the program version and exit. + +## Examples + +1. Convert a string to ASCII codes: + +```bash +./ascii-dencoder -s "hello" +``` + +Output: + +``` +The ASCII code of the character 'h' is 104 +The ASCII code of the character 'e' is 101 +The ASCII code of the character 'l' is 108 +The ASCII code of the character 'l' is 108 +The ASCII code of the character 'o' is 111 +``` + +2. Convert an ASCII code to a character: + +```bash +./ascii-dencoder -c 104 +``` + +Output: +``` +The ASCII character for code '104' is 'h' +``` + +3. Display help message: + +```bash +./ascii-dencoder -h +``` + +This will display detailed usage instructions and available options. + +4. Display version: + +```bash +./ascii-dencoder -v +``` + +This will show the current version of the program. + +## Notes + +- The ASCII code conversion accepts numbers between 0 and 255. +- Ensure proper usage of command-line arguments to avoid unexpected behavior. +- This tool is straightforward and does not handle complex text encoding + scenarios beyond basic ASCII. diff --git a/asciidencoder.c b/asciidencoder.c new file mode 100644 index 0000000..8c58ecd --- /dev/null +++ b/asciidencoder.c @@ -0,0 +1,132 @@ +/* + * asciidencoder.c + * + * Copyright 2024 Clay Gomera + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#include +#include +#include +#include +#include + +#define VERSION 0.1 +#define NAME "asciidencoder" + +void stringToAscii(char* input); +void asciiToCharacter(char *input); +void displayHelp(); +void displayVersion(); + +int main(int argc, char **argv) { + setlocale(LC_ALL, ""); + + int option; + int s_flag = 0, c_flag = 0; + char *s_value = NULL, *c_value = NULL; + + static const char* short_options = "s:c:hv"; + static struct option long_options[] = { + {"string", required_argument, NULL, 's'}, + {"code", required_argument, NULL, 'c'}, + {"help", no_argument, 0, 'h'}, + {"version", no_argument, 0, 'v'}, + {NULL, 0, NULL, 0} + }; + + while ((option = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) { + switch (option) { + case 's': + s_flag = 1; + s_value = optarg; + break; + case 'c': + c_flag = 1; + c_value = optarg; + break; + case 'h': + displayHelp(); + return 0; + case 'v': + displayVersion(); + return 0; + case '?': + default: + fprintf(stderr, "Use '-h, --help' for help.\n"); + return EXIT_FAILURE; + } + } + + if (s_flag && c_flag) { + fprintf(stderr, "Use only one option at a time.\n"); + displayHelp(); + return EXIT_FAILURE; + } + + // Check if no valid option was provided and display the help message + if (!s_flag && !c_flag) { + fprintf(stderr, "No valid options provided.\n"); + displayHelp(); + return EXIT_FAILURE; + } + + // Check which arguments are present + if (s_flag) { + stringToAscii(s_value); + } + else if (c_flag) { + asciiToCharacter(c_value); + } + + return EXIT_SUCCESS; +} + +void stringToAscii(char *input) { + input[strlen(input)] = '\0'; + + for (int i = 0; i < strlen(input); i++) { + printf("The ASCII code of the character '%c' is %d\n", input[i], (unsigned char)input[i]); + } +} + +// Function to convert an Extended ASCII code to its corresponding character. +void asciiToCharacter(char *input) { + int charCode = atoi(input); + if (charCode < 0 || charCode > 255) { + printf("Invalid ASCII code. Must be between 0 and 255.\n"); + } else { + printf("The ASCII character for code '%d' is '%c'\n", charCode, (char)charCode); + } +} + +// Function to display the help message +void displayHelp() { + printf("Usage: %s [OPTIONS] [VALUE]\n", NAME); + printf("ASCII Decoder & Encoder.\n\n"); + printf("Options:\n"); + printf("\t-s, --string\tGive ASCII codes for each character in the input string.\n"); + printf("\t-c, --code\tGive ASCII character for input code.\n"); + printf("\t-h, --help\tDisplay this help message and exit.\n"); + printf("\t-v, --version\tDisplay version and exit.\n"); +} + +// Function to display the version information +void displayVersion() { + printf("%s v%.1f\n", NAME, VERSION); +} +