# Convert to Hexadecimal

Posted: 24 Jan, 2021

Difficulty: Easy

#### You are given an integer N. Your task is to convert the number to its equivalent Hexadecimal.

#### Note - All letters in Hexadecimal (A-F) must be in uppercase.

#### For example:

```
If N = 50, then output will be 32.
If N = -50, then output will be FFFFFFCE.
```

#### Input format:

```
The first line of input contains an integer ‘T’ denoting the number of test cases.
Then the T test cases follow.
Each test case contains an integer N.
```

#### Output format:

```
For each test case, return the corresponding Hexadecimal format of the given decimal (integer) number N.
```

##### Note :

```
You are not required to print anything; it has already been taken care of. Just implement the given function.
```

##### Constraints :

```
1 <= T <= 5
-10^4 <= N <= 10^4
Time Limit: 1 sec
```

Approach 1

The base value of the Hexadecimal number system is 16.

- Create an empty string.
- If the given number is negative use 2’s complement method.
- Divide the number by 16.
- If the remainder is between 0 to 9, store it as it is, and if the remainder lies between 10 to 15 convert it in its character form such that 10 to 15 corresponds to A to F.
- Store the remainder/character in the string.
- Repeat the above 3 steps until the number is greater than 0.
- Return the reversed answer string. (which will be the equivalent Hexadecimal number of the given integer).

SIMILAR PROBLEMS

# Oniwa and Amix Numbers

Posted: 21 Apr, 2021

Difficulty: Moderate

# Vaccination Drive

Posted: 28 Apr, 2021

Difficulty: Easy

# Maximum length of same indexed subarrays

Posted: 15 Jun, 2021

Difficulty: Moderate

# Maximise the OR Sum

Posted: 23 Jun, 2021

Difficulty: Easy

# Find All Subsets

Posted: 23 Jul, 2021

Difficulty: Easy