M
muss
how to convert decimal number in a hexadecimal number using C?
Please help me.
Thanks.
Please help me.
Thanks.
What have you tried?muss said:how to convert decimal number in a hexadecimal number using C?
Please help me.
Thanks.
how to convert decimal number in a hexadecimal number using C?
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 = "0123456789abcdef"[val % base]; //its a c way to
pick up that particular index value that is in [].
Obfuscated C Contest is at said:return &buf[i+1];
}
int main()
^{
char c;
char* p,*q
p=itoa(8,8);
q=itoa(8,2);
printf("%s",p);
printf("%s",p);
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 = "0123456789abcdef"[val % base]; //its a c way to
pick up that particular index value that is in [].
Obfuscated C Contest is at said:return &buf[i+1];
}
int main()
It's:
int main(void) //this is wrong...
Spell out what you mean.
^
Syntax error: missing ';'.
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.
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 ..
Do you think all mails posted in the groups contain exact perfect
optimised and performant code...???
CoL said: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
Do you think all mails posted in the groups contain exact perfect
optimised and performant code...???
its:
int main(int argc, char** argv)
CoL said: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,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...???
CoL wrote:
said:That's being rude twice, and that's twice too many times...
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.
I've noticed you've had a number of problems with this. Try another openThe original doesn't appear to have reached my server (at least not yet).
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
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
Do you think all mails posted in the groups contain exact perfect
optimised and performant code...???
Neither do standard library routines in most cases. There areYou never check that inputs are valid.
That's a fair point. Although easily fixed.static char buf[32] = {0};
Are you sure 31 characters is enough for a 256-bit int on DS9K?
Test-at-top means that value 0 is output as no digits (an emptya) Why 30?
b) Isn't it clearer to put initialisation into `for`?
buf = "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>.
Since he declared buf static, it certainly will remain allocated atYou 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.
After fixing the buffer reuse I get 10 and 1000 as expected.Once corrected, when I run it, I get:
00
00
Which is incorrect.
I would make base unsigned, and probably val also.
Neither do standard library routines in most cases. There areYou never check that inputs are valid.
arguments on both sides, and (IMO properly) different decisions in
different situations and usages.
That's a fair point. Although easily fixed.static char buf[32] = {0};
Are you sure 31 characters is enough for a 256-bit int on DS9K?
Test-at-top means that value 0 is output as no digits (an emptya) Why 30?
b) Isn't it clearer to put initialisation into `for`?
string). While mathematically defensible, this is not what most
people usually want. Test-at-bottom is one fix; another is an
override (or digits_so_far == 0) which can be extended to the
additional and often useful functionality (or digits_so_far <
min_digits).
buf = "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>.
I don't consider that obfuscated. If you don't understand that C
character strings, and particularly literal ones, are actually array
of char, you're in big trouble; and if you do, I think that is
obvious and admirably terse. (val % base) ["digits"] would be
obfuscated.
The doubleslash comment doesn't add anything though.
Since he declared buf static, it certainly will remain allocated at
the same place throughout the program's entire execution.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.