Below is my code to convert from decimal to binary:

#include "stdafx.h"

typedef struct tamNode
{
	int Into;
	struct tamNode *Next;
}Node;

typedef struct
{
	Node *Head;
	Node *Tail;
}List;

void createList(List &l)
{
	l.Head = NULL;
	l.Tail = NULL;
}

Node* createNode(int x)
{
	Node *p;
	p = new Node;
	p->Into = x;
	p->Next = NULL;
	return p;
}

void addHead(List &l, Node *p)
{
	if(l.Head == NULL)
	{
		l.Head = p;
		l.Tail = p;
	}
	else
	{
		p->Next = l.Head;
		l.Head = p;
	}
}

void addTail(List &l, Node *p)
{
	if(l.Head == NULL)
	{
		l.Head = p;
		l.Tail = p;
	}
	else
	{
		l.Tail ->Next = p;
		l.Tail = p;
	}
}

void printList(List l)
{
	Node *p;
	p = l.Head;
	while(p != NULL)
	{
		printf("%d", p->Into);
		p = p ->Next;
	}
}

void main()
{
	List l1;
	Node *p;
	int x, sodu, i, n, k;
	//char digit[6] = {'a','b','c','d','e','f'};

	createList(l1);
	printf("Enter the number of decimal:");
	scanf("%d", &x);
	while(x!=0)
	{
		sodu = x%2;
		x = x/2;
		p = createNode(sodu);
		addHead(l1,p);
	}
	
	printf("\nThe number after converting from decimal to binary number:");
	printList(l1);
}

But I do not know how to convert from decimal to hexadecimal? Please help me!


Thanks!!!

Recommended Answers

All 7 Replies

You can first Convert your decimal to binary and then group them into digits of four and use a switch case for your sixteen hexadecimal digits.That is just 1 way of doing it.I suggested it so that you can use your above program for the same.

Can you provide me the sample code for do it!

Thanks!

Everything in the memory is in hex. :) Converting numbers to one base to another is fun, however there is a better solution for this.
http://www.cplusplus.com/reference/iostream/manipulators/hex/

//http://www.cplusplus.com/reference/iostream/manipulators/hex/

#include <iostream>
using namespace std;

int main ()
{
    int n = 15;

    cout << hex << n << endl;
    cout << dec << n << endl;
    cout << oct << n << endl;

    return 0;
}

In main() you use 2 to convert to binary, correct? What would you use for hex?

But I want to use list to solve it.

I want to solve the problem of converting decimal to hexadecimal on the same project!

What WaltP is trying to slow walk you to is dividing the amount by 16 (hexa),

how hexadecimal works is that you take the decimal amount, divide it by 16, take the fraction (after the decimal point) and multiply this by 16, then take the integer remainder (before the decimal point) and repeat the process until the dividing results in a value smaller than 1,

then you take the hexadecimal values (found by the multiplication) and turn it back to front...

this process can be found here www.permadi.com/tutorial/numDecToHex/

I wont give you the coding, sorry mate but you'll have to work for it, but I think this will give you what you need if you want to solve it yourself...

thus: 405678
step 1:

405678 / 16 = 25354.875.. take 0.875

25354 / 16 = 1584.625... take 0.625

1584 / 16 = 99.0... take 0.0

99 / 16 = 6.1875... take 0.1875

6 / 16 = 0.375... take 0.375

step two:

0.875 * 16 = 14 = E

0.625 * 16 = 10 = A

0 * 16 = 0 = 0

0.1875 * 16 = 3 = 3

0.375 * 16 = 6


Step three:EA036 -> 630AE


Hex value = 630AE

HINT: Step 1 and 2 can take place within a single loop

thus: 405678
step 1:

405678 / 16 = 25354.875.. take 0.875

25354 / 16 = 1584.625... take 0.625

1584 / 16 = 99.0... take 0.0

99 / 16 = 6.1875... take 0.1875

6 / 16 = 0.375... take 0.375

Why do it the hard way? Just do the same procedure as in the code, combine step 1&2:

405678 mod 16 = 14
25354 mod 16 = 10
1584 mod 16 = 0
99 mod 16 = 6
6 mod 16 = 6

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.