how to convert char to binary?

J

Joseph

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
 
J

John Harrison

Joseph said:
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
 
C

Chris \( Val \)

|
| | > 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
 
J

Joseph

|
| | > 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!!!
 
M

Mike Wahler

Joseph said:
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
 
K

Kari Laitinen

Joseph said:
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 ) ;

}
 
Joined
Jul 6, 2007
Messages
1
Reaction score
0
I just edited someone's (up above me) and made this. I don't take credit for anything they did, as I just added to it. It onl goes up to 8 characters, but you can change that:

#include <iostream>
#include <bitset>
using namespace std;

int main (int argc, char * const argv[]) {
char ch[8];
char str[8][8];
int z = 0;
int x;
int i;
cout << "Enter a string less than 8 characters.\n";
cin >> ch;
while(z <8){
i = CHAR_BIT;
x = 0;
while(i>0){
-- i;
str[x][z] = (ch[z]&(1 << i) ? '1' : '0');
std::cout << str[x][z];
x++;
}
z++;
std::cout << "\n";
}
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top