Velocity Reviews > convert decimal number in a hexadecimal number ?

convert decimal number in a hexadecimal number ?

muss
Guest
Posts: n/a

 03-16-2006
how to convert decimal number in a hexadecimal number using C?
Thanks.

Ian Collins
Guest
Posts: n/a

 03-16-2006
muss wrote:
> how to convert decimal number in a hexadecimal number using C?
> Thanks.
>

What have you tried?

--
Ian Collins.

CoL
Guest
Posts: n/a

 03-16-2006
This solves ur problem...

#include<stdio.h>

//function to convert a value to the desired base binary,oct,Hex
char* itoa(int val, int base){

static char buf[32] = {0};

int i = 30;

for(; val && i ; --i, val /= base)

buf[i] = "0123456789abcdef"[val % base]; //its a c way to
pick up that particular index value that is in [].

return &buf[i+1];

}

int main()
{

char c;
char* p,*q
p=itoa(8,;
q=itoa(8,2);
printf("%s",p);
printf("%s",p);

}

muss wrote:
> how to convert decimal number in a hexadecimal number using C?
> Thanks.

Guest
Posts: n/a

 03-16-2006
On Thursday 16 March 2006 05:58, muss opined (in
<(E-Mail Removed) .com>):

> how to convert decimal number in a hexadecimal number using C?

--

Friction is a drag.

Guest
Posts: n/a

 03-16-2006
On Thursday 16 March 2006 07:48, CoL opined (in
<(E-Mail Removed) .com>):
> muss wrote:
>> how to convert decimal number in a hexadecimal number using C?
>> Thanks.

>
> This solves ur problem...

No, it does not, as it doesn't work (even when all syntax errors are
corected).

Also, don't top-post (corrected here).

> #include<stdio.h>
>
> //function to convert a value to the desired base binary,oct,Hex

(Don't use `//` when posting to Usenet.)

> char* itoa(int val, int base){

You never check that inputs are valid.

> static char buf[32] = {0};

Are you sure 31 characters is enough for a 256-bit int on DS9K?

> int i = 30;

a) Why 30?
b) Isn't it clearer to put initialisation into `for`?

> for(; val && i ; --i, val /= base)
>
> buf[i] = "0123456789abcdef"[val % base]; //its a c way to
> pick up that particular index value that is in [].

Obfuscated C Contest is at <www.ioccc.org>.

>
> return &buf[i+1];
> }
>
> int main()

It's:

int main(void)

Spell out what you mean.

> {
>
> char c;
> char* p,*q

^
Syntax error: missing ';'.

> p=itoa(8,;
> q=itoa(8,2);
> printf("%s",p);
> printf("%s",p);

You need '\n' at the end of `printf` for anything to be output. You also
never output string pointed to by `q`. Do you think it's guaranteed
that the same memory will be allocated to `buf` on the second
invocation of `itoa`? If you do, you are mistaken. More likely you did

Also:

return 0;

Would've been nice (although not required in C99).

> }

Once corrected, when I run it, I get:

00
00

Which is incorrect.

As OP's question sounds like a homework assignment, I'll leave it to two
of you to correct this (or come up with something different).

--

Those who in quarrels interpose, must often wipe a bloody nose.

CoL
Guest
Posts: n/a

 03-16-2006
Who the hell is this Vladimir.....???????
I just gave him a hint of solution....
My fuction char* itoa(int val, int base) is perfectly right ..it
accepts only one input at a time so in main I gave two different inputs
first try itoa(8,base 8..then
itoa(8,2)base 2, not both same time...U will see the perfect answers.
and this is hint not a narration of exact program given with a
intention the the one who raised this problem can modify this according
to his requirements.

Do you think all mails posted in the groups contain exact perfect
optimised and performant code...???

> On Thursday 16 March 2006 07:48, CoL opined (in
> <(E-Mail Removed) .com>):
> > muss wrote:
> >> how to convert decimal number in a hexadecimal number using C?
> >> Thanks.

> >
> > This solves ur problem...

>
> No, it does not, as it doesn't work (even when all syntax errors are
> corected).
>
> Also, don't top-post (corrected here).
>
> > #include<stdio.h>
> >
> > //function to convert a value to the desired base binary,oct,Hex

>
> (Don't use `//` when posting to Usenet.)
>
> > char* itoa(int val, int base){

>
> You never check that inputs are valid.
>
> > static char buf[32] = {0};

>
> Are you sure 31 characters is enough for a 256-bit int on DS9K?
>
> > int i = 30;

>
> a) Why 30?
> b) Isn't it clearer to put initialisation into `for`?
>
> > for(; val && i ; --i, val /= base)
> >
> > buf[i] = "0123456789abcdef"[val % base]; //its a c way to
> > pick up that particular index value that is in [].

>
> Obfuscated C Contest is at <www.ioccc.org>.
>
> >
> > return &buf[i+1];
> > }
> >
> > int main()

>
> It's:
>
> int main(void) //this is wrong...

