Need help with c programming
ask i will help
concerning what be specific also
I am writing a code which takes in hex values and double values and uses it in a formula
and the answer should b in hex
I know how to scan a hex number and use in the formula. The only problem I am having is how can I convert the decimal value into hex
any idea?
try this one char * double2HexString(double a) { char *buf = new char[17]; // double is 8-byte long, so we have 2*8 + terminating \0 char *d2c; d2c = (char *) &a; char *n = buf; int i; for(i = 0; i < 8; i++) { sprintf(n, "%02X", *d2c++); n += 2; } *(n) = '\0'; } also declare the library
hm..I see you are using looping but we are told not to!! I don't know why. It is a very basic program. We have not started using arrays yet.
anyways, thank you !. I will try to do something else.
ok
Here's a recursive function for converting decimal to any base up to 36: #include <stdio.h> #include <stdlib.h> int mkDigit( int number, int base, char* answer ) { char characters[37] = { "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" }; int remainder, index; if( number == 0 ) return( 0 ); remainder = number % base; number /= base; index = mkDigit( number, base, answer ); answer[ index ] = characters[ remainder ]; return( ++index ); }
Join our real-time social learning platform and learn together with your friends!