Create Binary Message Decoder: HTML, CSS, and JavaScript

Faraz

By Faraz -

Learn how to build a binary message decoder using HTML, CSS, and JavaScript. Convert binary code into readable text with this beginner-friendly tutorial.


Create Binary Message Decoder HTML, CSS, and JavaScript.jpg

Table of Contents

  1. Project Introduction
  2. HTML Code
  3. CSS Code
  4. JavaScript Code
  5. Preview
  6. Conclusion

In the ever-evolving landscape of web development, the fusion of technology and creativity continues to shape how we interact with digital content. One fascinating aspect of this realm is binary code – the fundamental language of computers. Have you ever wondered how computers communicate with each other, and ultimately with us? This tutorial is your gateway to unraveling the mysteries of binary code through the creation of a Binary Message Decoder using HTML, CSS, and JavaScript.

Binary code lies at the core of all digital information, a symphony of 1s and 0s that orchestrate the digital world's intricate dance. Imagine these digits as the atoms of the digital universe, where every piece of data, whether a simple "Hello" or a complex image, is translated into these two fundamental states. Our journey will take us from these basic states to the realm of human-readable text, a bridge between the cryptic binary world and the languages we understand.

The aim of this tutorial is twofold: to empower you with hands-on web development experience and to demystify the seemingly enigmatic world of binary. Even if you're new to programming or web development, fear not. This tutorial is designed with beginners in mind, guiding you through each step in a clear and concise manner. By the end, you'll not only have a functional Binary Message Decoder but also a deeper understanding of how code can be used to interpret and process data.

Whether you're a budding web developer eager to enhance your skills or simply a curious mind intrigued by the inner workings of technology, this journey into building a Binary Message Decoder will not only broaden your horizons but also offer a tangible sense of accomplishment. So, let's embark on this adventure together, where lines of code converge to reveal the magic hidden within the digital realm – a realm powered by the binary heartbeat of 1s and 0s.

Source Code

Step 1 (HTML Code):

To get started, we will first need to create a basic HTML file. In this file, we will include the main structure for our Decoder.

After creating the files just paste the following codes into your file. Make sure to save your HTML document with a .html extension, so that it can be properly viewed in a web browser.

Let's break down each part of the code to understand its purpose:

1. <!DOCTYPE html>: This declaration specifies that the document type is HTML5, which is the latest version of HTML.

2. <html lang="en">: The opening <html> tag indicates the start of an HTML document. The lang attribute is set to "en," indicating that the document is in English.

3. <head>: The <head> section contains meta-information about the document and external resources like stylesheets and scripts.

  • <title>Binary Message Decoder</title>: This sets the title of the web page, which appears in the browser's title bar or tab.
  • <meta charset="UTF-8" />: This meta tag specifies the character encoding of the document, which is UTF-8 in this case (a character encoding that supports a wide range of characters and symbols).
  • <meta name="viewport" content="width=device-width" />: This meta tag sets the viewport properties, allowing the web page to be responsive and adapt to various screen sizes.
  • <link rel="stylesheet" href="styles.css" />: This line links an external stylesheet named "styles.css" to the HTML document, which is used to define the page's visual styles.

4. <body>: The <body> section contains the visible content of the web page that users will interact with.

5. <div class="grid-container">: This <div> element is given the class "grid-container" and serves as a container for the entire content on the page. It likely applies a grid-based layout to arrange the different sections of the page.

6. <header>: This section represents the header of the web page.

  • <img id="header-logo" src="..." alt="JavaScript logo" />: An image with the ID "header-logo" is displayed in the header. It is the logo of the JavaScript programming language.
  • <p>Binary Message Decoder</p>: A paragraph with the text "Binary Message Decoder" is included in the header.

