Thread: Memo Box to Binary

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Memo Box to Binary

    Hi,

    Not sure if this is meant for the windows section or C++ but sorry if it is in the wrong section

    i was wondering if anyone can help me with a problem i have.

    what i want is the user to open a file (this file will only have 1 word on it for now opening the file is not a problem as this is already been done) the file will then open in the memo box.

    now when the user presses a button the 2nd memo box will convert what is in memo 1 into binary i also have a mini binary program but that only looks for what the user inputs.

    can anyone help me?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm pretty sure I could help if I actually understood the question !?!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    ok ill try and explain it a bit better

    1# i will have 2 memo boxes on a form.

    2# 1 of the memo boxes will have a button to load a txt file.

    3# the txt file will then be displayed in memo number 1

    4# there will also be an additional button that the user can press

    5# once this button has been press all the text from the txt file that has been loaded into memo 1 will then convert into binary format and display in memo number 2

    Hope this helps explain what i am trying to achieve a bit more

    at the moment i have managed to do points 1 to 3.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you can't add an additional button, or you can't translate a ascii text into binary [and by this, I suppose you want sets of 8 digits 0..1 that represent each letter in the text?].

    E.g. ABCD would be
    Code:
    01000001 01000010 01000011 01000100
    So give us a sample of what you think this should look like. Or look at "recent posts by Salem" where he gives some examples of how to do binary to text and text to binary - under the subject of "bit stuffing".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    well i want the button to translate what is in memo1 into binary and display it in memo 2. just like you have put ABCD.

    i have a binary program but that just looks at a string that has already been created. this is the code i have for the binary program

    Code:
    int main()
    {
       string name("test");
       vector < bitset<8> > b;
    
       for (int i=0; i<name.length(); i++)
          b.push_back(name[i]);
    
       for (int i=0; i<b.size(); i++)
          cout << b[i] << endl;
    
          cin.get();
    
    }
    but obviously this looks at the string i have entered but i need to to read all the charactures that will be loaded in memo1.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not that familiar with how you get values out of and into different fields in a form - but surely that's not terribly hard?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    well the next thing i have managed to do is transfer the data from one memo to the other one by using this code

    Code:
    Memo2->Lines = Memo1->Lines
    so im just trying to find a way to get it to convert into binary using that button.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you need to use "memo1->lines" as input to the code you have above (instead of "name"), and instead of doing cout of it, create a string of the binary value. You could replace cout with a stringstream and then get the string out of stringstream and put that into memo2->lines.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I'm not sure what your "goal" is, but I suggest you try looking at a Hex Editor. A hex editor will show your file in hexadecimal, and alongside the hex, it will display the characters for any values in the ASCII range. It doesn't know if the value represents an ASCII character, but whenever it sees 41 (hex), it will also display an 'A', etc.

    I assume that a hex editor is not exactly what you want, but I think it will give you some ideas.

    Hexadecimal is frequently used in place of binary, because binary gets hard to read, write, or speak, especially when you are dealing with more than 8 bits. (And, binary isn't built into cin or cout.)

    You can easily learn to convert between hex and binary in your head. You only have to learn 16 conversions, and it does not require any calculations! You already know zero and one, and several of the other bit patterns are easy to learn, because they make "nice patterns". i.e. 0101 (binary) = 5 (hex), and 1010 (binary) = A (hex)... These are easy patterns to remember.


    You simply group the binary data into 4-bit nibbles (or nybbles) like this:

    0000 0000 0000 0101 (binary) = 0005 (hex) = 0005 (decimal)
    0101 0000 0101 0000 (binary) = 5050 (hex) = 20,560 (decimal)
    0101 0101 0101 0101 (binary) = 5555 (hex) = 21,845 (decimal)

    Or, with your "ABCD" example:

    'A' (ASCII) = 0100 0001 (binary) = 41 (hex) = 65 (decimal)
    'B' (ASCII) = 0100 0010 (binary) = 42 (hex) = 66 (decimal)
    'C' (ASCII) = 0100 0011 (binary) = 43 (hex) = 67 (decimal)
    'D' (ASCII) = 0100 0100 (binary) = 44 (hex) = 68 (decimal)

    ...Wherever you see 0100, it converts to 4, whenever you see 0101 it converts to 5... Wherever you see 1010 it converts to A... Wherever you see 0111 it converts to 7, etc. Very easy!

    (I used a calculator for the decimal conversions.)
    Last edited by DougDbug; 11-14-2007 at 05:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM