Help Help 
Author Message
 Help Help

Hi there

I have a little problem with this code. Any of you can help me with?. I
don't know what is wrong, I tried to covert 4 decimal into a binary and what
I got is "1" (binary) instead of "100" (binary). If you also can help me
with a
conversion from binary to decimal I'll appreciate it too.

Thank you so much !!!!

#include <stdio.h>
#include <string.h>
#include <conio.h>

char dectoanother (int value, int radix);
int decimal, radix;
char *hex [16], bin;

main()
{
hex[0] = "0";
hex[1] = "1";
hex[2] = "2";
hex[3] = "3";
hex[4] = "4";
hex[5] = "5";
hex[6] = "6";
hex[7] = "7";
hex[8] = "8";
hex[9] = "9";
hex[10] = "A";
hex[11] = "B";
hex[12] = "C";
hex[13] = "D";
hex[14] = "E";
hex[15] = "F";

clrscr();
printf ("\ndecimal: ");
scanf ("%d",&decimal);
printf ("\nbase/radix: ");
scanf ("%d",&radix);
bin = dectoanother(decimal,radix);
printf ("\nThe result is: %c from decimal: %d",bin,decimal);
getch();
return (0);

Quote:
}

char dectoanother (int dec1, int base)
{
char *strng;
int remainder, dec;
strng = " ";
dec = dec1;

  while (dec >= base)
    {
      remainder = dec % base;
      dec = dec / base;
      strcat (strng, hex[remainder]);
   }

  strcat (strng, hex[dec]);
  strng = strrev(strng);
  return (*strng);

Quote:
}

--



Mon, 23 Jun 2003 07:14:45 GMT  
 Help Help


Quote:
>char dectoanother (int dec1, int base)
>{
>char *strng;
>int remainder, dec;
>strng = " ";

here you arrange that strng points to a string literal (which is an
array of 2 char, a 'blank' followed by a '\0'

Quote:
>dec = dec1;

>  while (dec >= base)
>    {
>      remainder = dec % base;
>      dec = dec / base;
>      strcat (strng, hex[remainder]);

now you change a string literal which is undefined behaviour, it also
over-writes the null terminator the first time and then writes beyond
the end of the array subsequently. You are very lucky that your computer
did not blow up:)

Quote:
>   }

>  strcat (strng, hex[dec]);
>  strng = strrev(strng);
>  return (*strng);
>}

Now as this looks like homework, I am not going to give you actual code,
but think about what you are doing. You need to provide a sufficiently
large array of char to store your answer, and that is best done by
passing that into the function doing the work. Local storage will not
work, and string literals are semantically read only.

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Tue, 24 Jun 2003 06:19:58 GMT  
 Help Help
Quote:

>Hi there

>I have a little problem with this code. Any of you can help me with?. I
>don't know what is wrong, I tried to covert 4 decimal into a binary and what
>I got is "1" (binary) instead of "100" (binary). If you also can help me
>with a
>conversion from binary to decimal I'll appreciate it too.

>Thank you so much !!!!

>#include <stdio.h>
>#include <string.h>
>#include <conio.h>

>char dectoanother (int value, int radix);
>int decimal, radix;
>char *hex [16], bin;

>main()
>{
[...]
>bin = dectoanother(decimal,radix);

Now 'bin' is a single character containing some value (get to that later)

Quote:
>printf ("\nThe result is: %c from decimal: %d",bin,decimal);

And here you only print only that one character for 'result is: ' .

Quote:
>getch();
>return (0);
>}

>char dectoanother (int dec1, int base)
>{
>char *strng;

strng is now a pointer that is capable to point to a character, it may even
be used to point to some string. But you haven't allocated a string to let
it point to!

Quote:
>int remainder, dec;
>strng = " ";

Now strng points to a string constant, containing one space character and
terminated by a zero terminator.

Quote:
>dec = dec1;

>  while (dec >= base)
>    {
>      remainder = dec % base;
>      dec = dec / base;
>      strcat (strng, hex[remainder]);

OOPS. You try to concatenate something to the string where strng points to.
This isn't good because:
1. strng was pointing to a string constant, which may be located in memory
area that cannot be modified ['implementation dependent behaviour'].
2. the string will be appended, making it larger than it's available space.
Result: undefined behaviour.

Quote:
>   }

>  strcat (strng, hex[dec]);

Same here.

Quote:
>  strng = strrev(strng);
>  return (*strng);

Now the single character that is pointed to by strng, i.e. the first character
of the intended string, is returned.

Quote:
>}

