Check if a string represents a hexadecimal number or not
Last Updated :
23 Nov, 2023
Given an alphanumeric string S of length N, the task is to check if the given string represents a hexadecimal number or not. Print Yes if it represents a hexadecimal number. Otherwise, print No.
Examples:
Input: S = “BF57C”
Output: Yes
Explanation:
Decimal Representation of the given string = 783740
Input: S = “58GK”
Output: No
Approach: The approach is based on the idea that all the elements of a hexadecimal number lie between characters A to F or between integers 0 to 9. Below are the steps to solve the problem:
- Iterate over the given string.
- Check if each character of the string is between characters A to F or between 0 and 9.
- If found to be true, then print Yes.
- Otherwise, print No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void checkHex(string s)
{
int n = s.length();
for ( int i = 0; i < n; i++)
{
char ch = s[i];
if ((ch < '0' || ch > '9' ) &&
(ch < 'A' || ch > 'F' ))
{
cout << "No" << endl;
return ;
}
}
cout << "Yes" << endl;
}
int main()
{
string s = "BF57C" ;
checkHex(s);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG {
public static void checkHex(String s)
{
int n = s.length();
for ( int i = 0 ; i < n; i++) {
char ch = s.charAt(i);
if ((ch < '0' || ch > '9' )
&& (ch < 'A' || ch > 'F' )) {
System.out.println( "No" );
return ;
}
}
System.out.println( "Yes" );
}
public static void main(String[] args)
{
String s = "BF57C" ;
checkHex(s);
}
}
|
Python3
def checkHex(s):
for ch in s:
if ((ch < '0' or ch > '9' ) and
(ch < 'A' or ch > 'F' )):
print ( "No" )
return
print ( "Yes" )
s = "BF57C"
checkHex(s)
|
C#
using System;
class GFG{
public static void checkHex(String s)
{
int n = s.Length;
for ( int i = 0; i < n; i++)
{
char ch = s[i];
if ((ch < '0' || ch > '9' ) &&
(ch < 'A' || ch > 'F' ))
{
Console.WriteLine( "No" );
return ;
}
}
Console.WriteLine( "Yes" );
}
public static void Main(String[] args)
{
String s = "BF57C" ;
checkHex(s);
}
}
|
Javascript
<script>
function checkHex(s)
{
let n = s.length;
for (let i = 0; i < n; i++)
{
let ch = s[i];
if ((ch < '0' || ch > '9' ) &&
(ch < 'A' || ch > 'F' ))
{
document.write( "No" );
return ;
}
}
document.write( "Yes" );
}
let s = "BF57C" ;
checkHex(s);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Approach 2:
Approach:
We can use the built-in function int() with a base of 16 to convert the string to a hexadecimal number. If the conversion is successful, the input string represents a hexadecimal number.
- The input string s is passed as a parameter to the function.
- The int() function is called with the input string s and a base of 16 as its parameters. This function converts a string representation of a number into an integer.
- If the conversion is successful, the function returns True, indicating that the input string represents a hexadecimal number.
- If the conversion raises a ValueError, the function returns False, indicating that the input string does not represent a hexadecimal number. The ValueError is raised when the input string contains characters that are not valid hexadecimal digits.
C++
#include <iostream>
#include <string>
#include <sstream>
bool isHexadecimal( const std::string& s) {
try {
std::istringstream stream(s);
unsigned int value;
stream >> std::hex >> value;
return !stream.fail();
} catch (std::exception& e) {
return false ;
}
}
int main() {
std::string s1 = "BF57C" ;
if (isHexadecimal(s1)) {
std::cout << "Yes" << std::endl;
std::istringstream stream(s1);
unsigned int value;
stream >> std::hex >> value;
std::cout << "Decimal Representation of the given string = " << value << std::endl;
} else {
std::cout << "No" << std::endl;
}
std::string s2 = "58GK" ;
if (isHexadecimal(s2)) {
std::cout << "Yes" << std::endl;
std::istringstream stream(s2);
unsigned int value;
stream >> std::hex >> value;
std::cout << "Decimal Representation of the given string = " << value << std::endl;
} else {
std::cout << "No" << std::endl;
}
return 0;
}
|
Java
public class HexadecimalValidator {
public static boolean isHexadecimal(String s) {
try {
Integer.parseInt(s, 16 );
return true ;
} catch (NumberFormatException e) {
return false ;
}
}
public static void main(String[] args) {
String s1 = "BF57C" ;
if (isHexadecimal(s1)) {
System.out.println( "Yes" );
System.out.println( "Decimal Representation of the given string = " + Integer.parseInt(s1, 16 ));
} else {
System.out.println( "No" );
}
String s2 = "58GK" ;
if (isHexadecimal(s2)) {
System.out.println( "Yes" );
System.out.println( "Decimal Representation of the given string = " + Integer.parseInt(s2, 16 ));
} else {
System.out.println( "No" );
}
}
}
|
Python3
def is_hexadecimal_1(s):
try :
int (s, 16 )
return True
except ValueError:
return False
s1 = "BF57C"
if is_hexadecimal_1(s1):
print ( "Yes" )
print ( "Decimal Representation of the given string = {}" . format ( int (s1, 16 )))
else :
print ( "No" )
s2 = "58GK"
if is_hexadecimal_1(s2):
print ( "Yes" )
print ( "Decimal Representation of the given string = {}" . format ( int (s2, 16 )))
else :
print ( "No" )
|
C#
using System;
public class GFG {
public static bool IsHexadecimal( string s)
{
try {
_ = Convert.ToInt32(s, 16);
return true ;
}
catch (Exception) {
return false ;
}
}
public static void Main( string [] args)
{
string s1 = "BF57C" ;
if (IsHexadecimal(s1)) {
Console.WriteLine( "Yes" );
int value = Convert.ToInt32(s1, 16);
Console.WriteLine(
"Decimal Representation of the given string = "
+ value);
}
else {
Console.WriteLine( "No" );
}
string s2 = "58GK" ;
if (IsHexadecimal(s2)) {
Console.WriteLine( "Yes" );
int value = Convert.ToInt32(s2, 16);
Console.WriteLine(
"Decimal Representation of the given string = "
+ value);
}
else {
Console.WriteLine( "No" );
}
}
}
|
Javascript
function isHexadecimal(s) {
try {
let value = parseInt(s, 16);
return !isNaN(value);
} catch (e) {
return false ;
}
}
let s1 = "BF57C" ;
if (isHexadecimal(s1)) {
console.log( "Yes" );
let value = parseInt(s1, 16);
console.log( "Decimal Representation of the given string =" , value);
} else {
console.log( "No" );
}
let s2 = "58GK" ;
if (isHexadecimal(s2)) {
console.log( "Yes" );
let value = parseInt(s2, 16);
console.log( "Decimal Representation of the given string =" , value);
} else {
console.log( "No" );
}
|
Output
Yes
Decimal Representation of the given string = 783740
No
Time Complexity: O(n)
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...