473,786 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operating with binary

QQ
Hi I have a unsigned char array, but I need to compress it in the bit
level.
for instance the array is 0x91,0x92,0x93. ...
I need to change it into a bit stream like 10010001 10010010 10010011
....
and operate this stream bit by bit and transform it into another bit
stream,
and convert that bit stream into an unsigned char array.
How can I do it?
Thanks a lot!

Aug 30 '06 #1
22 2301
QQ wrote:
Hi I have a unsigned char array, but I need to compress it in the bit
level.
for instance the array is 0x91,0x92,0x93. ...
I need to change it into a bit stream like 10010001 10010010 10010011
...
and operate this stream bit by bit and transform it into another bit
stream,
and convert that bit stream into an unsigned char array.
How can I do it?
What is the transformation you want to achieve between the
two bit streams ? Depending on that it may not be necessary
to turn it into a "bit stream" at all.

Having said that I'n not completely clear what you mean by
bit stream. Also why you are using the word compress.

Aug 30 '06 #2
QQ wrote:

like another poster I'm unclear what you want to do.
Hi I have a unsigned char array, but I need to compress it in the bit
level.
for instance the array is 0x91,0x92,0x93. ...
I need to change it into a bit stream like 10010001 10010010 10010011
you are aware that everything is already in binary? That 0x91 is just a

