LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Graycode to hexadecimal converter from input file (https://www.linuxquestions.org/questions/programming-9/graycode-to-hexadecimal-converter-from-input-file-828519/)

Mei Yuko 08-25-2010 11:11 PM

Graycode to hexadecimal converter from input file
 
I need help with c program. Please help.

Design, debug and test a C program that uses Gray Code for decoding information stored in a binary file.
Gray Code G(A)of any value A can be easily produced from unsigned binary B(A) using the expression:
G(A) = B(A) ^ ( B(A) >> 1 ) (1)
The formula may be used to generate coding tables. For example for 4-bit codes:
Table 1. Coding table

Hex Value Binary Gray
0 0000 0000
1 0001 0001
2 0010 0011
3 0011 0010
4 0100 0110
5 0101 0111
. . . . . . . . . .
D 1101 1011
E 1110 1001
F 1111 1000

DESIGN SPECIFICATION
Design a program that reads a sequence of packed 4-bit numbers from a binary file encoded using Gray Code. Each byte stored in the file contains two packed together Gray coded values.
For example, the binary sequence of Gray codes
00111001 01110001 01101000 . . .
having been decoded, represents the following sequence of hexadecimal numbers:
2 E 5 1 4 F . . . (see Table 1 )


Your program shall
-read the sequence of bytes from the binary file stream.gr
-decompose each byte into two Gray coded 4-bit numbers
-decode every 4-bit number converting them into hexadecimal values
-output values in the same sequence as they appear in the file using hexadecimal notation

The file size may be arbitrary, but it is limited to 64 bytes.
To simplify decoding process, your program shall generate a decoding table using the expression (1). The table should be an array with 16 elements that contains unsigned binary values with array indexes that correspond to Gray codes. It can be easily derived from completed Table 1.
Table 2. Decoding table
Array Index Hexadecimal value
0000 0
0001 1
0010 3
0011 2
0100 7
0101 6
0110 4
. . .
1110 B
1111 A
With the Table 2, the decoding process becomes straightforward:
unsigned char decodingTable[16] = { 0, 1, 3, 2, 7, . . 0xA };
. . . .
hexValue = decodingTable[ grayValue ];

CODING REQUIREMENTS
Your program shall be menu driven and the menu shall be context dependent
----Menu----
1 Open a binary file
2 Exit the program
Please select an option (1 or 2):

If the first option is selected, your program shall prompt the user to enter the file name and open the specified file. In this assignment you can presume that the file size is always limited to 64 bytes. If the file is not found, the program shall display a message:
File not found
and display the menu, so the user can enter file name correctly or exit the program.

Sergei Steshenko 08-25-2010 11:18 PM

Quote:

Originally Posted by Mei Yuko (Post 4077965)
I need help with c program. Please help.
...

I.e. to do your homework instead of you ?

Innocently555 08-25-2010 11:23 PM

I don't think Markus will appreciate it if I tell you a solution....DO YOUR CSCI192 HOMEWORK ON YOUR OWN or get zero!!!

Wim Sturkenboom 08-25-2010 11:36 PM

OK, so where is your problem?

For the menu, look at the man pages for
printf to display the menu
scanf and fgets to read the user's choice (optionally read up on raw keyboard input)
fgets to read the filename form keyboard

For reading the file, look at the man pages for
open and fopen
read and fread
close and fclose

For splitting a byte into two nibbles, read up on bitwise shift and logical and

For the algorithm, you can probably find it using your favorite search engine


All times are GMT -5. The time now is 08:08 AM.