473,756 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to put 8 "int" => 10100010 into one character of type "char"

I try to put 8 int bit for example 10100010 into one character of type
char(1 octet) with no hope . Could anyone propose a simple way to do
it? Thank you very much.
Jun 27 '08
13 2808
CBFalconer wrote:
What gives you the idea that Anna speaks French?
Her email address petitmuoton?
Jun 27 '08 #11
CBFalconer wrote:
viza wrote:
>Anna wrote:
>>Ben Bacarisse <ben.use...@bsb .me.ukwrote:
Anna <petitmou...@gm ail.comwrites:

I try to put 8 int bit for example 10100010 into one character
of type char(1 octet) with no hope . Could anyone propose a
simple way to do it? Thank you very much.
It would help is we saw what you did. I would write:

unsigned char c = 0xA2; /* Hex A2 is 1010 0010 */

Not that unsigned char is almost always safer for this sort of
thing.
thank you Ben. I have bit sequence 01010101.... that I want to
implement into a simulator, but in the simulator, they use a
char as a type of data since each int comprise between 2-4 octet
and one char is only 1 octet (=8 bits 0 or 1). I guess it will
make the simulator run faster, I'm not sure.
Do you mean that you have eight integers but that each one has
either the value 1 or 0? In that case, do:

Est-ce que vous-voulez dire que vous avez huite integers et que
chaqun d'entre eux a le valeur 1 ou 0? En ce cas, faitez:

int integers[8];
unsigned char character;

character= ( integers[0] ? (1 << 0) : 0 )
| ( integers[1] ? (1 << 1) : 0 )
| ( integers[2] ? (1 << 2) : 0 )
| ( integers[3] ? (1 << 3) : 0 )
| ( integers[4] ? (1 << 4) : 0 )
| ( integers[5] ? (1 << 5) : 0 )
| ( integers[6] ? (1 << 6) : 0 )
| ( integers[7] ? (1 << 7) : 0 );

Actually this works if the integers have the value 0 or non-0.
If you are certain that they can only be 1 or 0, the following
may be faster:

Ceci marche si les integers ont les valeurs 0 ou non-0. Si vous
etez certain que ils n'ont que 1 ou 0, le ci-dessus peut-etre
plus vite:

character= (integers[0] << 0)
| ( integers[1] << 1)
| ( integers[2] << 2)
| ( integers[3] << 3)
| ( integers[4] << 4)
| ( integers[5] << 5)
| ( integers[6] << 6)
| ( integers[7] << 7);

What gives you the idea that Anna speaks French?
Calling herself 'petitmouton' is a clue.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 27 '08 #12
On 15 juin, 13:33, viza <tom.v...@gmil. comwrote:
Hi

On Sun, 15 Jun 2008 03:00:50 -0700, Anna wrote:
On 15 juin, 11:52, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
Anna <petitmou...@gm ail.comwrites:
I try to put 8 int bit for example 10100010 into one character of
type char(1 octet) with no hope . Could anyone propose a simple way
to do it? Thank you very much.
It would help is we saw what you did. I would write:
unsigned char c = 0xA2; /* Hex A2 is 1010 0010 */
Not that unsigned char is almost always safer for this sort of thing.
thank you Ben. I have bit sequence 01010101.... that I want to implement
into a simulator, but in the simulator, they use a char as a type of
data since each int comprise between 2-4 octet and one char is only 1
octet (=8 bits 0 or 1). I guess it will make the simulator run faster,
I'm not sure.

Do you mean that you have eight integers but that each one has either the
value 1 or 0? In that case, do:

Est-ce que vous-voulez dire que vous avez huite integers et que chaqun
d'entre eux a le valeur 1 ou 0? En ce cas, faitez:

int integers[8];
unsigned char character;

character= ( integers[0] ? (1 << 0) : 0 )
| ( integers[1] ? (1 << 1) : 0 )
| ( integers[2] ? (1 << 2) : 0 )
| ( integers[3] ? (1 << 3) : 0 )
| ( integers[4] ? (1 << 4) : 0 )
| ( integers[5] ? (1 << 5) : 0 )
| ( integers[6] ? (1 << 6) : 0 )
| ( integers[7] ? (1 << 7) : 0 );

Actually this works if the integers have the value 0 or non-0. If you
are certain that they can only be 1 or 0, the following may be faster:

Ceci marche si les integers ont les valeurs 0 ou non-0. Si vous etez
certain que ils n'ont que 1 ou 0, le ci-dessus peut-etre plus vite:

character= (integers[0] << 0)
| ( integers[1] << 1)
| ( integers[2] << 2)
| ( integers[3] << 3)
| ( integers[4] << 4)
| ( integers[5] << 5)
| ( integers[6] << 6)
| ( integers[7] << 7);

HTH
viza
Bonjour Viza,
Bien joué, je parle français aussi :-) merce beaucoup pour ta réponse.
Je trouve très intéressant et je pense que je vais essayer
d'implementer ce ci sur mon programme. Merci, merci, merci
Anna

Jun 27 '08 #13
On 15 juin, 13:33, viza <tom.v...@gmil. comwrote:
Hi

On Sun, 15 Jun 2008 03:00:50 -0700, Anna wrote:
On 15 juin, 11:52, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
Anna <petitmou...@gm ail.comwrites:
I try to put 8 int bit for example 10100010 into one character of
type char(1 octet) with no hope . Could anyone propose a simple way
to do it? Thank you very much.
It would help is we saw what you did. I would write:
unsigned char c = 0xA2; /* Hex A2 is 1010 0010 */
Not that unsigned char is almost always safer for this sort of thing.
thank you Ben. I have bit sequence 01010101.... that I want to implement
into a simulator, but in the simulator, they use a char as a type of
data since each int comprise between 2-4 octet and one char is only 1
octet (=8 bits 0 or 1). I guess it will make the simulator run faster,
I'm not sure.

Do you mean that you have eight integers but that each one has either the
value 1 or 0? In that case, do:

Est-ce que vous-voulez dire que vous avez huite integers et que chaqun
d'entre eux a le valeur 1 ou 0? En ce cas, faitez:

int integers[8];
unsigned char character;

character= ( integers[0] ? (1 << 0) : 0 )
| ( integers[1] ? (1 << 1) : 0 )
| ( integers[2] ? (1 << 2) : 0 )
| ( integers[3] ? (1 << 3) : 0 )
| ( integers[4] ? (1 << 4) : 0 )
| ( integers[5] ? (1 << 5) : 0 )
| ( integers[6] ? (1 << 6) : 0 )
| ( integers[7] ? (1 << 7) : 0 );

Actually this works if the integers have the value 0 or non-0. If you
are certain that they can only be 1 or 0, the following may be faster:

Ceci marche si les integers ont les valeurs 0 ou non-0. Si vous etez
certain que ils n'ont que 1 ou 0, le ci-dessus peut-etre plus vite:

character= (integers[0] << 0)
| ( integers[1] << 1)
| ( integers[2] << 2)
| ( integers[3] << 3)
| ( integers[4] << 4)
| ( integers[5] << 5)
| ( integers[6] << 6)
| ( integers[7] << 7);

HTH
viza
Bonjour Viza,
Oui, je parle français aussi :-) merci beaucoup pour ton aide. Ton
programme me semble très intéressant, je vais essayer d'implementer ce
ci sur le simulateur. Merci beaucoup.
Anna
Jun 27 '08 #14

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

Similar topics

134
9060
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
5
14116
by: Omats.Z | last post by:
what is meaning os "char **option meet"? I get a function like this: __________________________________ int getchoice(char *greet, char *choices) { int chosen = 0; int selected; char **option; // what is this line mean? do { printf("Choice: %s\n", greet);
34
2687
by: arnuld | last post by:
what is the difference between these 2: char name = "hackers"; char* name = "hackers";
20
3564
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
14
307
by: Anna | last post by:
I try to put 8 int bit for example 10100010 into one character of type char(1 octet) with no hope . Could anyone propose a simple way to do it? Thank you very much.
0
9455
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
10031
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
9869
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
9838
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,...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
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
3354
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.