GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Dec-2004, 17:10
Rosdahale Rosdahale is offline
New Member
 
Join Date: Dec 2004
Posts: 20
Rosdahale is on a distinguished road

Hex Result giving strange answers.


I have the following function. It is supposed to return the Hex result of a decimal integer. Sometimes it gives the right answer, most of the time it comes out with nothing, and sometimes comes out with weird characters (like the ` symbol). Seems strange. I have to run the program several times before it starts coming up with the right answers.

CPP / C++ / C Code:
char *Process_Dec_To_Hex(char a)
{
	static char Remainders[20];
	int x = 0;

	while ( a != 0 )
	{	
		Remainders[x++] = "0123456789ABCDEF"[a % 16];
		a /= 16;
	}
		Remainders[x]='\0';

return(Remainders);
}

int a is a decimal number passed in to the function, it returns a string result
  #2  
Old 06-Dec-2004, 17:40
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Rosdahale
I have the following function. It is supposed to return the Hex result of a decimal integer. Sometimes it gives the right answer, most of the time it comes out with nothing, and sometimes comes out with weird characters (like the ` symbol). Seems strange. I have to run the program several times before it starts coming up with the right answers.

CPP / C++ / C Code:
char *Process_Dec_To_Hex(char a)
{
	static char Remainders[20];
	int x = 0;

	while ( a != 0 )
	{	
		Remainders[x++] = "0123456789ABCDEF"[a % 16];
		a /= 16;
	}
		Remainders[x]='\0';

return(Remainders);
}

int a is a decimal number passed in to the function, it returns a string result

You really should learn how to ask a question in a way that someone can help you. Saying things like "Sometimes it gives the right answer, most of the time it comes out with nothing, and sometimes comes out with weird characters (like the ` symbol)" doesn't give anyone a clue as to how to help.

You could say, for example: "Here is the test program (the main()) that I used to verify the output. When I entered 12, the output was xx. When I entered 21, the output was 99, etc."

Now as to your function:

The function returns a pointer to char. In the C language there is no such thing as a "string" data type, but strings are represented as arrays of char with a byte of '\0' that indicates the end of the string.

How did you test the function? For what values of decimal input numbers is the function suppposed to give you hex output values?

You must hava a plan for verifying the output.

If I had a function that was supposed to give a pointer to a two-digit hexadecimal output string for integers from 0 to 255, I would probably test it like this:

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
  char *Process_Dec_To_Hex(int a);
  int x;
  int i;

  for (i = 0; i <= 255; i++) {
    printf("%2d decimal = %s hex\n", i, Process_Dec_To_Hex(i));
  }
  return 0;
}
Note that I made the argument an int for my function (you could make it an unsigned char if you want to, but if you make it a char, it expects values of -128 to 127, not 0 to 255).

