I have this code that I got from a program called arith on www.planet-source-code.com basically you input a hex value of FF say and it will output 255 I need to convert this so that I can input the value to convert into the function say by calling it as chex_dec("FF") and then output it as the return of the function by making it into an int. I'm not sure exactly how to do this because of the line ascii_char = cin.get (), cin.get() gets only a single character of input and i'm not sure if there is a replacement.
void chex_dec ()
{
char ascii_char;
char hex_buf[11] = {'0','0','0','0','0','0','0','0','0','0','0'};
int dec_count;
int index;
long int dec_value;
loop1:
index = 0;
do
{
++index;
ascii_char = cin.get ();
if (ascii_char <= '9')
hex_buf[index] = ascii_char - '0';
else hex_buf[index] = ((ascii_char - '0') - 7);
}
while (ascii_char != '\n');
dec_count = 1;
dec_value = 0;
do
{
--index;
if (hex_buf[index] != 0)
dec_value = (dec_value + (hex_buf[index] * dec_count));
dec_count = (dec_count * 16);
}
while (index >= 2);
cout << dec_value;
goto loop1;
}
Question
SatansAceN
Hey Neowin Community,
I have this code that I got from a program called arith on www.planet-source-code.com basically you input a hex value of FF say and it will output 255 I need to convert this so that I can input the value to convert into the function say by calling it as chex_dec("FF") and then output it as the return of the function by making it into an int. I'm not sure exactly how to do this because of the line ascii_char = cin.get (), cin.get() gets only a single character of input and i'm not sure if there is a replacement.
void chex_dec () { char ascii_char; char hex_buf[11] = {'0','0','0','0','0','0','0','0','0','0','0'}; int dec_count; int index; long int dec_value; loop1: index = 0; do { ++index; ascii_char = cin.get (); if (ascii_char <= '9') hex_buf[index] = ascii_char - '0'; else hex_buf[index] = ((ascii_char - '0') - 7); } while (ascii_char != '\n'); dec_count = 1; dec_value = 0; do { --index; if (hex_buf[index] != 0) dec_value = (dec_value + (hex_buf[index] * dec_count)); dec_count = (dec_count * 16); } while (index >= 2); cout << dec_value; goto loop1; }Any help would be ****ing fantastic.
From ZogoChieftan
Link to comment
Share on other sites
4 answers to this question
Recommended Posts