CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to convert bin to hex, hex to bin

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
yo4hsp



Joined: 12 Sep 2008
Posts: 7

View user's profile Send private message Yahoo Messenger

How to convert bin to hex, hex to bin
PostPosted: Tue Oct 27, 2009 12:00 am     Reply with quote

Hello,

I need to convert 16 bits Bin code(2) to 2 bytes Hexa (from an AD converter), then this 2bytes in Hexa(16) I need to convert to 12 bits Bin code (2). (for DA converter).

The problem is with my numbers: negatives and decimal like: -0.1234 (in dec)...

Thanks,
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Tue Oct 27, 2009 2:05 am     Reply with quote

Why do you need to convert to the HEX in the first place? Question
Data you supplied is not enough to adequately help you.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue Oct 27, 2009 3:32 am     Reply with quote

So you are reading a 16 bit value from an A2D and you need to convert that to a 12 bit value to output on your D2A !

The value, although stored as binary does not need converting! HEX and Decimal for that matter are just representations of the same value.

So 01100011 (binary) = 0x63 (hex) = 99 (decimal). No conversion, they just are those values.

What we need to know is what your A2D is reading in and what you are trying to control using the D2A.
You then need a formula to convert the 16 bit value to an appropriate 12 bit value.
yo4hsp



Joined: 12 Sep 2008
Posts: 7

View user's profile Send private message Yahoo Messenger

hex to bin, bin to hex
PostPosted: Sun Nov 01, 2009 11:57 am     Reply with quote

ok, this is my code:

Code:

int8 bin_to_hex (int bin)
{
   int8 hex;
   if ( bin == 0000) hex = 0x0;
   if ( bin == 0001) hex = 0x1;
   if ( bin == 0010) hex = 0x2;
   if ( bin == 0011) hex = 0x3;
   if ( bin == 0100) hex = 0x4;
   if ( bin == 0101) hex = 0x5;
   if ( bin == 0110) hex = 0x6;
   if ( bin == 0111) hex = 0x7;
   if ( bin == 1000) hex = 0x8;
   if ( bin == 1001) hex = 0x9;
   if ( bin == 1010) hex = 0xA;
   if ( bin == 1011) hex = 0xB;
   if ( bin == 1100) hex = 0xC;
   if ( bin == 1101) hex = 0xD;
   if ( bin == 1110) hex = 0xE;
   if ( bin == 1111) hex = 0xF;
   else hex = 0x0;
 
 return hex;
}

and the second,
Code:

int8 hex_to_bin (int hex)
{
   int8 bin;
   if (hex == 0x0) bin = 0000;
   if (hex == 0x1) bin = 0001;
   if (hex == 0x2) bin = 0010;
   if (hex == 0x3) bin = 0011;
   if (hex == 0x4) bin = 0100;
   if (hex == 0x5) bin = 0101;
   if (hex == 0x6) bin = 0110;
   if (hex == 0x7) bin = 0111;
   if (hex == 0x8) bin = 1000;
   if (hex == 0x9) bin = 1001;
   if (hex == 0xA) bin = 1010;
   if (hex == 0xB) bin = 1011;
   if (hex == 0xC) bin = 1100;
   if (hex == 0xD) bin = 1101;
   if (hex == 0xE) bin = 1110;
   if (hex == 0xF) bin = 1111;
   else bin = 0000;
   
 return bin;
}
yo4hsp



Joined: 12 Sep 2008
Posts: 7

View user's profile Send private message Yahoo Messenger

re..
PostPosted: Sun Nov 01, 2009 12:00 pm     Reply with quote

I used ADC - ads8321 (16bit) and DAC max531. The read value from ADC is applied to a FIR filter with 31 coef, then send to DAC...
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Nov 01, 2009 2:29 pm     Reply with quote

I'm almost helpless with your code.
E.g.
if (hex == 0xF) bin = 1111;

1111 means in words eleven hundred - eleven
After assigning the constant to int8, you get bin = 87, due to overflow
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 01, 2009 2:37 pm     Reply with quote

Try to find some web pages that explain number representation in C
in a simple way. Example:
http://www.codeguru.com/forum/showthread.php?t=316299

Based on your description, you really just need a simple program
as shown below.
Code:

#include <16F877.H>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include "ads8321.c"
#include "FIR_filter.c"
#include "max5321.c"

//======================================
void main()
{
int16 adc_value;
int16 result;

adc_value = read_ads8321();  // Read ADC value

result = FIR_filter(adc_value);  // Filter it

write_max5321(result);   // Write it to DAC

while(1);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group