C++ Program to Generate Random Hexadecimal Bytes

This is a C++ Program to generate random hex bytes. The idea is to genrate decimal number first and convert it to hexadecimal.

Here is source code of the C++ Program to Generate Random Hexadecimal Bytes. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<iostream>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. int main(int argc, char **argv)
  8. {
  9.     int val = rand();
  10.     char Hex[33];
  11.     itoa(val, Hex, 16);
  12.     cout<< "Random Decimal Byte:" << val;
  13.     cout << "\nEquivalent Hex Byte: " << Hex;
  14. }

Output:

$ g++ GenerateHexByte.cpp
$ a.out
 
Random Decimal Byte:41
Equivalent Hex Byte: 29

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement

Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.