![]() |
|
#1
|
|||
|
|||
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:
int a is a decimal number passed in to the function, it returns a string result |
|||
|
#2
|
|||
|
|||
|
Quote:
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:
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
|
|||
|
|||
|
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 is of type char * Magic_Num is random integer from 0-255 Function: CPP / C++ / C Code:
What fdo i do then to store the result correctly |
|
#4
|
|||
|
|||
|
Quote:
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:
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
CPP / C++ / C Code:
CPP / C++ / C Code:
__________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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