LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-25-2010, 11:11 PM   #1
Mei Yuko
LQ Newbie
 
Registered: Aug 2010
Posts: 1

Rep: Reputation: 0
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.
 
Old 08-25-2010, 11:18 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Mei Yuko View Post
I need help with c program. Please help.
...
I.e. to do your homework instead of you ?
 
Old 08-25-2010, 11:23 PM   #3
Innocently555
LQ Newbie
 
Registered: Aug 2010
Posts: 1

Rep: Reputation: 0
I don't think Markus will appreciate it if I tell you a solution....DO YOUR CSCI192 HOMEWORK ON YOUR OWN or get zero!!!
 
Old 08-25-2010, 11:36 PM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Video File Converter bicky Linux - Newbie 6 07-09-2008 09:26 AM
Avi file converter VincentB Linux - Software 1 04-08-2006 05:49 AM
Video file converter crofty13 Linux - Software 5 08-02-2005 07:45 AM
MS Word file converter? pepelaz Programming 2 12-13-2003 08:37 AM
MS Word file converter? pepelaz Linux - Software 4 12-12-2003 03:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration