Click here to Skip to main content
15,906,574 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Suppose i have two binary values 01000100 and 01000010.
I want to compress them like this:

01000100 = 1
01000010 = 0

After compress the 2bytes would be converted to 2bits
and decompress them like:

1 = 01000100
0 = 01000010

After decompress the 2bits would be converted to 2bytes

Actually i want to compress 2byte(binary) to 2bits.And after decompress the data again convert to 2byte.
How can i do it in visual basic. I am a new in computer programming
Sergey Alexandrovich Kryukov 5-Feb-13 14:13pm    
"Anyone knows C++"?! Well, well... :-)
"Compress"?! Total gibberish, sorry. You don't understand something very basic, but I cannot figure out what...
anonymous10 6-Feb-13 10:58am    
i have only 2 values 01000100 and 01000010 not 65,532 or 256.I want to make compressor which holds only 2 values and also decompressor.
Sergey Alexandrovich Kryukov 6-Feb-13 11:03am    
You apparently have no idea what is a number, don't see a difference between string and number. Your question makes no sense at all. You cannot get help before you understand the very basics, but not on this "question" which is based on some most basic misconceptions.
anonymous10 6-Feb-13 12:24pm    
ok. let see i chat online with my friends daily.i observe that the character like: "this" "is" "a" "why" etc.These words contains 8bits per character.i want to make this = 1,is = 0,a = 10,why = 01 in binary. like:this = 4bytes when i typed this to anyone on chat the only one bit travel through internet and again in his system it will become 4bytes. may know you understand something.
Sergey Alexandrovich Kryukov 6-Feb-13 12:35pm    
No need to understand this... this is helpless...

Pretty simple if you only have 2 input and 2 output values.

char Compress(char in)
  if(68 == in) return 1;  // 01000100 -> 1
  if(66 == in) return 0;  // 01000010 -> 0
  return -1;              // invalid

char Decompress(char in)
  if(1 == in) return 68;  // 1 -> 01000100
  if(0 == in) return 66;  // 0 -> 01000010
  return -1;              // invalid

Your problem description I think was the problem here...
Share this answer
anonymous10 7-Feb-13 8:15am    
Thanks man finally the person who understand me(after 2days):laugh
anonymous10 7-Feb-13 8:30am    
But how can i read these binary values from files and compress them.
For example:
I have a text file in my hard disk in which i have only two numbers 68 and 66.
i want to load this file in c++ then compress this file and save a new file in my hard disk.
I now "fstream" in c++ but how can i use your method with this.
H.Brydon 7-Feb-13 12:34pm    
You are asking questions that are so fundamental it is hard to answer them. You really need to pursue your training books/manuals/instructor. What you are asking about here should be found in "input/output" or "I/O" section of any programming book.
anonymous10 7-Feb-13 13:18pm    
I just want to read some specific numbers.In text file i have numbers from 1 to 100.i want to read some specific numbers like 8,9,10 from text file.
anonymous10 7-Feb-13 8:34am    
Actually i am c++ beginner and i ask this question just for knowledge.
You can't do it - except in the trivial case you are giving already which is easy:
1) Is value equal to 0?
2) if yes, output value is 01000010
3) if no, output value is 01000100

Two bits can only hold 4 values:

So the other 252 possible combinations you can get into 8 bits have nowhere to be stored. Compression algorithms work by finding patterns in numbers (in essence) and require an amount of overhead to store information about the compression method - with only eight bits of data there is insufficient information for a pattern to be determined - and with overhead you would end up with a longer output than input!
Share this answer
Klaus-Werner Konrad 6-Feb-13 10:36am    
Worse - he wants to compress 2 bytes to 2 bits, so there are 65,532 other possible values :-)
anonymous10 6-Feb-13 10:52am    
i have only 2 values 01000100 and 01000010 not 65,532 or 256.I want to make compressor which holds only 2 values and also decompressor.
OriginalGriff 6-Feb-13 11:03am    
if you only have two values, that's not a compressor - it's a bit test: 0 or 1.
anonymous10 6-Feb-13 11:13am    
what i am trying to say is that i have only two binary values.01000100 and 01000010.
First in compressor i want two make then equal 1 and 0.
01000100 = 1
01000010 = 0

after that the data which is stored in harddisk is 2bits.
after decompression it will become again 2bytes.

1 = 01000100
0 = 01000010
OriginalGriff 6-Feb-13 11:16am    
Yes I understand that - but it's just a comparison / bit test - there is no compression / decompression involved!
What part of it is giving you problems?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900