Try the test program with your function. (Start with values of i from 0 to 20 so that the output doesn't scroll off of the screen).

What does it give you?

I really think it is important to think about how you will test a program (or a function like this) before you write the program. You can write the function first, then the main() that tests it, but you should have a test plan sooner or later. I vote for sooner.

This helps to define the valid inputs and expected outputs, so that when you actually have something to test, you know what to expect. Then, if you see something you don't understand, you can dig down into the code, put some printfs() to track down the bug(s), etc.

Regards,

Dave
Last edited by davekw7x : 06-Dec-2004 at 18:17.
  #3  
Old 06-Dec-2004, 18:20
Rosdahale Rosdahale is offline
New Member
 
Join Date: Dec 2004
Posts: 20
Rosdahale is on a distinguished road
The number that passes in to the array is a random generated number from 0-255, i think the problem is what you said about char only being from -128 to 127. because it works with numbers smaller than 127, so my only problem now is how do I pass an integer in to the function and return a char that stores 0-255 (i presume its char as i need to store letters), as when i change the function from Char Convert_Dextohex(int a) to Int Convert_Dectohex(int a) it says it can not convert int to char when i return it. The value Hex_Num must store the result.

Example :
CPP / C++ / C Code:
Hex_Num=Process_Dec_To_Hex(Magic_Num); 

Hex_Num is of type char *
Magic_Num is random integer from 0-255

Function:
CPP / C++ / C Code:
char *Process_Dec_To_Hex(char a)
{
	static char Remainders[20];
	int x = 0;

	while ( a != 0 )
	{	
		Remainders[x++] = "0123456789ABCDEF"[a % 16];
		a /= 16;
	}
		Remainders[x]='\0';

return(Remainders);
}

What fdo i do then to store the result correctly
  #4  
Old 06-Dec-2004, 18:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Rosdahale
The number that passes in to the array is a random generated number from 0-255, i think the problem is what you said about char only being from -128 to 127. because it works with numbers smaller than 127, so my only problem now is how do I pass an integer in to the function and return a char that stores 0-255 (i presume its char as i need to store letters), as when i change the function from Char Convert_Dextohex(int a) to Int Convert_Dectohex(int a) it says it can not convert int to char when i return it. The value Hex_Num must store the result.

Example :
CPP / C++ / C Code:
Hex_Num=Process_Dec_To_Hex(Magic_Num); 

Hex_Num is of type char *
Magic_Num is random integer from 0-255

Function:
CPP / C++ / C Code:
char *Process_Dec_To_Hex(char a)
{
	static char Remainders[20];
	int x = 0;

	while ( a != 0 )
	{	
		Remainders[x++] = "0123456789ABCDEF"[a % 16];
		a /= 16;
	}
		Remainders[x]='\0';

return(Remainders);
}

What fdo i do then to store the result correctly


Make the argument unsigned char rather than char

It returns pointer to char. When you use the name of an array (Remainders) the compiler interprets this as pointer to char, so printf can print the value with a %s format specifier.

I still haven't seen what test program you are using. When I ran my test program (after changing the argument to your function to unsigned char), I got
Quote:
0 decimal = hex
1 decimal = 1 hex
2 decimal = 2 hex
3 decimal = 3 hex
4 decimal = 4 hex
5 decimal = 5 hex
6 decimal = 6 hex
7 decimal = 7 hex
8 decimal = 8 hex
9 decimal = 9 hex
10 decimal = A hex
11 decimal = B hex
12 decimal = C hex
13 decimal = D hex
14 decimal = E hex
15 decimal = F hex
16 decimal = 01 hex
17 decimal = 11 hex
18 decimal = 21 hex
19 decimal = 31 hex
20 decimal = 41 hex
21 decimal = 51 hex
22 decimal = 61 hex

That's not quite right is it?

What happened to 0 decimal?

is 16 decimal equal to 01 hex?

is 15 decimal equal to 11 hex?

Now your original post said the function gave weird results. I see nothing weird going on here, just some wrong answers.

Time to debug, I think.

Regards,
Dave
  #5  
Old 06-Dec-2004, 21:06
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
You also might want to reread my suggestions in the other thread. You're still using a trick that IMHO you should avoid.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #6  
Old 07-Dec-2004, 01:44
Rosdahale Rosdahale is offline
New Member
 
Join Date: Dec 2004
Posts: 20
Rosdahale is on a distinguished road
I could not get the other version working waltp, despite your suggestions, maybe me doing something wrong, you'll have to forgive me only been doing this for a few months.
  #7  
Old 07-Dec-2004, 20:28
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Rosdahale
I could not get the other version working waltp, despite your suggestions, maybe me doing something wrong, you'll have to forgive me only been doing this for a few months.
CPP / C++ / C Code:
Remainders[x++] = "0123456789ABCDEF"[a % 16];
is not something you should have learned (nor be using) after a couple months. It's a trick that makes your code a tad unreadable to the uninitiated. You should use the standard technique:
CPP / C++ / C Code:
char *HexValues = "0123456789ABCDEF";

Remainders[x++] = HexValues[a % 16];
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10
Outputting function result to an array Shufty C Programming Language 4 07-May-2004 10:45

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:37.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.