wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> cs >> Hexadecimal Conversion
(Message started by: kens on Jul 24th, 2007, 2:32pm)

Title: Hexadecimal Conversion
Post by kens on Jul 24th, 2007, 2:32pm
Write a function to convert a hexadecimal number into binary.

Title: Re: Hexadecimal Conversion
Post by I_am_searching on Jul 24th, 2007, 2:41pm
Does that mean we need to print the binary equivalent of the number?... If yes.. then how is the hexadecimal number inputed. Is it inputed as a string?

Title: Re: Hexadecimal Conversion
Post by kens on Jul 24th, 2007, 2:58pm
The haxadecimal number can be declared as a variable in the main and initialized. Or,the main may take string input and convert to hexadecimal and pass it to a function that prints the output in binary.

Title: Re: Hexadecimal Conversion
Post by I_am_searching on Jul 24th, 2007, 3:12pm
Kool..

I would then follow a very simple approach of pre compute and use the values.

I have a string array of size 16.

arr[0]="0000"
arr[1]="0001"
.
.
.
.
.
arr[14]="1110"
arr[15]="1111"

Though this might not be that space efficient but it saves alot of time in actually computing these values. This kind of approach helps alot where time complexity is give importance over space.

Algo

start reading the input hexa decimal string.

for(i=0;i<strlen(input);i++)
{
ch=input[i];
if( ch >= 48 && ch <=57)
index=ch-'0';
else if(ch >= 65 && ch <= 70)
index=10+ch-'A';
else if(ch >=97 && ch <=102)
index=10+ch-'a';
else
{
printf("Invalid Input")
return;
}
strcat(output,arr[index]);
}
return output;

Title: Re: Hexadecimal Conversion
Post by towr on Jul 24th, 2007, 3:14pm
If we consider string input and output

table[16] = { "0000", "0001" ... etc}

while(input)
{
 s= *intput++;
 if (s >='a')
   printf("%s", table[10+s-'a']);
 else
   printf("%s", table[s-'0']);
}

[edit]Seems like I was beaten to it (and I ignored potential invalid input)[/edit]

Title: Re: Hexadecimal Conversion
Post by Margit on Jul 26th, 2007, 12:53am
Hmm.

if( ch >= 48 && ch <=57)
index=ch-'0';
else if(ch >= 65 && ch <= 70)
index=10+ch-'A';
else if(ch >=97 && ch <=102)

Doesn't work on EBCDIC machines eg. IBM "big iron" boxen.  

Title: Re: Hexadecimal Conversion
Post by I_am_searching on Jul 26th, 2007, 1:02am
We can change this to...


if( ch >= '0' && ch <='9')  
index=ch-'0';  
else if(ch >= 'A' && ch <= 'F')  
index=10+ch-'A';  
else if(ch >='a' && ch <= 'f')  
index=10+ch-'a';  


:)

Title: Re: Hexadecimal Conversion
Post by Grimbal on Jul 26th, 2007, 2:39am
In Java, assuming the input is a string:
Integer.toString(Integer.parseInt(hexNumber, 16), 2)

Title: Re: Hexadecimal Conversion
Post by Sameer on Jul 26th, 2007, 9:13am
Create an associative array and use the hex number as the index to get respective binary value..

Title: integer using putchar
Post by mad on Jul 31st, 2007, 12:50pm
Print an integer using only putchar. Try doing it without using extra storage.

Title: Re: integer using putchar
Post by Grimbal on Aug 2nd, 2007, 7:55am

on 07/31/07 at 12:50:51, mad wrote:
Print an integer using only putchar. Try doing it without using extra storage.

switch( n ){
 case 0; putchar('0'); break;
 case -1: putchar('-'); // fallthrough
 case 1: putchar('1'); break;
 case -2: putchar('-'); // fallthrough
 case 2: putchar('2'); break;
...
}

::)

Title: Re: Hexadecimal Conversion
Post by baba on Aug 21st, 2007, 4:48am
Hey Grimbal what abt something like

void PrintWithoutExtraStorage(int n)
{

if (n>10)

PrintWithoutExtraStorage(n/10);

putchar(n%10+'0');

}

Assuming we don't count the stack used for recursion as extra storage. 8)

Title: Re: Hexadecimal Conversion
Post by Grimbal on Aug 21st, 2007, 5:01am

on 08/21/07 at 04:48:20, baba wrote:
Assuming we don't count the stack used for recursion as extra storage. 8)

That's the problem.

And what happens when n=10?

Title: Re: Hexadecimal Conversion
Post by baba on Aug 21st, 2007, 6:13am

on 08/21/07 at 05:01:05, Grimbal wrote:
And what happens when n=10?


Nice catch...just change the comparison to  (n>=10)



Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board