Convert Char to binary
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: Convert Char to binary

  1. #1
    Join Date
    Nov 2005
    Posts
    7

    Convert Char to binary

    anyone know how to convert char to binary?

  2. #2
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Convert Char to binary

    Quote Originally Posted by preci0usGal
    anyone know how to convert char to binary?
    Not sure what you mean... any data a digital computer deals with is always binary, and so are the contents of a char variable.

  3. #3
    Join Date
    Nov 2005
    Posts
    7

    Re: Convert Char to binary

    i am trying to send hex and receive hex... as in i can display and see it in hex. but not in char...

  4. #4
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Convert Char to binary

    Quote Originally Posted by preci0usGal
    i am trying to send hex and receive hex... as in i can display and see it in hex. but not in char...
    It is still unclear what you mean by "hex" and "char". Hexadecimal is a base which can be used for displaying a numerical value as a character string - just as octal, decimal or binary. Hex is not a data type. As said above, all numbers are stored and treated in binary form in a digital computer - by its very nature. 'char', OTOH, is a data type of the C/C++ programming langue, which is always 8 bits (or one byte). The numerical value it contains can be displayed (by formatting it accordingly) as a number to any base - such as binary, octal, decimal or hex, or it can be treated as an index into a character set of a specific code (such as ASCII) and be displayed as that specific character.
    However, "converting from char to binary" or "hex to char" doesn't make any sense: One is a a base, the other is a data type.

  5. #5
    Join Date
    Nov 2005
    Location
    Thrissur
    Posts
    96

    Angry Re: Convert Char to binary

    Use Binary Operators and store them in an array.

    jUST LIKE THIS

    int nNumA=8; // beginning value is 8

    nNumA >> 2; // ending value is 2


    0000 1000

    IS CONVERTED TO

    0000 0010


    I THINK that will help you.

  6. #6
    Join Date
    May 2005
    Posts
    399

    Re: Convert Char to binary

    Quote Originally Posted by preci0usGal
    i am trying to send hex and receive hex... as in i can display and see it in hex. but not in char...
    This is how I interpret your problem, unless you care to explain in more details.
    You are sending a single byte value and trying to display it on the screen. A single byte value (0-255 hex) has an ascii representation and so you will be only able to see the ascii value.
    For e.g. if you send single byte 0x41, then you will be able to see the letter A on your screen. Try performing an atoi() on that value you receive and then display it on screen. This will give you the decimal value.
    You can use sscanf() to perform type conversions also.

  7. #7
    Join Date
    Jun 2005
    Location
    Chennai , India
    Posts
    1,375

    Thumbs up Re: Convert Char to binary

    this might help u ...
    Code:
    string hexData = "AABBCCDD01234567";
    string binData = Conversion.HexToBin(hexData);
    and possible check thi slink .. can give u some idea !!

    Hexadecimal, Binary, and Decimal conversions


    if a post helps.. dont forget to "Rate this post"
    It takes seconds for rating…that actually compensates the minutes taken for giving answers
    The biggest guru-mantra is: Never share your secrets with anybody. It will destroy you.
    Regards, Be generous->Rate people
    Jayender!!

  8. #8
    Join Date
    Nov 2005
    Posts
    7

    Re: Convert Char to binary

    Quote Originally Posted by leojose
    This is how I interpret your problem, unless you care to explain in more details.
    You are sending a single byte value and trying to display it on the screen. A single byte value (0-255 hex) has an ascii representation and so you will be only able to see the ascii value.
    For e.g. if you send single byte 0x41, then you will be able to see the letter A on your screen. Try performing an atoi() on that value you receive and then display it on screen. This will give you the decimal value.
    You can use sscanf() to perform type conversions also.
    hi there... i tried... currently. using component of mscomm...
    when i use 0x41 hex, i nv get A...instead, i got 3F....

    can help?? tell me how am i gg to do it... to get A i mean...

    btw, wats atoi()??

    do u haf any sample that i can refer to??
    Last edited by preci0usGal; November 28th, 2005 at 12:41 AM.

  9. #9
    Join Date
    May 2005
    Posts
    399

    Re: Convert Char to binary

    Quote Originally Posted by preci0usGal
    hi there... i tried... currently. using component of mscomm...
    when i use 0x41 hex, i nv get A...instead, i got 3F....
    Did you transmit a single byte as in
    BYTE ValueA = (BYTE)0x41;

  10. #10
    Join Date
    Nov 2005
    Posts
    7

    Re: Convert Char to binary

    i put it this way...

    <pre>
    CString test = 0x50;
    m_comm.SetOutput(COleVariant(test));


    Sleep(1000);

    VARIANT var;

    var = m_comm.GetInput();


    CString aa(var.bstrVal);

    char CBuff[100];

    sprintf(CBuff, "%x", aa.GetAt(aa.GetLength()-2));
    MessageBox(CBuff);

    </pre>

  11. #11
    Join Date
    May 2005
    Posts
    399

    Re: Convert Char to binary

    sorry..can't help you out with an MFC application... not my domain...

    See if you can find something useful here
    http://www.codeguru.com/forum/showthread.php?t=350757

  12. #12
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Convert Char to binary

    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  13. #13
    Join Date
    Nov 2005
    Posts
    7

    Re: Convert Char to binary

    for the program that i have written, when the input mode is 0 which is text. the correct format and answer appear. but when i change it to binary, which is 1. it is wrong. how can i change it to the correct answer??

  14. #14
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Convert Char to binary

    Quote Originally Posted by preci0usGal
    for the program that i have written, when the input mode is 0 which is text. the correct format and answer appear. but when i change it to binary, which is 1. it is wrong. how can i change it to the correct answer??
    Sorry, but what you are saying is rather cryptic. Which input mode, and which answer? Please give more details about your problem - possibly with source code, and with examples of your data: Both how you expect it to be, and what you actually get.

  15. #15
    Join Date
    Sep 2003
    Location
    M...N
    Posts
    220

    Re: Convert Char to binary

    Quote Originally Posted by preci0usGal
    anyone know how to convert char to binary?
    simply:

    int nResult = (int) chSource;

    and then you can transfer nResult from decimal to binary.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center