LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 05-31-2005, 10:40 PM   #1
bernie82
LQ Newbie
 
Registered: May 2005
Location: Melbourne, Australia
Distribution: Fedora Core 3
Posts: 4

Rep: Reputation: 0
Compressing file using a binary file


Hi there, At the moment I am trying to create a file that contains just the characters '0' and '1'. I am trying to compress a text file by using my own coding scheme but I can't write to a file using C that actually compresses because it takes one byte to represent '0' or '1'. Can this be done through C? PLEASE HELP! THANKS!
 
Old 05-31-2005, 11:04 PM   #2
trutnev
Member
 
Registered: Sep 2003
Distribution: RedHat 9.0, Mandrake 9.1
Posts: 68

Rep: Reputation: 15
do you want to work with bits with bitwise operators ?
 
Old 06-01-2005, 01:19 AM   #3
bernie82
LQ Newbie
 
Registered: May 2005
Location: Melbourne, Australia
Distribution: Fedora Core 3
Posts: 4

Original Poster
Rep: Reputation: 0
Basically, i have a string like this for example:
001110100
And I want to write this to some file but not have it encoded as a text file.
I need this saved as a binary file.
 
Old 06-01-2005, 02:11 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Code:
itsme@itsme:~/C$ cat strtobin.c
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  char *str = "00111010";

  printf("%s binary is %X hexadecimal\n",
    str, (unsigned int)strtoul(str, NULL, 2));
  return 0;
}
itsme@itsme:~/C$ ./strtobin
00111010 binary is 3A hexadecimal
And then you can use fwrite() to write 0x3A to the file. It's 1 byte containing all 8 bits.

EDIT: You're going to have to familiarize yourself with bit operations to work with this a lot. For instance, to grab the least significant bit you could do int lsb = 0x3a & 1; and if you want the next least one you'd have to do int nextlsb = (0x3a & 2) >> 1;

It's definitely possible to do this in C, you just need to know what you're doing.

Last edited by itsme86; 06-01-2005 at 02:21 AM.
 
  


Reply



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
Automatically tell text file from binary file? Hosiah Linux - Software 3 05-31-2005 05:22 AM
convert text file to binary excel file ust Linux - General 2 11-23-2004 02:33 AM
mounting a binary file thats not on any file system!?!!? aXoneX Linux - General 7 10-26-2004 02:21 AM
compressing a file sanjith11 Linux - General 2 07-08-2004 03:42 AM
Compressing a file dynamically xanthium Programming 5 03-22-2002 05:06 PM

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

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

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