473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to convert char to binary?

Hi all,

I was wondering if there is any lib or function could do the following
things:

input: a char
output: 8 digits binary presentation for that given char

Example:

-------------------------
input: k
Hex=6B <-ASCII table 'k'
Binary = 0110 1011

output: 0110 1011
done
-------------------------
input: K
Hex=4B <-ASCII table 'k'
Binary = 0100 1011

output: 0100 1011
done

--------------------------

(I am only dealing with the ASCII printable chars,no UNICODE,no non-
printables)

Am I clear with the question?
I want to make it standard and system independent(no windows/linux system
calls).
The only solution I came up with is to have a pre-computed table.Is there
any easier way of doing this ?


Thanks a lot in advance
Joseph
Jul 22 '05 #1
5 25613

"Joseph" <wu*******@hotmail.com> wrote in message
news:Xn***********************@203.109.252.31...
Hi all,

I was wondering if there is any lib or function could do the following
things:

input: a char
output: 8 digits binary presentation for that given char

Example:

-------------------------
input: k
Hex=6B <-ASCII table 'k'
Binary = 0110 1011

output: 0110 1011
done
-------------------------
input: K
Hex=4B <-ASCII table 'k'
Binary = 0100 1011

output: 0100 1011
done

--------------------------

(I am only dealing with the ASCII printable chars,no UNICODE,no non-
printables)

Am I clear with the question?
I want to make it standard and system independent(no windows/linux system
calls).
The only solution I came up with is to have a pre-computed table.Is there
any easier way of doing this ?


Simplest way is to use bit manipulation, but it isn't the only way.

#include <iostream>
#include <limits.h>

void print_char_as_binary(char ch)
{
int i = CHAR_BIT;
while (i > 0)
{
-- i;
std::cout << (ch&(1 << i) ? '1' : '0');
}
}

Untested code.

john
Jul 22 '05 #2

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2s*************@uni-berlin.de...
|
| "Joseph" <wu*******@hotmail.com> wrote in message
| news:Xn***********************@203.109.252.31...
| > Hi all,
| >
| > I was wondering if there is any lib or function could do the following
| > things:
| >
| > input: a char
| > output: 8 digits binary presentation for that given char
| >
| > Example:
| >
| > -------------------------
| > input: k
| > Hex=6B <-ASCII table 'k'
| > Binary = 0110 1011
| >
| > output: 0110 1011
| > done
| > -------------------------
| > input: K
| > Hex=4B <-ASCII table 'k'
| > Binary = 0100 1011
| >
| > output: 0100 1011
| > done
| >
| > --------------------------
| >
| > (I am only dealing with the ASCII printable chars,no UNICODE,no non-
| > printables)
| >
| > Am I clear with the question?
| > I want to make it standard and system independent(no windows/linux system
| > calls).
| > The only solution I came up with is to have a pre-computed table.Is there
| > any easier way of doing this ?
|
| Simplest way is to use bit manipulation, but it isn't the only way.
|
| #include <iostream>
| #include <limits.h>
|
| void print_char_as_binary(char ch)
| {
| int i = CHAR_BIT;
| while (i > 0)
| {
| -- i;
| std::cout << (ch&(1 << i) ? '1' : '0');
| }
| }

Simplest ? :-)

// ...
# include <bitset>
int main()
{
std::cout << std::bitset<CHAR_BIT>( 'K' )
<< std::endl;

return 0;
}

Cheers.
Chris Val
Jul 22 '05 #3
"Chris \( Val \)" <ch******@bigpond.com.au> wrote in
news:2s*************@uni-berlin.de:

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2s*************@uni-berlin.de...
|
| "Joseph" <wu*******@hotmail.com> wrote in message
| news:Xn***********************@203.109.252.31...
| > Hi all,
| >
| > I was wondering if there is any lib or function could do the
| > following things:
| >
| > input: a char
| > output: 8 digits binary presentation for that given char
| >
| > Example:
| >
| > -------------------------
| > input: k
| > Hex=6B <-ASCII table 'k'
| > Binary = 0110 1011
| >
| > output: 0110 1011
| > done
| > -------------------------
| > input: K
| > Hex=4B <-ASCII table 'k'
| > Binary = 0100 1011
| >
| > output: 0100 1011
| > done
| >
| > --------------------------
| >
| > (I am only dealing with the ASCII printable chars,no UNICODE,no
| > non- printables)
| >
| > Am I clear with the question?
| > I want to make it standard and system independent(no windows/linux
| > system calls).
| > The only solution I came up with is to have a pre-computed table.Is
| > there any easier way of doing this ?
|
| Simplest way is to use bit manipulation, but it isn't the only way.
|
| #include <iostream>
| #include <limits.h>
|
| void print_char_as_binary(char ch)
| {
| int i = CHAR_BIT;
| while (i > 0)
| {
| -- i;
| std::cout << (ch&(1 << i) ? '1' : '0');
| }
| }

Simplest ? :-)

// ...
# include <bitset>
int main()
{
std::cout << std::bitset<CHAR_BIT>( 'K' )
<< std::endl;

return 0;
}

Cheers.
Chris Val


Thank u 2,and sorry that I forgot to post another question

how can I do it vice versa? from binary to char?


