Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-02-2006, 08:09 PM   #1
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
Question Printing the octal representation of a character in C...

First let me start by saying I am new to C. My program needs to read in a character in binary(including non-printable characters) and then display the character in octal representation, like the 'od' program does. My code is as follows:

  int c;

  FILE *Fp;
  Fp = fopen(argv[1], "rb");

  while((c = fgetc(Fp)) != EOF)
      fprintf(stdout, "%06ho\t", c);

The two program results are as follows:
[solaris]$ myod test
000150 000145 000154 000154 000157 000040 000167 000157 000162 000154 000144 000012

[solaris]$ od test
0000000 064145 066154 067440 073557 071154 062012
"%06ho" was given to me for formatting purposes, so I know that is correct.

I'll admit, I don't really understand...basically typing "myod test" should perform the same function as typing "od test", where test is a binary file.

Thanks for your help.

Last edited by smoothdogg00; 03-02-2006 at 11:00 PM.
Old 03-02-2006, 11:44 PM   #2
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,571

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
od -b test
man od
will help you understanding what's going on.


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
python: converting a 3 character octal string into and ASCII char llama_meme Programming 1 07-06-2010 03:00 PM
ncurses printing character array prob. slzckboy Programming 3 07-31-2005 09:51 AM
Problem with reading and printing a character hubabuba Programming 3 02-28-2005 03:41 PM
Textual representation of numbers lackluster Programming 4 09-05-2004 05:02 PM
Can 't convert from a character to octal value in memory Linh Programming 2 07-21-2003 04:11 PM

All times are GMT -5. The time now is 03:45 PM.

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