Please look at this code n tell me where i am wrong 
Author Message
 Please look at this code n tell me where i am wrong

Hi again,

I need to convert a bunch of variables to another kind of variable, i.e.
if a have an int, i have to convert that to hexadecimal. This is what i am doing
please let me know or give me a hint on what to do to fix my problem,
ultimatelly, i want to be able to have print in hex

this ia my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>

int main()
{

char char1='g';
int dnumber;
int hnumber[25];

dnumber = (char)char1;
hnumber = (float)char1;
printf ("Pointer value of %c is %p\n",char1,dnumber);
printf ("Hexadecimal value of %c is %x\n",char1,dnumber);
printf ("Character value of %c is %c\n",char1,dnumber);
printf ("Floating point value of %c is %f\n",char1,number);
printf ("integer decimal value of %c is %d\n",char1,dnumber);
printf ("Hexadecimal  %c is %x ",char1,hnumber);
getch();
return 0;

Quote:
}



Wed, 31 Mar 2004 00:20:06 GMT  
 Please look at this code n tell me where i am wrong

Quote:

>  I need to convert a bunch of variables to another kind of variable, i.e.
>  if a have an int, i have to convert that to hexadecimal.

You are confused. Hexadecimal is not some different kind of number,
it is one way to print a number. 0x10 and decimal 16 have the same
value, they are just different representations. No conversion is
necessary (or even possible) on the value.

Quote:
>  This is what i am doing
>  please let me know or give me a hint on what to do to fix my problem,
>  ultimatelly, i want to be able to have print in hex

What do you want to print in hex?

Quote:
>  this ia my code:
>  #include <stdio.h>
>  #include <string.h>
>  #include <stdlib.h>

The latter two headers are not needed.

Quote:
>  #include <conio.h>

This is a non-standard header. Don't post platfor-specific code to
comp.lang.c, we only discuss ISO C and its predecessors.

Quote:
>  int main()

There's no reason not to use a prototype here.

int main(void)

Quote:
>  {

>  char char1='g';

Indent your code, that will make it more readable.

Quote:
>  int dnumber;
>  int hnumber[25];

>  dnumber = (char)char1;

The cast is not necessary. char1 is a char, and it will be promoted
to int (or unsigned int) automatically, whether you like it or not.

Quote:
>  hnumber = (float)char1;

You cannot assign anything to an array name. What are you trying to
do?

Quote:
>  printf ("Pointer value of %c is %p\n",char1,dnumber);

Undefined behavior. The output this generates depends heavily on the
platform the program is compiled and run on. Whatever this line
prints will probably be more confusing than enlightening.

Quote:
>  printf ("Hexadecimal value of %c is %x\n",char1,dnumber);

"%x" expects an unsigned int. Since dnumber is positive, this will
probably work. But it's still undefined, I believe.

Quote:
>  printf ("Character value of %c is %c\n",char1,dnumber);
>  printf ("Floating point value of %c is %f\n",char1,number);

Undefined behavior.

Quote:
>  printf ("integer decimal value of %c is %d\n",char1,dnumber);
>  printf ("Hexadecimal  %c is %x ",char1,hnumber);

Undefined behavior.

Quote:
>  getch();

Nonstandard function. You can use getchar() instead if this is just
meant to keep an output window open.

Quote:
>  return 0;
>  }

Gergo

--
Where it is a duty to worship the sun it is pretty sure to be a
crime to examine the laws of heat.
                -- Christopher Morley



Wed, 31 Mar 2004 01:09:12 GMT  
 Please look at this code n tell me where i am wrong

Quote:

>Hi again,

>I need to convert a bunch of variables to another kind

In C, 'kind' is known as 'type'.

Quote:
>of variable, i.e.
>if a have an int, i have to convert that to hexadecimal.

C has a type 'int', it does not have a type 'hexadecimal'
Hexadecimal is a number base, expressed with the characters
'0' through '9' and 'A' through 'F'.  All objects are stored
as binary.  The values of numerical type objects can be
output in any of a number of ways, some of which involve
different number bases, e.g. decimal and hexadecimal.

Quote:
> This is what i am doing
>please let me know or give me a hint on what to do to fix my problem,
>ultimatelly, i want to be able to have print in hex

>this ia my code:
>#include <stdio.h>
>#include <string.h>
>#include <stdlib.h>
>#include <conio.h>

Nonstandard header.  Please omit from code posted here.

Quote:

>int main()
>{

>char char1='g';

A (single) character.

Quote:
>int dnumber;

A (single) integer.

Quote:
>int hnumber[25];

An array of twenty-five integers.

Quote:

>dnumber = (char)char1;

That cast is ridiculous.  'char1' is already
type 'char'.  What do you think the cast is
doing for you?  Also, if you want to store a
'char' value in an 'int', you can just do it
directly:

int dnumber = 'g';

Quote:
>hnumber = (float)char1;

Why are you casting the character to type 'float'?
What do you think that will achieve?

Also note that 'hnumber' is an array.  You cannot
assign to arrays.

Also, even if you were assigning to a single int
object, your cast to float has absolutely no effect,
except that you're trying to assign a value of one
type to another which cannot be guaranteed to represent
all possible values of the first.

Quote:
>printf ("Pointer value of %c is %p\n",char1,dnumber);

'Pointer value of a char'?  What does that mean?
The character expressed as an address?  What good
is that?  (Note that the result of such an expression
is undefined).  The only possible 'pointer value' associated
with 'char1' that has any meaning, is 'char1's address,
expressed with '&char1'

Quote:
>printf ("Hexadecimal value of %c is %x\n",char1,dnumber);

Here's the 'meat' of what you need (according to your
description above), but like this:

printf ("Hexadecimal value of %c is %x\n",char1,char1);

You can throw away that 'dnumber' and 'hnumber'.
You don't need them.

Quote:
>printf ("Character value of %c is %c\n",char1,dnumber);

This is kind of pointless, isn't it?  It says e.g.

"Character value of g is g."

Quote:
>printf ("Floating point value of %c is %f\n",char1,number);

Floating point value?  What for?

Quote:
>printf ("integer decimal value of %c is %d\n",char1,dnumber);
>printf ("Hexadecimal  %c is %x ",char1,hnumber);

The second value output will be the address of the
first element of the array 'hnumber'.  I.e. it's the
same as &hnumber[0].  I doubt that's what you want.

Quote:
>getch();

Nonstandard function. Please omit from code posted here.

Equally suitable would be the standard call:

getchar();

And please learn to indent your code.

Quote:
>return 0;
>}

-Mike


Wed, 31 Mar 2004 01:22:15 GMT  
 Please look at this code n tell me where i am wrong

Quote:

> I need to convert a bunch of variables to another kind of variable, i.e.
> if a have an int, i have to convert that to hexadecimal. This is what i am doing
> please let me know or give me a hint on what to do to fix my problem,
> ultimatelly, i want to be able to have print in hex

You had better check with your prof to make sure you understand the
problem correctly...

Quote:
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> #include <conio.h>
> int main()

int main(void)

Quote:
> {
> char char1='g';
> int dnumber;
> int hnumber[25];
> dnumber = (char)char1;
> hnumber = (float)char1;

Converting 'g' to a float has no meaning. Nor does converting 'g' to a
pointer (to char), or a double. Since 'g' has an integer type (char), it
can be cast to any other integer type (char, short, int, long).

Quote:
> printf ("Pointer value of %c is %p\n",char1,dnumber);
> printf ("Hexadecimal value of %c is %x\n",char1,dnumber);
> printf ("Character value of %c is %c\n",char1,dnumber);
> printf ("Floating point value of %c is %f\n",char1,number);
> printf ("integer decimal value of %c is %d\n",char1,dnumber);
> printf ("Hexadecimal  %c is %x ",char1,hnumber);
> getch();

getch is NOT a standard function. Try getchar.

Quote:
> return 0;
> }

        david

--
If 91 were prime, it would be a counterexample to your conjecture.
    -- Bruce Wheeler



Wed, 31 Mar 2004 01:15:09 GMT  
 Please look at this code n tell me where i am wrong


Quote:
> I need to convert a bunch of variables to another kind of variable,
> i.e. if a have an int, i have to convert that to hexadecimal. This is

You don't. Numerical values are stored in binary in C. What can be done is
to use a decimal, octal or hexadecimal character representation using
*printf() and the appropriate string format converter: "%d" %o" %x" or
"%X".

Quote:
> what i am doing please let me know or give me a hint on what to do to
> fix my problem, ultimatelly, i want to be able to have print in hex

> this ia my code:
> #include <stdio.h>
> #include <string.h>

What for ?

Quote:
> #include <stdlib.h>

What for ?

Quote:
> #include <conio.h>

Non portable and probably useless header.

Quote:
> int main()
> {

> char char1='g';

a character has a type of int.

   int char1 = 'g';

Quote:
> int dnumber;
> int hnumber[25];

> dnumber = (char)char1;

The cast is useless. Pascal != C.

Quote:
> hnumber = (float)char1;

This is a bug. hnumber is not a L-value. It's an array identifier (in fact,
a constant address).

Quote:
> printf ("Pointer value of %c is %p\n",char1,dnumber);

What the hell is a pointer value? Do you meant the address of char1?

   printf ("value of char1 is '%c' address of char1 is %p\n"
          ,char1
          ,(void*) &char1
          );

Quote:
> printf ("Hexadecimal value of %c is %x\n",char1,dnumber);

It's not a "value". It's a string representation. "%x" wants an unsigned
int.

   printf ("Hexadecimal representation of '%c' is %x\n"
          ,char1
          ,(unsigned int)char1
          );

Quote:
> printf ("Character value of %c is %c\n",char1,dnumber);

   printf ("Character value of %c is %c\n",char1,char1);