Thanks again!!!
Jul 22 '05 #4
"Joseph" <wu*******@hotmail.com> wrote in message
news:Xn***********************@203.109.252.31...
"Chris \( Val \)" <ch******@bigpond.com.au> wrote in
news:2s*************@uni-berlin.de:

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2s*************@uni-berlin.de...
|
| "Joseph" <wu*******@hotmail.com> wrote in message
| news:Xn***********************@203.109.252.31...
| > Hi all,
| >
| > I was wondering if there is any lib or function could do the
| > following things:
| >
| > input: a char
| > output: 8 digits binary presentation for that given char
| >
| > Example:
| >
| > -------------------------
| > input: k
| > Hex=6B <-ASCII table 'k'
| > Binary = 0110 1011
| >
| > output: 0110 1011
| > done
| > -------------------------
| > input: K
| > Hex=4B <-ASCII table 'k'
| > Binary = 0100 1011
| >
| > output: 0100 1011
| > done
| >
| > --------------------------
| >
| > (I am only dealing with the ASCII printable chars,no UNICODE,no
| > non- printables)
| >
| > Am I clear with the question?
| > I want to make it standard and system independent(no windows/linux
| > system calls).
| > The only solution I came up with is to have a pre-computed table.Is
| > there any easier way of doing this ?
|
| Simplest way is to use bit manipulation, but it isn't the only way.
|
| #include <iostream>
| #include <limits.h>
|
| void print_char_as_binary(char ch)
| {
| int i = CHAR_BIT;
| while (i > 0)
| {
| -- i;
| std::cout << (ch&(1 << i) ? '1' : '0');
| }
| }

Simplest ? :-)

// ...
# include <bitset>
int main()
{
std::cout << std::bitset<CHAR_BIT>( 'K' )
<< std::endl;

return 0;
}

Cheers.
Chris Val


Thank u 2,and sorry that I forgot to post another question

how can I do it vice versa? from binary to char?


std::bitset<CHAR_BIT> bs("101010");

char c(char(bs.to_ulong())); /* 'c' now has value of 42 */

Methinks you need a book or two. :-)

-Mike
Jul 22 '05 #5
Joseph wrote:

Hi all,

I was wondering if there is any lib or function could do the following
things:

input: a char
output: 8 digits binary presentation for that given char

Example:

-------------------------
input: k
Hex=6B <-ASCII table 'k'
Binary = 0110 1011

output: 0110 1011
done
-------------------------
input: K
Hex=4B <-ASCII table 'k'
Binary = 0100 1011

output: 0100 1011
done

--------------------------

(I am only dealing with the ASCII printable chars,no UNICODE,no non-
printables)

Am I clear with the question?
I want to make it standard and system independent(no windows/linux system
calls).
The only solution I came up with is to have a pre-computed table.Is there
any easier way of doing this ?

Thanks a lot in advance
Joseph

Below is a copy of a program that contains
a method that prints a char variable in binary form.
That may help.

--
Mr. (Dr.) Kari Laitinen
Oulu Institute of Technology, Finland
http://www.naturalprogramming.com/
// double_to_binary.cpp (c) 1998-2004 Kari Laitinen

// This program is a modified version of a program
// in a C++ book. More information at
// http://www.naturalprogramming.com/cppbook.html
#include <iostream.h>

void print_in_binary_form( char given_byte )
{
unsigned char bit_mask = 0x80 ;
unsigned char one_bit_in_given_byte ;

for ( int bit_counter = 0 ;
bit_counter < 8 ;
bit_counter ++ )
{
one_bit_in_given_byte = given_byte & bit_mask ;

if ( one_bit_in_given_byte == 0 )
{
cout << "0" ;
}
else
{
cout << "1" ;
}

bit_mask = bit_mask >> 1 ;
}
}
void print_in_binary_form( int given_integer )
{
// This program works only with 32-bit int variables.
// To make this program work with 16-bit int variables,
// you should use initial mask 0x8000 and let the loop
// be executed only 16 times.

unsigned int bit_mask = 0x80000000 ;
unsigned int one_bit_in_given_integer ;

for ( int bit_counter = 0 ;
bit_counter < 32 ;
bit_counter ++ )
{
one_bit_in_given_integer = given_integer & bit_mask ;

if ( one_bit_in_given_integer == 0 )
{
cout << "0" ;
}
else
{
cout << "1" ;
}

bit_mask = bit_mask >> 1 ;
}
}
int main()
{
cout << "\n\n" ;

double test_number = 123.456 ;

char* byte_in_test_number = (char*) &test_number ;

for ( int byte_counter = 0 ;
byte_counter < sizeof( double ) ;
byte_counter ++ )
{
cout << " " ;
print_in_binary_form( *byte_in_test_number ) ;
byte_in_test_number ++ ;
}

cout << "\n\n" ;

cout << "\n The following is not correct: \n" ;

int* first_four_bytes = (int*) &test_number ;
int* last_four_bytes = first_four_bytes + 1 ;

print_in_binary_form( *first_four_bytes ) ;

cout << " " ;

print_in_binary_form( *last_four_bytes ) ;

}
Jul 22 '05 #6

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

Similar topics

19
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
19
by: Vincent | last post by:
Hi all, I want to convert a char (binary) to an unsigned long. How can I do this? Thanks, Vincent
7
by: Golan | last post by:
Hi, I need to convert a Binary value to Decimal. I've been told that the value is an unsigned one. How can I do this? I use memcpy into an unsigned char variable, but when I print the value I got...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
16
by: Dave | last post by:
Hi all, I have a 4 byte char array with the binary data for two 16-bit signed integers in it like this: Index 3 2 1 0 Data Bh Bl Ah Al Where Bh is the high byte of signed 16-bit...
24
by: cedarson | last post by:
I am having trouble writing a simple code that will convert an int ( 487 for example ) to binary form just for the sake of printing the binary. Can someone please help? Thanks!
13
by: HNT20 | last post by:
Hello All i am new to python language. i am working on a gnuradio project where it uses python as the primary programming language. i am trying to convert a message, text, or numbers into binary...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
29
by: Harlin Seritt | last post by:
Hi... I would like to take a string like 'supercalifragilisticexpialidocius' and write it to a file in binary forms -- this way a user cannot read the string in case they were try to open in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.