char to binary conversion

This is a discussion on char to binary conversion within the C++ Programming forums, part of the General Programming Boards category; hello, I have a question about how to convert a single character to its binary equivalent. I have find the ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287

    char to binary conversion

    hello, I have a question about how to convert a single character to its binary equivalent. I have find the way to do it for integer numbers:

    Code:
    int c=8;
    while(c>0) {cout<<c%2;c=c/2;}
    to show the 'reversed' binary. also I have find the way to do it with plain characters, only get its ascii and do the same as before, so:

    Code:
    int bin[8];
    int pe;
    char c='a';
    printf("dec:%d\n",c);
    pe=7;
    while(c>0)
        {
        bin[pe]=c%2;
        c=c/2;
        pe--;
        }
    for(int np=pe;np>=0;np--) {bin[np]=0;}
    printf("bin:");
    for(int q=0;q<8;q++) {printf("%d",bin[q]);}
    printf("\n");
    but how can I convert an accentuated character to its binary representation? the method of convert its ascii number does not work here. also seems not to work here the loop though a
    while(c<0)
    mutiplying instead '%'.
    How can I do it?
    Thank's in advance
    Niara

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    A bitset is very easy to use:

    Code:
    #include <bitset>
    #include <iostream>
    
    ...
    
    char c = 'A';
    
    // Convert character into bitset
    std::bitset<8> bits(static_cast<unsigned long>(c));
    
    // Display bitset
    std::cout << bits << std::endl;
    On my machine this outputs: 01000001
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    http://cboard.cprogramming.com/searc...earchid=218529

    You'll note that this is quite a frequent subject with many solutions. Filter through the binary search tree and binary file writing results and that search will have many helpful replies with example source you might like.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    hello both, thank's for your time and help.
    hk_mp5kpdw: that function gives exactly what I was trying to do, so I will try to find a bit more info about related functions and functionality.
    Tonto: yes, I never used the searcher; so I'll be to take into account before ask new problems, I know that a previous search can help me on orientating my doubts to some exact interest points.
    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  2. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21