String to Hexadecimal Converter in C Program with Source Code

Introduction

The C programming language is a procedural programming language. Between 1969 and 1973, Dennis Ritchie worked on the project. It was created primarily as a system programming language for developing operating systems. Low-level memory access, a small collection of keywords, and a clean style are all qualities that make C language excellent for system programming, such as operating system or compiler development.

Why Learn C Programming Language?

Because it mixes the characteristics of high-level languages with the functionalism of assembly language, C is often referred to as a middle-level computer language. The manipulation of bits, bytes, and addresses in C gives the programmer more control over how the program will behave and more direct access to the underlying hardware mechanisms.

Working programmers influenced, influenced, and field-tested C. As a result, C provides the programmer with exactly what he or she want. C++ is a more advanced version of the C programming language. C++ incorporates all of the features of C, as well as support for object-oriented programming (OOP). Furthermore, C++ includes numerous enhancements and features that make it a “better C,” regardless of whether or not it is used for object-oriented programming.

Converting String into Hexadecimal in C Program

The American Standard Code for Information Interchange (ASCII) is a character encoding standard for electronic communication.  ASCII is an 8-bit code standard that assigns letters, numerals, and other characters to the 256 slots available. For example, the decimal value of the lower case “h” character (Char) is 104, which is “01101000” in binary and “68” in hexadecimal.

The Base of 16 is used in the “Hexadecimal” or simply “Hex” numbering system. To represent all integers, hexadecimal numerals employ 16 symbols: 0, 1, 2, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. (A, B, C, D, E, F) are the letters for the following digits (10, 11, 12, 13, 14, 15).

The best way to learn is to do. Hence, what better way to learn C programming language (if you know the basics) than to code your first mini project. In this tutorial we are going to build a binary string to hexadecimal converter using c programming language. Keep in mind this is a mini project for absolute beginners and intermediate programmers as well.

We will convert each character of the given text to its hexadecimal counterpart in this program, then store the transformed value in a string, and lastly print the Hexadecimal String.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

int main()
{
unsigned char str[100],strH[200];
int i,j;
printf("Enter the string: ");
scanf("%[^\n]s",str);
printf("\nThe string given by the user is: %s\n",str);
// set strH with nulls
memset(strH,0,sizeof(strH));
// converting str character into Hex and adding into strH
for(i=0,j=0;i<strlen(str);i++,j+=2)
{ 
sprintf((char*)strH+j,"%02X",str[i]);
}
// adding NULL in the end
strH[j]='\0';
printf("The hexadecimal converted string is: \n");
printf("%s\n",strH);
return 0;
}

Output

  • Output Screen Shot 1 :
  • Output Screen Shot 2 :
  • Output Screen Shot 3 :

You may Also Like

SHARE String to Hexadecimal Converter in C Program with Source Code

You may also like...

Leave a Reply

Your email address will not be published.

Share