convenient notation for describing a binary number.
and operate this stream bit by bit and transform it into another bit
stream,
which you fail to specify...
and convert that bit stream into an unsigned char array.
How can I do it?
well if you want to take an array of unsigned char apply a
transformation
at the bit level to produce another array of unsigned char then you
probably
want to look at C's bit level operators. See &, |, ^, !, >and <<
(assuming
I didn't miss something).

If you want to print binary then you'll have to do something with
shifts and
masks to produce a sequence of '0' and '1' characters.

HTH

--
Nick Keighley

Aug 30 '06 #3
Nick Keighley wrote:

well if you want to take an array of unsigned char apply a
transformation
at the bit level to produce another array of unsigned char then you
probably
want to look at C's bit level operators. See &, |, ^, !, >and <<
(assuming
I didn't miss something).
You probably meant ~ instead of !

Aug 30 '06 #4
for example, you can say

#define SIZE 8
#define BITS(n,x) ( ( (x) & (1<<(n)) ) >(n) )
#define GETBIT(n,x) (BITS((n)%SIZE, x[(n)/SIZE]))

and now you can say

unsigned int arr[3] = { 0x91, 0x92, 0x93 };

and say GETBIT(n,arr) to get/set n'th bit of arr.

QQ wrote:
Hi I have a unsigned char array, but I need to compress it in the bit
level.
for instance the array is 0x91,0x92,0x93. ...
I need to change it into a bit stream like 10010001 10010010 10010011
...
and operate this stream bit by bit and transform it into another bit
stream,
and convert that bit stream into an unsigned char array.
How can I do it?
Thanks a lot!
Aug 30 '06 #5
yup, the set will be a little different, though, but all the same old
way.

mospehra...@gma il.com wrote:
for example, you can say

#define SIZE 8
#define BITS(n,x) ( ( (x) & (1<<(n)) ) >(n) )
#define GETBIT(n,x) (BITS((n)%SIZE, x[(n)/SIZE]))

and now you can say

unsigned int arr[3] = { 0x91, 0x92, 0x93 };

and say GETBIT(n,arr) to get/set n'th bit of arr.

QQ wrote:
Hi I have a unsigned char array, but I need to compress it in the bit
level.
for instance the array is 0x91,0x92,0x93. ...
I need to change it into a bit stream like 10010001 10010010 10010011
...
and operate this stream bit by bit and transform it into another bit
stream,
and convert that bit stream into an unsigned char array.
How can I do it?
Thanks a lot!
Aug 30 '06 #6
QQ posted:
Hi I have a unsigned char array, but I need to compress it in the bit
level.
for instance the array is 0x91,0x92,0x93. ...
I need to change it into a bit stream like 10010001 10010010 10010011
...
and operate this stream bit by bit and transform it into another bit
stream,
and convert that bit stream into an unsigned char array.
How can I do it?

This might get you started:

#include <stddef.h>
#include <assert.h>
#include <limits.h>
#include <stdio.h>

void PrintBits(void const *const mem,size_t amount_bytes,FI LE *const f)
{
int const assert_dummy =
(assert(!!mem), assert(!!amount _bytes),assert( !!f),0);

char static str[CHAR_BIT + 1] = {0};

char unsigned const *p = (char unsigned const*)mem;

do
{
unsigned const byte_val = *p++;
char *pos = str;

unsigned to_and_with = 1U << CHAR_BIT - 1;

do *pos++ = byte_val & to_and_with ? '1' : '0';
while(to_and_wi th >>= 1);

fprintf(f,str);
} while (--amount_bytes);
}

#define PrintObjBits(p, f) PrintBits((p),s izeof(*p),(f))

int main(void)
{
double long array[4] = {241.126, 632.225, 2662.2523, 23345.2352};

PrintObjBits(&a rray,stdout);

return 0;
}

--

Frederick Gotham
Aug 30 '06 #7
mo*********@gma il.com wrote:
>
for example, you can say

#define SIZE 8
#define BITS(n,x) ( ( (x) & (1<<(n)) ) >(n) )
#define GETBIT(n,x) (BITS((n)%SIZE, x[(n)/SIZE]))

and now you can say

unsigned int arr[3] = { 0x91, 0x92, 0x93 };

and say GETBIT(n,arr) to get/set n'th bit of arr.
Don't top-post. It's rude, and we will not put up with it in this
newsgroup. See the links below.

--
Some informative links:
news:news.annou nce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Aug 30 '06 #8
QQ wrote:
Hi I have a unsigned char array, but I need to compress it in the bit
level.
for instance the array is 0x91,0x92,0x93. ...
I need to change it into a bit stream like 10010001 10010010 10010011
...
and operate this stream bit by bit and transform it into another bit
stream,
and convert that bit stream into an unsigned char array.
How can I do it?
First, you (or I) may misunderstand the problem. Let me change the
values of the array to printable ASCII for explanation purposes.

unsigned char array[] = {'a', 'b', 'c', '\0'};

In decimal this array is {97, 98, 99, 0}.
In hex, {0x01100001, 0x01100010, 0x01100011, 0x00000000}.

None of it is a 'stream' of anything (streams are to do with byte I/O
stuff, not with representations ). There is no 'bit stream'.

The finest granularity of memory objects in C is the byte, usually
unsigned char, and usually eight bits. A value of 0..255 decimal.

The *printf() utilities declared in stdio.h (provided in libc?) provide
means to 'print' (represent as text) the values you might present as
octal, decimal and hexadecimal. But not in Binary. We have to do it
ourselves. That's the bad news. The good news is that it's easy. Check
this out.

#define CHARBITS 8

typedef unsigned char uchar;

void bits(uchar b, int n) {
for (--n; n >= 0; --n)
putchar((b & 1 << n) ? '1' : '0');
putchar(' ');
}

void byte(uchar b) {
bits(b, CHARBITS);
}

If we call byte(x) we print 9 characters, eight of 0 or 1 and a trailing
space. Calling bits() prints the least significant n bits and a trailing
space. If you don't want to put all this to putchar(), direct it as you
will.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Aug 31 '06 #9
Joe Wright wrote:
QQ wrote:
In decimal this array is {97, 98, 99, 0}.
In hex, {0x01100001, 0x01100010, 0x01100011, 0x00000000}.
rather large hex numbers those...

--
Nick Keighley
Infinitely many bits doesn't give you "100% accuracy". You will
only be able to represent the algebraic numbers.
Sure, if you use one of those old-fashioned implementations that only
has aleph-null-bit floating-point. Any decent modern implementation
should provide at least aleph-one bits.
(Bill Pursell and Keith Thompson clc)

Aug 31 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1407
by: Joerg Schuster | last post by:
Hello, I want to open the file 'configuration.smo' that is in directory dir. Yet, I don't know on which os my program is being run. On Unix I would say: f = open(dir + '/configuration.smo', 'r') What is the os-independent version of this line?
20
7561
by: Christian Stigen Larsen | last post by:
A signed int reserves one bit to signify whether a number is positive or negative. In light of this, a colleague asked me whether there existed an int in C++ that was -0, a zero with the negative bit set. I was intrigued by this, so I tried the following code: #include <stdio.h> int main(int, char**) { int a(-0); printf("a=%d\n", a);
27
6798
by: Kevin A | last post by:
Hi, Is there a way to determine the name and version of the operating system in a portable way? (for Solaris/Linux) Thanks, Kevin
3
1304
by: Jordan | last post by:
I am planning to write a very small operating system soon in c. It's going to be just something I am going to do as a hobby and if it get's good maybe go further on it. Anyway I want to know is c still a good language to do this in, or would c++ be a better one? I know c better then c++.
2
1602
by: seash | last post by:
H iam developing my windows form application(ide:visual studio.net 2003, visual c#) on windows 2000 professional operating system , but when i run the exe on Xp operating system, the screen (windows form) of my application gets truncated at the end of the screen i have to make changes to the forms height and width to make it fully visible on the screen iam using infragistics library for some controls on the form, is it the culprit i...
6
1415
by: Matt | last post by:
Hello, How can I write an Operating System in C or C++ -- Mateusz Rajca
5
2075
by: JerryK | last post by:
Hi, I am trying to put a hyperlink to a .cer (certificate) file on a form. Normally (on a ,htm page) when the user clicks on the hyperlink, the file is not recognized and the user is given the option to save the file. This is the desired result. However, when I put the hyperlink on a webform, the file is opened and it's binary contents are displayed on the screen. Does anyone know why this happens? The hyperlink is as follows:
1
2291
by: Jesper | last post by:
Hi, I've made an application that is running in the background - i.e. its not visible on the screen. I would like an eventhandler in the program to subscribe to the event that occurs when, say the windowskey+(some free key) is pressed. A little like the way you can start the file Explorer from windowskey+E, except that I do not want to start an app, but invoke an eventhandler in an already running program.
3
2403
by: PythonUsr | last post by:
Although I know for a fact that an Operating System can be written in Python, I need to ask some questions to the more advanced users of Python. Uuu and Cleese are two operating systems that were / are written in Python. Does anyone use them? If so, how do they function / feel? Do they have a graphical mode and a command line mode, such as Linux does? How hard would it be to write a full blown bootable operating system in Python? With...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.