|
 |
|
 |
10-14-2004, 07:19 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Oct 2004
Posts: 22
|
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
|
|
|
10-14-2004, 08:10 PM
|
#2 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
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.
__________________
|
|
|
10-15-2004, 06:17 PM
|
#3 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
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 (<<, >>)
|
|
|
10-16-2004, 03:20 AM
|
#4 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
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.
__________________
|
|
|
10-16-2004, 04:05 AM
|
#5 (permalink)
|
|
Registered User
Join Date: Oct 2004
Posts: 22
|
I still dont know any thing.
Tell me more about that. And if you can, give me an example or practice.
Thanks all.
|
|
|
10-16-2004, 04:53 AM
|
#7 (permalink)
|
|
Registered User
Join Date: Oct 2004
Posts: 22
|
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.
|
|
|
10-16-2004, 01:21 PM
|
#8 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
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.
|
|
|
10-16-2004, 01:23 PM
|
#9 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
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.
__________________
|
|
|
10-20-2004, 04:43 AM
|
#10 (permalink)
|
|
Registered User
Join Date: Oct 2004
Posts: 22
|
Yes, I will do as your Command!!!
(I think Bit Operator will solve all my problems)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 10:49 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|