7. <form>: This section contains a form for user input.

  • <h1>Binary Message Decoder</h1>: A top-level heading indicating the purpose of the form.
  • <input type="text" placeholder="Enter a binary message" id="binary-message" />: An input field where users can enter a binary message. The placeholder text provides a hint to users.
  • <input type="submit" id="submit" value="Decode" />: A submit button that users can click to decode the entered binary message.
  • <p id="decoded-message"></p>: An empty paragraph element with the ID "decoded-message." This is likely where the decoded message will be displayed.
  • <a href="https://en.wikipedia.org/wiki/ASCII" target="_blank">ASCII - American Standard Code for Information Interchange</a>: A link to the Wikipedia page about ASCII, which opens in a new tab when clicked.

8. <footer>: The footer section at the bottom of the page.

  • <p>© Developed by Paulo Fidalgo</p>: A paragraph indicating that the page was developed by Paulo Fidalgo.

9. <script src="script.js"></script>: This line includes an external JavaScript file named "script.js." This script is likely responsible for handling the decoding process and interacting with the page elements.

This is the basic structure of our Binary Message Decoder using HTML, and now we can move on to styling it using CSS.

Step 2 (CSS Code):

Once the basic HTML structure of the Binary Message Decoder is in place, the next step is to add styling to the Decoder using CSS.

Next, we will create our CSS file. In this file, we will use some basic CSS rules to style our Decoder.

1. The first block of code applies a set of styles to all elements on the page using the universal selector *. These styles include:

  • Setting the margin and padding of all elements to 0, effectively removing any default spacing.
  • Using the box-sizing property to ensure that an element's width and height include its padding and border, but not its margin. This helps in the consistent sizing of elements.
  • Vendor-specific prefixes (-webkit-box-sizing and -moz-box-sizing) are included to support older versions of Safari and Firefox.

