Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 10-14-2004, 07:19 PM   #1 (permalink)
john_tran
Registered User
 
Join Date: Oct 2004
Posts: 22
john_tran is on a distinguished road
need your help about writing data to file

I've read the LZW compression algorithm. And i know i must use 12 bit number to code the string.
But i dont know how to write this code 12 bit to file. But C++ just accepts bytes. How can i do that with bit. Someone tell me do this :
write 16bit (2 bytes) , certainly remaining 4 bit from the next code. And add the next 16 bit to file, surely contains 8 bit from the next code, and so one. I think it is right, isn it?
But how i can do that?
Please suggesting me all the technologies i have to learn for this purpose.
Or give me the link to page that contain this topic or tutorial.
Thanks
john_tran is offline   Reply With Quote
Old 10-14-2004, 08:10 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
The standard (C++) never defined a byte as 8 bits. It defines as:
A byte is AT LEAST 8 bits.

So if you are working with 12-bit bytes then you can still use the normal way of stream manipulation.
__________________
Valmont is offline   Reply With Quote
Old 10-15-2004, 06:17 PM   #3 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
Quote:
A byte is AT LEAST 8 bits.
c++ does not define a byte at all.
a char must be of size 1. however many bits are in "1" is undefined, but a char must be able to store the "basic character set". this can be accomplished in 7 bits.

c99: 6.2.5

2. An object declared as type char is large enough to store any member of the basic execution character set. If a member of the required source character set enumerated in 5.2.1 is stored in a char object, its value is guaranteed to be positive. If any other character is stored in a char object, the resulting value is implementation-defined but shall be within the range of values that can be represented in that type.

as to the original question, see the bit-shift operators (<<, >>)
joe_bruin is offline   Reply With Quote
Old 10-16-2004, 03:20 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Yes you are right. I just opened the iso standard for C++.
Quote:
but a char must be able to store the "basic character set". this can be accomplished in 7 bits.
Yep, a 6-bit system does have problems has C/C++.
Thanks for the correction.
__________________
Valmont is offline   Reply With Quote
Old 10-16-2004, 04:05 AM   #5 (permalink)
john_tran
Registered User
 
Join Date: Oct 2004
Posts: 22
john_tran is on a distinguished road
I still dont know any thing.
Tell me more about that. And if you can, give me an example or practice.
Thanks all.
john_tran is offline   Reply With Quote
Old 10-16-2004, 04:11 AM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I don't know about LZW.

Is this any help?
http://members.aol.com/rf21exe/gif.htm
__________________
Valmont is offline   Reply With Quote
Old 10-16-2004, 04:53 AM   #7 (permalink)
john_tran
Registered User
 
Join Date: Oct 2004
Posts: 22
john_tran is on a distinguished road
Oh, I knew how LZW does, but i dont know how to write it in C++, because of bits as i mentioned.
what techs i do when i break bits. Such as :
Put 12 bit of a 12bit-number (A) and concatenate 4bit of the next 12bit-number (B) into 2 bytes and write it to file, and then put 8bit remaining of 12bit-number (B) and concatenate 8bit of the next 12bit-number (C) into 2 bytes and write it to file. After then, i continue put 4bit remaining of 12bit-number (C) and concatenate 12bit of the next 12bit-number (E) into 2 bytes and write it to file. So on, just repeat the loop if i want to add more.
But How i can do that in C++, what functions or technologies i have to learn?
Thanks for reply.
john_tran is offline   Reply With Quote
Old 10-16-2004, 01:21 PM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
Originally posted by joe_bruin
c++ does not define a byte at all.
a char must be of size 1. however many bits are in "1" is undefined, but a char must be able to store the "basic character set". this can be accomplished in 7 bits.

c99: 6.2.5

2. An object declared as type char is large enough to store any member of the basic execution character set. If a member of the required source character set enumerated in 5.2.1 is stored in a char object, its value is guaranteed to be positive. If any other character is stored in a char object, the resulting value is implementation-defined but shall be within the range of values that can be represented in that type.

as to the original question, see the bit-shift operators (<<, >>)
Thinking again:
C++ is not based on C99 but C90.
I am sure that a minimal of 8 bits are guaruanteed. The value -127 +127 are guaranteed for a byte.
The trick is that CHAR_BIT = 8.
- number of bits for the smallest object that is not a bit-field (byte)
Defined here:
5.2.4.2.1 Sizes of integer types
__________________

Last edited by Valmont; 10-16-2004 at 01:55 PM.
Valmont is offline   Reply With Quote
Old 10-16-2004, 01:23 PM   #9 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
Originally posted by john_tran
Oh, I knew how LZW does, but i dont know how to write it in C++, because of bits as i mentioned.
what techs i do when i break bits. Such as :
Put 12 bit of a 12bit-number (A) and concatenate 4bit of the next 12bit-number (B) into 2 bytes and write it to file, and then put 8bit remaining of 12bit-number (B) and concatenate 8bit of the next 12bit-number (C) into 2 bytes and write it to file. After then, i continue put 4bit remaining of 12bit-number (C) and concatenate 12bit of the next 12bit-number (E) into 2 bytes and write it to file. So on, just repeat the loop if i want to add more.
But How i can do that in C++, what functions or technologies i have to learn?
Thanks for reply.
You have to learn the ways of shifting like Joe mentioned:
>> , <<
And masking might come in handy: &

I am not so good with this myself. Sorry.
__________________
Valmont is offline   Reply With Quote
Old 10-20-2004, 04:43 AM   #10 (permalink)
john_tran
Registered User
 
Join Date: Oct 2004
Posts: 22
john_tran is on a distinguished road
Yes, I will do as your Command!!!
(I think Bit Operator will solve all my problems)
john_tran is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing to a file. (PHP) Nasimov PHP 1 03-11-2004 05:19 AM
finding precision of data in file Blaqb0x Standard C, C++ 1 09-24-2002 11:01 PM


All times are GMT -8. The time now is 10:49 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting