Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
October 23rd, 2013, 10:44 AM
 sparky90ful
Registered User

Join Date: Sep 2013
Posts: 21
Time spent in forums: 4 h 6 m 49 sec
Reputation Power: 0
Need help with converting

i have this code and i am trying to convert a decimal to hex i get the result but recursive so i need 2a but my result is a2 can anyone help me please
PS: i can't use printf what i am doing is my own printf
Code:
void            x(long decimal)
{
int n;
int j;
int i;
int r[10];

i = 0;
j = 0;
while (decimal > 0)
{
r[i] = decimal % 16;
decimal = decimal /16;

if (r[i] < 10)
my_putnbr(r[i]);
else if (r[i] == 10)
my_putchar('a');
else if (r[i] == 11)
my_putchar('b');
else  if (r[i] == 12)
my_putchar('c');
else if (r[i] == 13)
my_putchar('d');
else if (r[i] == 14)
my_putchar('e');
else if (r[i] == 15)
my_putchar('f');
i++;
j++;
}
}

#2
October 23rd, 2013, 11:21 AM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,890
Time spent in forums: 3 Months 1 Day 19 h 4 m 17 sec
Reputation Power: 2199
Break that into two loops. In the first loop, fill the array, r; instead of i, which you'll need in the second loop, use a different variable, such as n (which is not being used) to keep count and to store the values. Then output the values in the second loop, starting with the last value you entered into the array and working your way backwards down to r[0].

#3
October 23rd, 2013, 03:22 PM
 sparky90ful
Registered User

Join Date: Sep 2013
Posts: 21
Time spent in forums: 4 h 6 m 49 sec
Reputation Power: 0
done
thanks
Code:
void            x(long decimal)
{
int j;
int i;
int r[10];

i = 0;
j = 0;
while (decimal > 0)
{
r[i] = decimal % 16;
decimal = decimal /16;
i++;
}
for(j = i - 1; j >= 0; j--)
{
if (r[j] < 10)
my_putnbr(r[j]);
else if (r[j] == 10)
my_putchar('a');
else if (r[j] == 11)
my_putchar('b');
else  if (r[j] == 12)
my_putchar('c');
else if (r[j] == 13)
my_putchar('d');
else if (r[j] == 14)
my_putchar('e');
else if (r[j] == 15)
my_putchar('f');
}
}

#4
October 23rd, 2013, 08:30 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,890
Time spent in forums: 3 Months 1 Day 19 h 4 m 17 sec
Reputation Power: 2199
If I may submit an alternative:
Code:
#include <stdio.h>

void x(unsigned long decimal)
{
int  i, n;
char r[10];

for(i=0; i<8; i++)
{
n = (decimal >> (28 - i * 4)) & 0x0000000F;
if (n < 10)
r[i] = n + '0';
else
r[i] = n -10 + 'a';
}
r[i] = '\0';

printf("%luD = 0x%s\n", decimal, r);
}

int main()
{
x(2345);
x(736588705UL);
return 0;
}

Quote:
 C:TEST>a 2345D = 0x00000929 736588705D = 0x2be773a1 C:TEST>

This is based on a 4-byte (32-bit) unsigned long. It is also based on the fact that you convert from binary to hexadecimal by taking 4 bits at a time and writing down their hex equivalent. Also keep in mind that that decimal value has been converted to binary and is stored internally as binary.

Since this approach will only work for conversion to hex, it cannot be used for converting to other number bases. Though you should be able to convert it to octal, which takes the bits three at a time, except that the most significant octal digit would only have two bits, since 32 is not evenly divisible by 3.

However, yours is the more general approach based on the general pencil-and-paper method for converting from decimal to another base. I just took advantage of a special case.

Last edited by dwise1_aol : October 23rd, 2013 at 08:33 PM.

#5
October 24th, 2013, 04:24 AM
 Mich Song
Registered User

Join Date: Oct 2013
Location: China
Posts: 6
Time spent in forums: 5 h 7 m 33 sec
Reputation Power: 0

Quote:
 Originally Posted by dwise1_aol If I may submit an alternative: Code: #include void x(unsigned long decimal) { int i, n; char r[10]; for(i=0; i<8; i++) { n = (decimal >> (28 - i * 4)) & 0x0000000F; if (n < 10) r[i] = n + '0'; else r[i] = n -10 + 'a'; } r[i] = '\0'; printf("%luD = 0x%s\n", decimal, r); } int main() { x(2345); x(736588705UL); return 0; } This is based on a 4-byte (32-bit) unsigned long. It is also based on the fact that you convert from binary to hexadecimal by taking 4 bits at a time and writing down their hex equivalent. Also keep in mind that that decimal value has been converted to binary and is stored internally as binary. Since this approach will only work for conversion to hex, it cannot be used for converting to other number bases. Though you should be able to convert it to octal, which takes the bits three at a time, except that the most significant octal digit would only have two bits, since 32 is not evenly divisible by 3. However, yours is the more general approach based on the general pencil-and-paper method for converting from decimal to another base. I just took advantage of a special case.

Hello,

As mentioned, it is possible to convert it to octal, i tried to edit the code on the basis of the above. It failed anyway.

Code:
#include <stdio.h>

void x(unsigned long decimal)
{
int  i, n;
char r[10];

for(i=0; i<9; i++)
{
n = (decimal >> (29 - i * 3)) & 0x000000007;

r[i] = n + '0';
}

r[i] = '\0';

printf("%lu D = %s O\n", decimal, r);
}

int main()
{
x(123);
return 0;
}

Can anyone give some help on it? Is it because 32 cannot be evenly divided by 3?

Thanks a lot,
Mich

#6
October 24th, 2013, 05:19 AM
 Mich Song
Registered User

Join Date: Oct 2013
Location: China
Posts: 6
Time spent in forums: 5 h 7 m 33 sec
Reputation Power: 0
just solved octal

[/code]
#include <stdio.h>

void x(unsigned long decimal)
{
int i, n;
char r[11];

for(i=0; i<11; i++)
{

n = (decimal >> (30 - i * 3)) & 0x000000007;

r[i] = n + '0';

}
r[i] = '\0';

printf("%lu D = %s O\n", decimal, r);
}

int main()
{
x(123);
return 0;
}[/code]

sorry for the disturbing

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Need help with converting