hex converter

i wanna ask, here's a simple test hex converter i wrote...

why my code only returned "wrong input!" ?

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
int hex_conv(std::string base) {
	double asc = 0.00;
	int count = (base.size() - 1);
	int res{ 0 };

	while (count >= 0) {
		res = hex_conv_check(base[count]) + pow(16.00, asc);
		--count;
		++asc;
	}

	return res;
}

int hex_conv_check(char param) {
	if (param == 'A')
		return 10;
	if (param == 'B')
		return 11;
	if (param == 'C')
		return 12;
	if (param == 'D')
		return 13;
	if (param == 'E')
		return 14;
	if (param == 'F')
		return 15;
	else {
		if (((int) param) >= 0 && ((int) param) < 10)
			return ((int) param);
		else
			std::cout << "Wrong input!" << std::endl;
	}
}
Last edited on
> if (((int) param) >= 0 && ((int) param) < 10)
Well from your A to F, I'm guessing chars.

So
 
if ( param >= '0' && param <= '9')

what...?? but i compared it with integer... it's not logical if it's turns out to be char...
Realise that a char is just a small integer. Have a look at an ASCII table to see what the first 10 chars are. This will lead to the wisdom of salem c answer :+)
@chipp, you are passing a char into the function, so even 0 - 9 is a char in the function. Simply casting a char to an int doesn't covert to an int. (HINT: ASCII table as TheIdeasMan mentioned)
http://www.cplusplus.com/doc/ascii/

1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
   for (char i { '0' }; i <= '9'; ++i)
   {
      std::cout << '\'' << i << '\'' << " = " << (int) i << '\n';
   }
}
'0' = 48
'1' = 49
'2' = 50
'3' = 51
'4' = 52
'5' = 53
'6' = 54
'7' = 55
'8' = 56
'9' = 57
A not optimized means to check and convert hex numbers to unsigned ints using two functions and some C++ stdlib constructs:
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
#include <iostream>
#include <string>
#include <cctype>
#include <sstream>

bool hex_check(std::string);
unsigned hex_convert(std::string);

int main()
{
   std::string hex { "12Fe6" };

   std::cout << "The hex number is: ";

   if (!hex_check(hex))
   {
      std::cout << "invalid!!!\n";
   }
   else
   {
      std::cout << hex_convert(hex) << '\n';
   }
}

bool hex_check(std::string num)
{
   for (const auto& itr : num)
   {
      if (!isxdigit(itr))
      {
         return false;
      }
   }

   return true;
}

unsigned hex_convert(std::string num)
{
   unsigned x;
   std::stringstream ss;

   ss << std::hex << num;
   ss >> x;

   return x;
}
Based upon the original functions:

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
#include <string>
#include <iostream>

int hex_conv_check(char param)
{
	if (param >= 'A' && param <= 'F')
		return param - 'A' + 10;

	if (param >= 'a' && param <= 'f')
		return param - 'a' + 10;

	if (param >= '0' && param <= '9')
		return param - '0';

	std::cout << "Wrong input!\n";
	return -1;
}

int hex_conv(const std::string& base) {
	int res {};

	for (const auto& d : base)
		res = res * 16 + hex_conv_check(d);

	return res;
}

int main()
{
	std::cout << hex_conv("12FE6") << '\n';
}


The issue, of course, is signal that a char is not a valid hex.

This is done in hex_conv as well as the conversion. This method means that somehow an invalid char has also to have a return value (as a function with a return type has to return a value of that type). Here it's -1.
Last edited on
FurryGuy wrote:
@chipp, you are passing a char into the function, so even 0 - 9 is a char in the function.

but i tried to check it within the function, and it identified as int:
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
int main (int argc, char * argv []) {
	std::string inpt;
	std::cout << "Enter: ";
	std::cin >> inpt;
	hex_conv(inpt);
	//...
}

int hex_conv(std::string base) {
	double asc = 0.00;
	int count = (base.size() - 1);
	int res{ 0 };

	while (count >= 0) {
		res = (hex_conv_check(base[count])) * (pow(16.00, asc));
		--count;
		++asc;
	}

	return res;
}

int hex_conv_check(char param) {
	std::cout << typeid((int)param).name() << std::endl;
	return 0;
}


result:
Enter: 9A2
int
int
int
press ENTER to close program.
Last edited on
It will - as you're casting param to an int - which is duly reported as an int by typeid!

Note that char is a signed (usually) 8 bit number. digits, letters etc are represented by their ASCII values with '0' being 48 etc. Don't confuse 0 (the number 0) with '0' which is the char 'a' with a value of 48. '0' and 48 can be interchanged. Same with the other ASCII chars.

See https://en.wikipedia.org/wiki/ASCII
i tried to check it within the function, and it identified as int

Because you C-style cast to an int! You could have param be ANY type and when you cast it the type is 'seen' as the type you cast it to.

A char is still a number, but not the number you "logically" believe it should be. A '0' char is 48 int.

Change your cast to double at line 24 in your latest code and the type would be reported as double.

Casting can be a 2000 kilo sledge-hammer, trying to ram one type into another. Especially C-style casts. The results may not be what you expect.
oh f*ckin sh*t!!! (pardon the lang!)

i just remembered!!!!

so you guys mean that if param contain number 0, it will be as the type of char (as it is the original type) which have integer value 48 and thus it's converted to int as 48 in this code??

if (((int) param) >= 0 && ((int) param) < 10)
yes, that trips people up frequently, '0' != 0
@jonnin: ok, thx!!
so you guys mean that if param contain number 0, it will be as the type of char (as it is the original type) which have integer value 48 and thus it's converted to int as 48 in this code??


I hope you mean number '0'....
so you guys mean that if param contain number 0, it will be as the type of char (as it is the original type) which have integer value 48 and thus it's converted to int as 48 in this code??


Not sure if you understand fully yet: there is no need for casting. '0' is a small integer and will work in comparisons and math operations, as in seeplus code earlier.

1
2
if (param >= 'A' && param <= 'F')
		return param - 'A' + 10;


So here 'A' is 65 and 'F' is 70
seeplus wrote:
I hope you mean number '0'....

yes... character '0'
ikr
Last edited on
Topic archived. No new replies allowed.