wu :: forums
« wu :: forums - Hexadecimal Conversion »

Welcome, Guest. Please Login or Register.
Jan 6th, 2025, 7:58pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: william wu, Icarus, ThudnBlunder, Eigenray, SMQ, Grimbal, towr)
   Hexadecimal Conversion
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Hexadecimal Conversion  (Read 834 times)
kens
Junior Member
**





   


Posts: 59
Hexadecimal Conversion  
« on: Jul 24th, 2007, 2:32pm »
Quote Quote Modify Modify

Write a function to convert a hexadecimal number into binary.
IP Logged
I_am_searching
Newbie
*





   


Gender: male
Posts: 30
Re: Hexadecimal Conversion  
« Reply #1 on: Jul 24th, 2007, 2:41pm »
Quote Quote Modify Modify

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?
IP Logged
kens
Junior Member
**





   


Posts: 59
Re: Hexadecimal Conversion  
« Reply #2 on: Jul 24th, 2007, 2:58pm »
Quote Quote Modify Modify

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.
IP Logged
I_am_searching
Newbie
*





   


Gender: male
Posts: 30
Re: Hexadecimal Conversion  
« Reply #3 on: Jul 24th, 2007, 3:12pm »
Quote Quote Modify Modify

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;
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Hexadecimal Conversion  
« Reply #4 on: Jul 24th, 2007, 3:14pm »
Quote Quote Modify Modify

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]
« Last Edit: Jul 24th, 2007, 3:15pm by towr » IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Margit
Junior Member
**





   


Gender: female
Posts: 54
Re: Hexadecimal Conversion  
« Reply #5 on: Jul 26th, 2007, 12:53am »
Quote Quote Modify Modify

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.  
« Last Edit: Jul 26th, 2007, 12:59am by Margit » IP Logged
I_am_searching
Newbie
*





   


Gender: male
Posts: 30
Re: Hexadecimal Conversion  
« Reply #6 on: Jul 26th, 2007, 1:02am »
Quote Quote Modify Modify

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';  
 
 
Smiley
IP Logged
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Hexadecimal Conversion  
« Reply #7 on: Jul 26th, 2007, 2:39am »
Quote Quote Modify Modify

In Java, assuming the input is a string:
Integer.toString(Integer.parseInt(hexNumber, 16), 2)
IP Logged
Sameer
Uberpuzzler
*****



Pie = pi * e

   


Gender: male
Posts: 1261
Re: Hexadecimal Conversion  
« Reply #8 on: Jul 26th, 2007, 9:13am »
Quote Quote Modify Modify

Create an associative array and use the hex number as the index to get respective binary value..
« Last Edit: Jul 26th, 2007, 9:13am by Sameer » IP Logged

"Obvious" is the most dangerous word in mathematics.
--Bell, Eric Temple

Proof is an idol before which the mathematician tortures himself.
Sir Arthur Eddington, quoted in Bridges to Infinity
mad
Junior Member
**





   


Posts: 118
integer using putchar  
« Reply #9 on: Jul 31st, 2007, 12:50pm »
Quote Quote Modify Modify

Print an integer using only putchar. Try doing it without using extra storage.
IP Logged
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: integer using putchar  
« Reply #10 on: Aug 2nd, 2007, 7:55am »
Quote Quote Modify Modify

on Jul 31st, 2007, 12:50pm, 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;
...
}
 
 Roll Eyes
IP Logged
baba
Newbie
*





   


Gender: male
Posts: 14
Re: Hexadecimal Conversion  
« Reply #11 on: Aug 21st, 2007, 4:48am »
Quote Quote Modify Modify

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. Cool
IP Logged
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Hexadecimal Conversion  
« Reply #12 on: Aug 21st, 2007, 5:01am »
Quote Quote Modify Modify

on Aug 21st, 2007, 4:48am, baba wrote:
Assuming we don't count the stack used for recursion as extra storage. Cool

That's the problem.
 
And what happens when n=10?
« Last Edit: Aug 21st, 2007, 5:03am by Grimbal » IP Logged
baba
Newbie
*





   


Gender: male
Posts: 14
Re: Hexadecimal Conversion  
« Reply #13 on: Aug 21st, 2007, 6:13am »
Quote Quote Modify Modify

on Aug 21st, 2007, 5:01am, Grimbal wrote:

And what happens when n=10?

 
Nice catch...just change the comparison to  (n>=10)
IP Logged
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

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