but you knew it already...

Quote:
> printf ("Floating point value of %c is %f\n",char1,number);

Nonsense. If you insist:

   printf ("Floating point value of %c is %f\n"
           ,char1
           , (double)char1
           );

Quote:
> printf ("integer decimal value of %c is %d\n",char1,dnumber);

a character is already an int:

   printf ("integer decimal value of '%c' is %d\n"
          ,char1
          ,char1
          );

Quote:
> printf ("Hexadecimal  %c is %x ",char1,hnumber);

Again?

Quote:
> getch();

Drop that, or use getchar().

Quote:
> return 0;
> }

--
-hs- emdel at noos.fr "support Afghans against Taleban"
"Car les bandits qui sont cause des guerres
 N'en meurent jamais, on n'tue qu'les innocents."
Gaston Monthus -- La Butte Rouge


Wed, 31 Mar 2004 03:32:23 GMT  
 Please look at this code n tell me where i am wrong


...

Quote:
>> int main()
>> {

>> char char1='g';

>a character has a type of int.

>   int char1 = 'g';

True, but it is perfectly reasonable, in many cases normal, to store it in
a char variable.

Quote:
>> int dnumber;
>> int hnumber[25];

>> dnumber = (char)char1;

>The cast is useless. Pascal != C.

And even if a cast was required or desired it would be (int) rather
than (char).

Quote:
>> hnumber = (float)char1;

>This is a bug. hnumber is not a L-value. It's an array identifier (in fact,
>a constant address).

Technically speaking it is a non-modifiable lvalue. However you still can't
assign to it. Also type conversion of a float value to an array (or
for that matter pointer) makes no sense.

If the OP wantd to convert the number to a string then a cast won't do it,
rather a function like sprintf() is needed:

    sprintf(hnumber, "%f", (double)char1);

Quote:
>> printf ("Pointer value of %c is %p\n",char1,dnumber);

>What the hell is a pointer value? Do you meant the address of char1?

Or to look at it another way %p requirs its argument to be a valid value
of type void *, which char1 certainly isn't.

Quote:
>   printf ("value of char1 is '%c' address of char1 is %p\n"
>          ,char1
>          ,(void*) &char1
>          );

That's valid, although it isn't clear whether it meets the OP's intentions,
mostly because they aren't clear.

Quote:
>> printf ("Hexadecimal value of %c is %x\n",char1,dnumber);

>It's not a "value". It's a string representation. "%x" wants an unsigned
>int.

>   printf ("Hexadecimal representation of '%c' is %x\n"
>          ,char1
>          ,(unsigned int)char1
>          );

>> printf ("Character value of %c is %c\n",char1,dnumber);

>   printf ("Character value of %c is %c\n",char1,char1);

This is equally correct with dnumber. And when you change char1 to int
there isn't a lot of difference.

Quote:
>but you knew it already...

>> printf ("Floating point value of %c is %f\n",char1,number);

>Nonsense. If you insist:

>   printf ("Floating point value of %c is %f\n"
>           ,char1
>           , (double)char1
>           );

>> printf ("integer decimal value of %c is %d\n",char1,dnumber);

>a character is already an int:

A char is an integer type but it is not int. However it will get promoted
to int (or rarely unsigned int) when passed in a variable argument list
such as printf()'s. For this reason %c takes an int argument as %d does.

Quote:
>   printf ("integer decimal value of '%c' is %d\n"
>          ,char1
>          ,char1
>          );

>> printf ("Hexadecimal  %c is %x ",char1,hnumber);

>Again?

If a string has been written to hnumber then the appropriate conversion
specifier is %s.

Quote:
>> getch();

>Drop that, or use getchar().

>> return 0;
>> }

--
-----------------------------------------


-----------------------------------------


Wed, 31 Mar 2004 22:17:50 GMT  
 Please look at this code n tell me where i am wrong

Quote:


>> char char1='g';

>a character has a type of int.

Are you sure?

 * Character --- a single byte representing a member of the basic
   character set of either the source or the execution environment.

If you meant a "character constant", then say so!

Quote:
>   int char1 = 'g';

Why?  The standard guarantees that 'g' is representable as a char.
The original declaration is correct and with well defined behaviour.
Your version provides no improvement, so what is your point?

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Sun, 04 Apr 2004 05:24:35 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Please I am looking for sample code (C++ BUT NO MFC PLEASE)

2. Please tell me what went wrong...

3. Please tell me what im doing wrong :(

4. please help, where am i wrong??

5. can you tell me what's wrong with the code

6. What am i doing wrong in this code?

7. Code: What am I doing wrong?

8. could please look at my code and tell me what I'm doing wrong here.

9. Could someone please look at my code and tell me what I'm doing wrong here

10. Am I wrong or the compiler is wrong?

11. Am I wrong or compiler is wrong?

12. i am looking for some source code.

 

 
Powered by phpBB® Forum Software