2. The styles for the body element include:

  • Setting the font family to a sans-serif font.
  • Setting the default font size to 16 pixels.
  • Setting the text color to a light grayish-blue (#F1F1F9).

3. The .grid-container class is used to create a CSS grid layout. The grid is divided into three rows: a fixed 50px header, a flexible main content area, and a fixed 40px footer. These areas are defined using the grid-template-rows property and named using the grid-template-areas property.

4. Styles for the header element:

  • Background color is set to black (#000).
  • It occupies the area defined as "header" in the grid.
  • Positioned at the top-left corner of the grid.
  • Uses flexbox to horizontally center its contents.
  • Padding is applied to provide some spacing inside the header.
  • Font size is set to 1em (relative to the default font size).
  • Text color is set to white (#FFF).

5. Styles for the #header-logo element:

  • The logo's height and width are both set to 3% of the viewport width (vw).
  • Margin is applied to create space on the right side of the logo.

6. Styles for the form element:

  • It occupies the "content" area of the grid.
  • Flexbox is used to center its contents vertically and horizontally.
  • A background image is set, and various background properties control its display.
  • Opacity is set to 0.9 to make the background slightly transparent.

7. Styles for various elements within the form:

  • The h1 element within the form has a large font size and margin to provide a title-like appearance.
  • input fields have a light gray background color, margin, width, height, padding, and rounded borders.
  • The #submit button has a green background color, changes color and moves slightly when hovered.
  • Paragraphs (p) and anchor links (a) have defined margins and font sizes.

8. Styles for the footer element:

  • Background color is black (#000).
  • It occupies the "footer" area of the grid.
  • Contents are horizontally aligned to the right.
  • Font size is smaller (0.75em) and italicized.

This will give our Binary Message Decoder an upgraded presentation. Create a CSS file with the name of styles.css and paste the given codes into your CSS file. Remember that you must create a file with the .css extension.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-box-sizing: border-box; /* for Safari */
  -moz-box-sizing: border-box; /* for Firefox */
}

body {
  font-family: sans-serif;
  font-size: 16px;
  color: #F1F1F9;
}

/* CSS grid layout */
.grid-container {
  display: grid;
  height: 100vh;
  grid-template-rows: 50px 1fr 40px;
  grid-template-areas:
    "header"
    "content"
    "footer"
}

/* CSS grid elements */
header {
  background-color: #000;
  grid-area: header;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  padding: 2px 10px;
  font-size: 1em;
  color: #FFF;
}

#header-logo {
  height: 3vw;
  width: 3vw;
  margin-right: 10px;
}

form {
  grid-area: content;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-image: url(https://toppng.com/uploads/preview/binary-code-code-numbers-green-glow-11569783618iniftegu8q.jpg);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat; 
  opacity: 0.9;
}

form h1 {
  margin-bottom: 1em;
  font-size: 2.75em;
}

form input {
  background-color: #F1F1F9;
  margin: 1.5em auto;
  width: 18em;
  height: 3em;
  border: none;
  border-radius: 5px;
  padding: 10px;
}

form #submit {
  background-color: #5CA641;
  opacity: 0.9;
  color: #F1F1F9;
  cursor: pointer;
}

form #submit:hover {
  background-color: #FD3535;
  opacity: 0.9;
  color: #F1F1F9;
}

form p {
  margin: 1.5em auto;
  font-size: 1.25em;  
}

form a {
  margin: 1.5em auto;
  font-size: 1em;
  font-style: italic;
  color: #5CA641;
  opacity: 0.9;
  transition: transform 0.5s ease-in-out;      
}

form a:hover {
  color: #FD3536;
  transform: translateY(0.5rem);
}

footer {
  background-color: #000;
  grid-area: footer;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 2px 10px;
  font-size: 0.75em;
  font-style: italic;
  color: #FFF;
} 

Step 3 (JavaScript Code):

Finally, we need to create a function in JavaScript.

Let's break down the code step by step:

1. The binaryTranslator function: This function takes a single argument str, which is the binary-encoded message to be translated. It converts the binary-encoded characters into their corresponding numeric values and then uses the String.fromCharCode() method to convert those numeric values into actual characters. The decoded characters are then concatenated to form the translated message, which is eventually returned.

2. The enteredMessage function: This function is intended to be triggered when a form is submitted (typically through a button click), as it includes the line event.preventDefault();. This prevents the default behavior of form submission, allowing the JavaScript code to handle the submission instead.

Inside this function, the code fetches the value entered in an input field with the id 'binary-message'. This value is assumed to be a binary-encoded message. Then, it calls the binaryTranslator function with the binary message as an argument to decode it.

The decoded message is then placed into an HTML element with the id 'decoded-message' using the .innerHTML property. This will display the decoded message on the web page.

3. Event Listener: The code gets a reference to an HTML element with the id 'submit', which is assumed to be a button. It adds an event listener to this button for the 'click' event. When the button is clicked, the enteredMessage function is triggered.

Create a JavaScript file with the name script.js and paste the given codes into your JavaScript file and make sure it's linked properly to your HTML document so that the scripts are executed on the page. Remember, you’ve to create a file with .js extension.

// function for decode the message:
function binaryTranslator(str) {
  let translatedMessage = '';
  
  str.split(' ').forEach(binaryTranslator => {
    let numericCharacterRepresentation = parseInt(binaryTranslator, 2);
    let letter = String.fromCharCode(numericCharacterRepresentation);
    translatedMessage += letter;
  })
  return translatedMessage;
}


// Get binary message and decode it using 'binaryTranslator()' function:
function enteredMessage(event) {
event.preventDefault();
var binaryMessage = document.getElementById('binary-message').value;
var decode = binaryTranslator(binaryMessage);  
document.getElementById('decoded-message').innerHTML = decode;
}

var btn = document.getElementById('submit');
btn.addEventListener('click', enteredMessage);

Final Output:

Create Binary Message Decoder HTML, CSS, and JavaScript.gif

Conclusion:

Congratulations! You've successfully built a binary message decoder using HTML, CSS, and JavaScript. You've learned how to translate binary code into human-readable text, opening the door to endless possibilities in data interpretation and web development.

By following these steps, you've not only created a functional decoder but also gained insights into event handling, data manipulation, and user interface design in web development. Have fun experimenting with your new decoder and exploring further enhancements you can make!

Credit: Paulo Fidalgo

That’s a wrap!

I hope you enjoyed this post. Now, with these examples, you can create your own amazing page.

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post