To improve this:
- Let string pointer point to sufficient (and modifiable) data. If length cannot
be known in advance, let the caller supply the string.
- return pointer to the string instead of the first character
- print the string instead of the first character.

Now it's your turn again :-)

--
Rob Windgassen

When the weight of the paperwork equals the weight of the plane, the
plane will fly.
                -- Donald Douglas
--



Tue, 24 Jun 2003 06:20:02 GMT  
 Help Help

Quote:

> I have a little problem with this code. Any of you can help me with?. I
> don't know what is wrong, I tried to covert 4 decimal into a binary and what
> I got is "1" (binary) instead of "100" (binary). If you also can help me
> with a conversion from binary to decimal I'll appreciate it too.

Your code seems to convert ints to hex strings instead of binary
strings, so here're some comments in that respect. It shouldn't be too
hard to modify it to work on binary strings.

Quote:
> Thank you so much !!!!

> #include <stdio.h>
> #include <string.h>
> #include <conio.h>

> char dectoanother (int value, int radix);

See comments for dectoanother below.

Quote:
> int decimal, radix;
> char *hex [16], bin;

You could initialize your hex array here (the dimension can be
compile-time calculated, so you don't need to use the explicit 16
unless you really want to):

char    *hex[] = { "0", "1", "2", etc... };

Quote:
> main()

Implicit return int shouldn't be used. Use a correct declaration:

int     main(void)

Quote:
> {
> hex[0] = "0";
> hex[1] = "1";
> hex[2] = "2";
> hex[3] = "3";
> hex[4] = "4";
> hex[5] = "5";
> hex[6] = "6";
> hex[7] = "7";
> hex[8] = "8";
> hex[9] = "9";
> hex[10] = "A";
> hex[11] = "B";
> hex[12] = "C";
> hex[13] = "D";
> hex[14] = "E";
> hex[15] = "F";

You could initialize the array in the declaration (see above).

Quote:
> clrscr();

Oops... that's a non-standard function.

Quote:
> printf ("\ndecimal: ");

Printf is not guaranteed to print the text out. You should add

        fflush(stdout);

Quote:
> scanf ("%d",&decimal);

Try and see what happens if you enter an invalid value (a letter, for
example)...

Quote:
> printf ("\nbase/radix: ");

See the previous printf.

Quote:
> scanf ("%d",&radix);

Do you take newlines and erroneous input into account? Maybe you
should replace the scanfs with fgets and atoi.

Quote:
> bin = dectoanother(decimal,radix);
> printf ("\nThe result is: %c from decimal: %d",bin,decimal);

You want dectoanother to return a single character? I thought you
wanted something like 256 translating to "100". In that case, your bin
should probably be a pointer to char (see dectoanother comments below)
and use %s here.

Quote:
> getch();

Another non-standard function...

Quote:
> return (0);
> }

> char dectoanother (int dec1, int base)
> {
> char *strng;

Hmm... an uninitialized pointer to char above...

Quote:
> int remainder, dec;
> strng = " ";

Here you make strng point to a string literal of two bytes (one space
and one nul character). Generally you shouldn't try modify this area,
since it may reside in a read-only data storage (although it also may
not).

Quote:
> dec = dec1;

>   while (dec >= base)
>     {
>       remainder = dec % base;
>       dec = dec / base;
>       strcat (strng, hex[remainder]);

Here you add to end of a string literal: it's a BAD THING (TM). What
you should do is to make strng have some room, so change its
declaration to:

        char strng[50] = " ";

for example. After this, you can strcat to it max. 49 chars plus the
terminating nul.

Quote:
>    }

>   strcat (strng, hex[dec]);
>   strng = strrev(strng);
>   return (*strng);

This returns the first character of the memory pointed to by strng.
Maybe this is not what you wanted? If you want to return the whole
string, you should make the strng array static and return its address
(requires changing of the function return type to char*).

Quote:
> }

Another problem with dectoanother is that if you give a base of, say,
32, the behaviour is undefined since there's not enough elements in
the hex array.

Here's a function that converts an int into a hex ASCIIZ:

char    *IntToChar(int value)
{
        static char     rv[9];  /* max 8 hex digits, assuming int = 32bits */
        int             i;

        rv[8] = '\0';
        for (i = 7; i >= 0; i--) {
                rv[i] = "0123456789ABCDEF"[value & 0xf];
                if (!(value >>= 4))
                        break;
        }
        return &rv[i >= 0 ? i : 0];

Quote:
}

