Converting a hexademical to decimal string

Please be aware I cannot use any intermediate storage such as int or long as will be working with big integer numbers. My current progress is below


1
2
3
4
5
6
7
      std::string line;
      std::ifstream testfile("test.txt");
      std::getline( testfile, line ) ;
      std::istringstream ss( line );
      std::string dest;
      ss >> std::hex >> dest;
      std::cout << dest << std::endl;
What`s your question?
My question is the heading, converting a hexadecimal string to decimal string. I am using big integer numbers, trying to convert the wrong number at once is out of the question. What I am looking for is a way to convert the hex into Dec and store individual elements in an array.
I don't think its possible without first converting the hex string to a number. I'd like to know what the experts say.
I have solved this by first converting it to a binary string :D
@Smoke

Was your solution anything like mine?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Hex2Dec.cpp : main project file.

#include "stdafx.h" // Used with MS Visual C++ 2008 Express. Delete if using different compiler.
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string>

using namespace std;

void Pause();

int main()
{
	string Hex_Upper = "0123456789ABCDEF", Hex_Lower = "0123456789abcdef", Hex_Num;
	int Hex_Value[4]={4096,256,16,1},len = 0, x,y;
	int Hex_Array[4]={0,0,0,0};
	long Hex_Convert;
	do
	{
		cout << "Enter a Hex number from 0 to FFFF :" << endl;
		cin >> Hex_Num;
		len = Hex_Num.length();
	} while ( len < 1 || len > 4);
	Hex_Convert=0;

	for (x=0;x<len;x++)
	{
		for(y=0;y<16;y++)
		{
			if(Hex_Upper[y]==Hex_Num[x] || Hex_Lower[y]==Hex_Num[x])
			{
				Hex_Array[(4-len)+x] = Hex_Value[(4-len)+x]*y;
				Hex_Convert = Hex_Convert +(Hex_Value[(4-len)+x]*y);
			}
		}
	}

	cout << endl << endl << "The value of " << Hex_Num << " in decimal is " << Hex_Convert << endl;

	for(x=0;x<4;x++)
		cout << "Hex_Array[" << x << "] = " << Hex_Array[x] << endl;

	Pause();

	return 0;
}

void Pause()
{
	cout << "\n\n\t\tPress any key to close program..\n\n";
	while (_kbhit()) _getch(); // Empty the input buffer
	_getch(); // Wait for a key
	while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}
It will be better if you reverse hexa decimal string and then do the casting as follows:

char a[]="0Xff00";
reverse(a); //after conversion result should be like a[0]=ff, a[1]=00;
unsigned int* i= ( unsigned int*)a;

char b[100];
bzero(b,100);
sprintf(b,"%s",*i);

Mine uses big integer number so using long was out of the question, my soultion focuses of operating of each individual digit
I have solved this by first converting it to a binary string :D
The algorithm will be the same, but modulo 16.
How so? The above would error the second the value of my number goes out of the long range.
@Smoke

I know it's been more than 10 days since this was commented on, but I hope you're still interest in this program. I was able to enhance it to accept a hex number to 'FFFFFFFF' and give the correct results. It does what you requested.
What I am looking for is a way to convert the hex into Dec and store individual elements in an array.


Here it is...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Hex2Dec.cpp : main project file.

#include "stdafx.h" // Used with MS Visual C++ 2008 Express. Delete if using different compiler.
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string>

using namespace std;

void Pause();

int main()
{
	string Hex_Upper = "0123456789ABCDEF", Hex_Lower = "0123456789abcdef", Hex_Num;
	const int MAX = 8;
	int len, x, y;
	unsigned long long Hex_Value[MAX]={221184241,14745616,983041,65536,4096,256,16,1};
	unsigned long long Hex_Array[MAX]={0};
	unsigned long long Hex_Convert;
	do
	{
		cout << "Enter a Hex number from 0 to FFFFFFFF :" << endl;
		cin >> Hex_Num;
		len = Hex_Num.length();
	} while ( len < 1 || len > MAX);
	Hex_Convert=0;

	for (x=0;x<len;x++)
	{
		for(y=0;y<16;y++)
		{
			if(Hex_Upper[y]==Hex_Num[x] || Hex_Lower[y]==Hex_Num[x])
			{
				Hex_Array[(MAX-len)+x] = Hex_Value[(MAX-len)+x]*y;
				Hex_Convert = Hex_Convert +(Hex_Value[(MAX-len)+x]*y);
			}
		}
	}

	cout << endl << endl << "The value of " << Hex_Num << " in decimal is " << Hex_Convert << endl << endl;

	for(x=0;x<len;x++)
		cout << "Hex_Array[" << x << "] = " << Hex_Array[(MAX-len)+x] << endl;

	Pause();

	return 0;
}

void Pause()
{
	cout << endl << endl <<"\t\tPress any key to close program.." << endl << endl;
	while (_kbhit()) _getch(); // Empty the input buffer
	_getch(); // Wait for a key
	while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}
Topic archived. No new replies allowed.