• 0

Quick C++ Port


Question

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

  • 0

Well, you could pass it a char array.

void chex_dec (char *x,int length)
{               
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 = *(x+index);
 if (ascii_char <= '9')
  hex_buf[index] = ascii_char - '0';
  else hex_buf[index] = ((ascii_char - '0') - 7);
 }
 while (index != length);               
 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;
}

Link to comment
Share on other sites

  • 0

Thanks for the reply but that code doesn't seem to work, hex code FF should return 255 but it returns some random result.

Link to comment
Share on other sites

  • 0

Use the default stuff in the iostream.

#include <iostream>

using namespace std;

int main()
{
	int i = 0;
	cout << "Enter a hex value: ";
	cin >> hex >> i;

	cout << "In hex: " << hex << i << endl;
	cout << "In oct: " << oct << i << endl;
	cout << "In dec: " << dec << i << endl;

	return 0;
}

Link to comment
Share on other sites

  • 0
Thanks for the reply but that code doesn't seem to work, hex code FF should return 255 but it returns some random result.

Hmm, looking at it, it should be:

void chex_dec (char *x,int length)

{

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

{

ascii_char = *(x+index);

if (ascii_char <= '9')

hex_buf[index] = ascii_char - '0';

else hex_buf[index] = ((ascii_char - '0') - 7);

++index;

}

while (index != length);

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;

}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.