Why is it difrent but when I check its the same?

This is a discussion on Why is it difrent but when I check its the same? within the Windows Programming forums, part of the Platform Specific Boards category; refer to these 2 pieces of code <1> h[0] = (OLECHAR) 72; h[1] = (OLECHAR) 69; h[2] = (OLECHAR)76; h[3] ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    161

    Why is it difrent but when I check its the same?

    refer to these 2 pieces of code
    <1>
    h[0] = (OLECHAR) 72;
    h[1] = (OLECHAR) 69;
    h[2] = (OLECHAR)76;
    h[3] = h[2];
    h[4] = (OLECHAR) 111;
    h[5] = NULL;

    <2>
    h[0] = (OLECHAR) "H";
    h[1] = (OLECHAR) "E";
    h[2] = (OLECHAR)"L";
    h[3] = h[2];
    h[4] = (OLECHAR) "o";
    h[5] = NULL;

    When I convert sample 1 to BSTR then send it to VB I get "HELLo" at the other end. When I convert sample 2 to BSTR then send it to vb. I get some thing like "????". And when I check "H" and 72 and the rest in C++ its the same. Why is it like that?

    Thanx in advance!
    Last edited by Benzakhar; 12-12-2003 at 07:21 PM.

  2. #2
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    Probably because VB is not as efficient as C++ and was created by Microsoft :P

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    What company created C++?

    Microsoft hides to many things. Once I started learning C++ I learned that Microsoft hides alot more things lol. But this is the strangest thing. In c++ (OLECHAR) 72 = (OLECHAR) "H" but in VB (OLECHAR) 72 = "H" and (OLECHAR) "H"= "?". It makes no sense? You can check if you want to.

    STDMETHODIMP CTEST_ATL::get_Text(BSTR *pVal)
    {
    // TODO: Add your implementation code here
    //OLECHAR *h;
    OLECHAR *h = new OLECHAR[5];

    h[0] = (OLECHAR) "H";
    h[1] = (OLECHAR) "E";
    h[2] = (OLECHAR)"L";
    h[3] = h[2];
    h[4] = (OLECHAR) "o";
    h[5] = NULL;

    SysFreeString(*pVal);
    *pVal = SysAllocString(h);
    //MessageBox(0,h,"caption",0);
    //*pVal = (BSTR) h;
    return S_OK;
    }

    Do that then create an ole dll. put something like stringvariable = Test_ATL.Text
    msgbox s
    and you get "????"
    replace the characters in " " with its character code and you get what you want. Have a comparisan using if statements in C++ and its the same thing?
    Last edited by Benzakhar; 12-12-2003 at 07:59 PM.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    I think I fiqured it out. Create a new win32 console project and add this to the main() function;

    int i;
    i = (int) 'H';
    cout << i << '\n';
    i = (int) "H";
    cout << i;
    cin >> i;
    return 0;

    it compares 'H' and "H". The results are difrent. Why is 'H' and "H" is difrent?

    Thanx in advance!

  5. #5
    King of the Internet Fahrenheit's Avatar
    Join Date
    Oct 2001
    Posts
    128
    'H' is the ASCII number of the letter H.
    "H" is a string containing H and \0.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>"H" is a string containing H and \0.
    And it will return a pointer to the location of the first character of the string in memory.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    If you would create a pointer and browse around "H" you would see it is difrent from 'H' and its more than 2 chars one H and the other NULL. If you would compile this;
    char h;
    h = "H";
    you would recieve the error "Cannot convert char[2] to char". There it say's 2 thus its 3 bytes, there is char[0] char[1] and char[2] then there is more then a null - terminated string. Save it to a long variable for storage to browse around it.

    char *i = new char;
    long ii;
    ii = (long) "H";
    i = (char *) &ii;
    cout << *i << '\n';
    i++;
    cout << *i << '\n';
    i++;
    cout << *i << '\n';
    cout << ii << '\n';
    cout << "H";
    cin >> i;
    here is something that browses around the "H". If you would take a look you would see that the first byte is a symbol the next is the letter p then a capitle B. and if you put cout << "H"; or cout << 'H'; its the same thing. The mysteries of strings.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    What's a mystery to me is how 2 can equal 3.

    gg

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    An olechar is a unicode character so you could do something like this:
    Code:
    h[0] = L'H';
    LPOLESTR myOleStr = L"Hello";
    or
    LPOLESTR myOleStr = OLESTR("Hello");
    or
    OLECHAR myOleStr[] = L"Hello";
    Also, unless pVal is an [in,out] parameter you should not free its contents.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. How can i check a directory for certain files?
    By patrioticpar883 in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:27 PM
  3. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  4. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM

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