How this works is left as an excercise for the reader.

Later,
        AriL
--
Pain and disappointment are inevitable. Misery is optional.
Homepaged at http://www.angelfire.com/or/lukumies
--



Tue, 24 Jun 2003 06:21:40 GMT  
 Help Help
Quote:

> Hi there

> I have a little problem with this code. Any of you can help me with?. I
> don't know what is wrong, I tried to covert 4 decimal into a binary and what
> I got is "1" (binary) instead of "100" (binary). If you also can help me

[...]

As I'm sure people will point out, your code is rather "suboptimal", but
I'm only going to focus on the "why do I get the wrong answer" part of
the question, and leave the rest of the logic alone...

Quote:
> bin = dectoanother(decimal,radix);
> printf ("\nThe result is: %c from decimal: %d",bin,decimal);
[...]
> char dectoanother (int dec1, int base)

dectoanother() returns a single char, not a string of characters.

Quote:
> {
> char *strng;
> int remainder, dec;
> strng = " ";
[...]
>       strcat (strng, hex[remainder]);

This is not valid.  There is no memory available to concatenate
anything off the end of "strng".  (Not to mention that on some
platforms, the string pointed to by "strng" may be read-only.)
The fact that your program isn't crashing due to this is mere
coincidence.

[...]

Quote:
>   return (*strng);

You are returning the first character of the string you built,
rather than the string itself.

[...]

Although you are doing things the hard way, you could probably
use the basic logic you have (I haven't verified that it is
correct), with the following changes:

    Change the return value to "char *", change strng from
    "char *strng" to "static char strng[65]", initialize it with
    "strng[0] = '\0'", and then return "strng" rather than "*strng".

(Note that this limits the return string to 64 digits, which unless
you have a system with larger than 64-bit integers, it should suffice.
I'm sure someone here could give a platform-independent method of
setting the buffer size.  Also, the static buffer precludes the use of
this function to return a second value unless you are done with the
first one.)

--

+---------+----------------------------------+-----------------------------+

|    J.   |                                  |  herein are not necessarily |
|  Brody  | http://www.bestweb.net/~kenbrody |  those of fP Technologies." |
+---------+----------------------------------+-----------------------------+
GCS (ver 3.12) d- s+++: a C++$(+++) ULAVHSC^++++$ P+>+++ L+(++) E-(---)

    DI+(++++) D---() G e* h---- r+++ y?
--



Tue, 24 Jun 2003 06:22:35 GMT  
 Help Help

Quote:

> Hi there

> I have a little problem with this code. Any of you can help me with?. I
> don't know what is wrong, I tried to covert 4 decimal into a binary and
what
> I got is "1" (binary) instead of "100" (binary). If you also can help me
> with a
> conversion from binary to decimal I'll appreciate it too.

> Thank you so much !!!!

> #include <stdio.h>
> #include <string.h>
> #include <conio.h>

> char dectoanother (int value, int radix);
> int decimal, radix;
> char *hex [16], bin;

> main()
> {
> hex[0] = "0";
> hex[1] = "1";
> hex[2] = "2";
> hex[3] = "3";
> hex[4] = "4";
> hex[5] = "5";
> hex[6] = "6";
> hex[7] = "7";
> hex[8] = "8";
> hex[9] = "9";
> hex[10] = "A";
> hex[11] = "B";
> hex[12] = "C";
> hex[13] = "D";
> hex[14] = "E";
> hex[15] = "F";

> clrscr();
> printf ("\ndecimal: ");
> scanf ("%d",&decimal);
> printf ("\nbase/radix: ");
> scanf ("%d",&radix);
> bin = dectoanother(decimal,radix);
> printf ("\nThe result is: %c from decimal: %d",bin,decimal);
> getch();
> return (0);
> }

> char dectoanother (int dec1, int base)
> {
> char *strng;
> int remainder, dec;
> strng = " ";
> dec = dec1;

>   while (dec >= base)
>     {
>       remainder = dec % base;
>       dec = dec / base;
>       strcat (strng, hex[remainder]);
>    }

>   strcat (strng, hex[dec]);
>   strng = strrev(strng);
>   return (*strng);
> }
> --


Hi Justo Borregales
I tweeked your program a little
Im not using a pointer *string but I am using your function
dectoanother() check it out here is the code:

#include <stdio.h>
#include <string.h>
#include <conio.h>

void dectoanother (int value, int radix);
int decimal, radix;
char *hex [16], bin[50], strng[50];

 void main(void)
{
hex[0] = "0";
hex[1] = "1";
hex[2] = "2";
hex[3] = "3";
hex[4] = "4";
hex[5] = "5";
hex[6] = "6";
hex[7] = "7";
hex[8] = "8";
hex[9] = "9";
hex[10] = "A";
hex[11] = "B";
hex[12] = "C";
hex[13] = "D";
hex[14] = "E";
hex[15] = "F";

clrscr();
printf ("\ndecimal: ");
scanf ("%d",&decimal);
printf ("\nbase/radix: ");
scanf("%d", &radix);
dectoanother(decimal,radix);
strrev(strng);
printf ("\nThe result is: %s from decimal: %d",strng,decimal);
getch();

Quote:
}

/*-------------------   function------------------------*/
void dectoanother (int dec1, int base)
{

int dec, remainder;

dec = dec1;

  while (dec > NULL )
    {
                remainder = dec % base;
                dec /= base;
                strcat (strng, hex[remainder]);

        }

Quote:
}

IF you have any more questions feel free to email me at:

see ya
Steve
--



Tue, 24 Jun 2003 06:22:51 GMT  
 Help Help

Quote:

> Hi there

> I have a little problem with this code. Any of you can help me with?. I
> don't know what is wrong, I tried to covert 4 decimal into a binary and
what
> I got is "1" (binary) instead of "100" (binary). If you also can help me
> with a
> conversion from binary to decimal I'll appreciate it too.

> Thank you so much !!!!

> #include <stdio.h>
> #include <string.h>
> #include <conio.h>

> char dectoanother (int value, int radix);
> int decimal, radix;
> char *hex [16], bin;

> main()
> {
> hex[0] = "0";
> hex[1] = "1";
> hex[2] = "2";
> hex[3] = "3";
> hex[4] = "4";
> hex[5] = "5";
> hex[6] = "6";
> hex[7] = "7";
> hex[8] = "8";
> hex[9] = "9";
> hex[10] = "A";
> hex[11] = "B";
> hex[12] = "C";
> hex[13] = "D";
> hex[14] = "E";
> hex[15] = "F";

> clrscr();
> printf ("\ndecimal: ");
> scanf ("%d",&decimal);
> printf ("\nbase/radix: ");
> scanf ("%d",&radix);
> bin = dectoanother(decimal,radix);
> printf ("\nThe result is: %c from decimal: %d",bin,decimal);
> getch();
> return (0);
> }

> char dectoanother (int dec1, int base)
> {
> char *strng;
> int remainder, dec;
> strng = " ";
> dec = dec1;

>   while (dec >= base)
>     {
>       remainder = dec % base;
>       dec = dec / base;
>       strcat (strng, hex[remainder]);
>    }

>   strcat (strng, hex[dec]);
>   strng = strrev(strng);
>   return (*strng);
> }
> --


here is a link you should go to:
http://205.205.200.235/~brir/bin_to_dec.cpp
see ya
--



Tue, 24 Jun 2003 06:23:05 GMT  
 Help Help

roductions.com> writes
Quote:
>here is a link you should go to:
>http://205.205.200.235/~brir/bin_to_dec.cpp
>see ya

Nice to see someone so cheerfully offer to sabotage someone else's
process of learning :)

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Tue, 24 Jun 2003 14:21:39 GMT  
 Help Help
