# Altitude Gray Code to Binary

J

#### Joe

Is there a method/formula to convert the gray code used in the aircraft mode C altitude encoding system to a Binary No.

J

#### Jake Brodsky

Gray code changes only one bit at a time. Here's a lookup table for the first sixteen hex digits

0: 0000
1: 0001
2: 0011
3: 0010
4: 0110
5: 0111
6: 0101
7: 0100
8: 1100
9: 1101
A: 1111
B: 1110
C: 1010
D: 1001
E: 1000

...And so on and so forth. Get the idea?

J

#### Johan Bengtsson

Check the archives, this subject have been up before...

/Johan Bengtsson

Do you need education in the area of automation?
----------------------------------------
P&L, Innovation in training
Box 252, S-281 23 H{ssleholm SWEDEN
Tel: +46 451 49 460, Fax: +46 451 89 833
E-mail: [email protected]
Internet: http://www.pol.se/
----------------------------------------

A

Hi, I came across your question when trying to do the exact same thing.

The method that I found is as follows:

The first 3 Least Significant Bits are the 100Ft divisions and the 6 Most Significant Bits are the 500Ft divisions. So to get the altitude from the encoder, convert C4, C2 and C1 into binary using Gray code to Binary conversions (there are many sites explaining how to do this simple conversion) and multiply this value by 100. Then convert the remaining 6 digits (shift left 3 places to get a new number) and multiply this by 500. Add the 2 values together and subtract 1300 from the answer (This enables the code to read (-1300Ft) and voila!

Hope you come right.

R

#### Roy Silvernail

What you'll discover is that this results in a series 1, 2, 3, 4, 7, 7, 4, 3 ,2 ,1. Due to the way gray code works, the interpretation of the C lines depends on the value of the DAB lines. I discovered this when doing an embedded system to translate Mode C data to serial altitude (way back in 1991).

Here's the C code to translate Mode C data to altitude. Put the gray code into a long int in the form DABC and call SSItoAlt().

unsigned short grayToBinary(unsigned short num)
{
unsigned short temp = num ^ (num>>8);
temp ^= (temp>>4);
temp ^= (temp>>2);
temp ^= (temp>>1);
return temp;
}

int getParity(long v) {
v ^= v >> 16;
v ^= v >> 8;
v ^= v >> 4;
v &= 0xf;
return (0x6996 >> v) & 1;
}

long SSItoAlt(long a) {
unsigned short dab,c;
c = grayToBinary(a & 0x07) - 1;
if(c == 6) {
c = 4;
}
dab = a >> 3;
if(getParity(dab)) {
c = 4 - c;
}
return (long)((grayToBinary(dab)*500)-1200) + (c*100);
}