its:
int main(int argc, char** argv)
>
> Spell out what you mean.
>
> > {
> >
> > char c;
> > char* p,*q

> ^
> Syntax error: missing ';'.
>
> > p=itoa(8,;
> > q=itoa(8,2);
> > printf("%s",p);
> > printf("%s",p);

>
> You need '\n' at the end of `printf` for anything to be output. You also
> never output string pointed to by `q`. Do you think it's guaranteed
> that the same memory will be allocated to `buf` on the second
> invocation of `itoa`? If you do, you are mistaken. More likely you did
> not copy and paste your code, but retyped it -- badly.

> ///what crap you trying to make ??

> Also:
>
> return 0;
>
> Would've been nice (although not required in C99).
>
> > }

>
> Once corrected, when I run it, I get:
>
> 00
> 00
>
> Which is incorrect.
>
> As OP's question sounds like a homework assignment, I'll leave it to two
> of you to correct this (or come up with something different).
>
> --
>
> Those who in quarrels interpose, must often wipe a bloody nose.

Richard Heathfield
Guest
Posts: n/a

 03-16-2006
CoL said:

> Who the hell is this Vladimir.....???????

A guy that pointed out some errors in your code, it seems.

> I just gave him a hint of solution....

And Vladimir gave you some hints on how to make it better.

> My fuction char* itoa(int val, int base) is perfectly right ..

The original doesn't appear to have reached my server (at least not yet).
I'd be glad to look it over if you want a second opinion and are prepared
to post it again. But if Vladimir says it's broken, I'm inclined to believe
him. He certainly has a tendency to be right more often than not.

> Do you think all mails posted in the groups contain exact perfect
> optimised and performant code...???

Of course they don't. But they ought to, if they are being presented as
solutions rather than questions. Or at least, if short cuts /have/ been
taken, these should be made explicit, perhaps with a brief comment.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Guest
Posts: n/a

 03-16-2006

CoL wrote:
> Who the hell is this Vladimir.....???????

You are being rude...

(A: Someone not afraid to show his full name.)

> I just gave him a hint of solution....
> My fuction char* itoa(int val, int base) is perfectly right ..it

You're not telling the truth...

> Do you think all mails posted in the groups contain exact perfect
> optimised and performant code...???

> > On Thursday 16 March 2006 07:48, CoL opined (in
> > <(E-Mail Removed) .com>):
> > >
> > > int main()

> >
> > It's:
> >
> > int main(void) //this is wrong...

> its:
> int main(int argc, char** argv)

....and you don't really know C.

FYI, it can be both:

int main(void)
int main(int argc, char *argv[])
or anything that an implementation cares to define

Your

int main()

is close, but not exactly the same as the first one above.

> > > printf("%s",p);
> > > printf("%s",p);

> >
> > You need '\n' at the end of `printf` for anything to be output. You also

>
> > ///what crap you trying to make ??

That's being rude twice, and that's twice too many times...

> > Once corrected, when I run it, I get:
> >
> > 00
> > 00
> >
> > Which is incorrect.

Care to dispute this with the output of your code? If you do, please
post *exact* code that you compiled and ran, together with it's output.

That way someone may be able to check your claims (but beware the sig).
I won't, as I won't be reading any more of your posts. Goodbye.

*PLONK*

--

Those who in quarrels interpose, must often wipe a bloody nose.

CBFalconer
Guest
Posts: n/a

 03-16-2006
CoL wrote:
>
> Who the hell is this Vladimir.....???????
> I just gave him a hint of solution....
> My fuction char* itoa(int val, int base) is perfectly right ..it
> accepts only one input at a time so in main I gave two different inputs
> first try itoa(8,base 8..then
> itoa(8,2)base 2, not both same time...U will see the perfect answers.
> and this is hint not a narration of exact program given with a
> intention the the one who raised this problem can modify this according
> to his requirements.
>
> Do you think all mails posted in the groups contain exact perfect
> optimised and performant code...???

Don't toppost. Do quote relative portions, and snip
irrelevancies. Ignoring your rudeness, poor spelling, poor
punctuation, and lacking proper code to criticize, we can say
little more, except that yes, we do expect code to be correct, but
not optimized.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the

Guest
Posts: n/a

 03-16-2006
On Thursday 16 March 2006 10:53, Vladimir S. Oka opined (in
<(E-Mail Removed) .com>):
> CoL wrote:

<snip>

>> > On Thursday 16 March 2006 07:48, CoL opined (in
>> > <(E-Mail Removed) .com>):

<snip>

>> > > printf("%s",p);
>> > > printf("%s",p);
>> >
>> > You need '\n' at the end of `printf` for anything to be output. You
>> > also

>>
>> > ///what crap you trying to make ??

>
> That's being rude twice, and that's twice too many times...
>
>> > Once corrected, when I run it, I get:
>> >
>> > 00
>> > 00
>> >
>> > Which is incorrect.

>
> Care to dispute this with the output of your code? If you do, please
> post *exact* code that you compiled and ran, together with it's
> output.

FWIW, here's the code from CoL's original post, in its unadulterated
incorrectness (I've only reduced spacing, mostly vertical, and removed

#include<stdio.h>

/* function to convert a value to the desired base binary,oct,Hex */
char* itoa(int val, int base){

static char buf[32] = {0};

int i = 30;

for(; val && i ; --i, val /= base)
buf[i] = "0123456789abcdef"[val % base]; /* its a c way to
pick up that particular index value that is in []. */

return &buf[i+1];
}

int main()
{
char c;
char* p,*q; /* VSO added ';' */
p=itoa(8,;
q=itoa(8,2);
printf("%s",p);
printf("%s",p);
}

--

We are not loved by our friends for what we are;
rather, we are loved in spite of what we are.
-- Victor Hugo