Well Francis Glassborow your right, but I was just trying to help.
I was stuck on a similar type problem, and my teacher could not be of much
help scince he is on vacation, we e-mail each other but he just could not
understand my problem after three days of trying to learn it on my own I
gave up. Then he posted his solution on his site and told me to go there
and trace it. I understand what he is doing in the code, I was trying to
do it differently. All I wanted to know was if there was a way you could
print an array of integers without a loop, or send an array of integers
to gcvt() or atoi() so I could change my array of integers into a string.
Sorry if I stept on anyones toes, Im learning myself too.

If it ain't broke, take it apart and see how it works

Steve Nelson

--



Wed, 25 Jun 2003 12:24:44 GMT  
 Help Help

roductions.com> writes

Quote:
>If it ain't broke, take it apart and see how it works

I did that to a watch once, then it did not work:)
I would greatly encourage students to help each other, unfortunately
some just copy, which is why we generally ask them to show us the code
even if it does not work, that way we can help develop understanding.

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Thu, 26 Jun 2003 12:49:20 GMT  
 Help Help
Actually, I'd say you're pretty close.  But...
On a thirty-two bit machine the longest possible int is
get this - 32 bits.  So, if you define strng as char[33]
it will hold the largest binary result.

That should be enough of a hint to fix the function.

--Jeff
--



Sun, 06 Jul 2003 04:02:25 GMT  
 Help Help

Quote:

> Actually, I'd say you're pretty close.  But...
> On a thirty-two bit machine the longest possible int is
> get this - 32 bits.  So, if you define strng as char[33]
> it will hold the largest binary result.
> That should be enough of a hint to fix the function.
> --Jeff

And to make it real C:
On any machine, the longest possible int is sizeof(int)
bytes, each of CHAR_BIT bits. Which means that a string
of '1' and '0' terminated by '\0' will *always* fit into
char[sizeof(int)*CHAR_BIT].

Now for the 64000 bytes question: How many chars do you need
for a "long int"?

--
Michiel Salters


--



Tue, 08 Jul 2003 10:49:09 GMT  
 Help Help

Quote:


> > Actually, I'd say you're pretty close.  But...
> > On a thirty-two bit machine the longest possible int is
> > get this - 32 bits.  So, if you define strng as char[33]
> > it will hold the largest binary result.

> > That should be enough of a hint to fix the function.

> And to make it real C:
> On any machine, the longest possible int is sizeof(int)
> bytes, each of CHAR_BIT bits. Which means that a string
> of '1' and '0' terminated by '\0' will *always* fit into
> char[sizeof(int)*CHAR_BIT].

> Now for the 64000 bytes question: How many chars do you need
> for a "long int"?

In unicode chars, I would expect a lower limit to be:

  char[ sizeof(long) * CHAR_BIT / 16].  
          which usually simplifies to sizeof(long)

and an upper limit of:

  char[ sizeof(long) * CHAR_BIT / 11 ].   (log2(2048))

(N869)
[#15] The number of wide characters that can be produced  by
any single conversion shall be at least 4095.

How and whether to display those chars is a separate subject left
as an exercise for the student.

--

http://www.qwikpages.com/backstreets/cbfalconer
   (Remove "NOSPAM." from reply address. my-deja works unmodified)

--



Thu, 10 Jul 2003 13:21:48 GMT  
 Help Help

Quote:

>> scanf ("%d",&radix);
> Do you take newlines and erroneous input into account? Maybe you
> should replace the scanfs with fgets and atoi.

Or fgets and strto*.

Quote:
> Here's a function that converts an int into a hex ASCIIZ:

Wrong.

Quote:
> char       *IntToChar(int value)
> {
>    static char     rv[9];  /* max 8 hex digits, assuming int = 32bits */
>    int             i;
>    rv[8] = '\0';
>    for (i = 7; i >= 0; i--) {
>            rv[i] = "0123456789ABCDEF"[value & 0xf];
>            if (!(value >>= 4))
>                    break;
>    }
>    return &rv[i >= 0 ? i : 0];
> }
> How this works is left as an excercise for the reader.

It doesn't.  Its function specification is "convert an int
into a hex ASCIIZ".  What it does instead is "convert an int
into a hex string".

Perhaps this error could be fixed by simply changing your
function specification.

--

--



Thu, 10 Jul 2003 13:33:22 GMT  
 Help Help

....
Quote:
> And to make it real C:
> On any machine, the longest possible int is sizeof(int)
> bytes, each of CHAR_BIT bits. Which means that a string
> of '1' and '0' terminated by '\0' will *always* fit into
> char[sizeof(int)*CHAR_BIT].

Plus one for the terminating null character.

--
- David.Thompson 1 now at worldnet.att.net
--



Fri, 18 Jul 2003 13:44:52 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. HELP HELP HELP HELP HELP HELP!!!

2. HELP HELP HELP HELP HELP

3. HELP help HELP help

4. HELP ++++++++++++++ HELP +++++++++++ HELP ++++++++++++++ HELP

5. OSP (Operating System Project) HELP HELP HELP HELP (files module)

6. Help Help Help

7. HELP HELP HELP

8. How to do that, HELP, HELP , HELP!

9. help..help..help..i can't put icons on the dialoge

10. Help...help...help

11. Help help help!

12. HELP - HELP - HELP

 

 
Powered by phpBB® Forum Software