UINT

Can't convert char * to UINT

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 6129 Views - Last Post: 15 June 2010 - 11:51 AM Rate Topic: -----

#1 m4unot  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 163
  • Joined: 05-June 10

UINT

Posted 15 June 2010 - 04:21 AM

i'm using: C++

and couldn't find a way to convert char * to UINT

like this:



// From char *

char * test = ("MB_OK");


UINT convert = (test); // To unsigned int




Tried so many things but no luck ...

what i'm doing wrong?

can someone help me`?

Show me how to do it?

This post has been edited by m4unot: 15 June 2010 - 04:28 AM


Is This A Good Question/Topic? 0
  • +

Replies To: UINT

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: UINT

Posted 15 June 2010 - 05:15 AM

Exactly what do you mean by "convert"? A char* is a pointer (a memory address). If you simply want to see its value, just cast it like this
    char* x;
    cout << reinterpret_cast<uint>(x) << endl;



If that's not what you wanted to do, please try to give a clearer explanation.
Was This Post Helpful? 0
  • +
  • -

#3 Banfa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 109
  • Joined: 07-June 10

Re: UINT

Posted 15 June 2010 - 05:45 AM

Of course nowadays you need to be aware that on a 64bit platform pointers are normally 64bits but a UINT which is an integer is often only 32bits so the value you get from such a cast on a 64bit platform may well not be the actual pointer value.
Was This Post Helpful? 0
  • +
  • -

#4 m4unot  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 163
  • Joined: 05-June 10

Re: UINT

Posted 15 June 2010 - 06:42 AM

i'm trying to make this function works:


void NewMessage(char * get)
{


MessageBox(0, L"Hello", L"Dreamincode", "Info", get);


}







So i can diced what type of messagebox button should have...


is there a way to convert char * to UINT ?

like:



void NewMessage(char * get)
{


UINT convert = (get);

MessageBox(0, L"Hello", L"Dreamincode", "Info", convert);


}




This post has been edited by m4unot: 15 June 2010 - 06:50 AM

Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • Joined: 18-February 07

Re: UINT

Posted 15 June 2010 - 06:51 AM

What do you want the call to void NewMessage(char * get) to look like?

This: NewMessage("MB_OK");


There is no way in general to convert the string "MB_OK" into a numeric value directly in C/C++ -- you would need to create a function to do that for you. see the strcmp() function for how to compare strings.
Was This Post Helpful? 0
  • +
  • -

#6 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: UINT

Posted 15 June 2010 - 06:51 AM

Why would you want to do that? The fourth parameter of MessageBox represents one of the most important part of this function. Quote from MSDN, regarding this:

Quote

The contents and behavior of the dialog box. This parameter can be a combination of flags from the following groups of flags.

To indicate the buttons displayed in the message box, specify one of the following values.

And see this link for more information regarding fourth parameter. So why not using something like this:
void NewMessage(unsigned int type)
{
   MessageBox(0, L"Hello", L"Dreamincode", L"Info", type);
}


and call the function with whatever type you want:
// Now you have created a message box with an OK button.
NewMessage(MB_OK);


Was This Post Helpful? 0
  • +
  • -

#7 m4unot  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 163
  • Joined: 05-June 10

Re: UINT

Posted 15 June 2010 - 07:00 AM

Sorry: 'MB_OK' : undeclared identifier

i'm using my own static library.. that why i dont'

#include <Windows.h> in my main.cpp


and that why i whant to do

NewMessage("MB_OK");

and not NewMessage(MB_OK);

This post has been edited by m4unot: 15 June 2010 - 07:01 AM

Was This Post Helpful? 0
  • +
  • -

#8 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: UINT

Posted 15 June 2010 - 07:04 AM

Then you need to create a function, as Nick stated. Pass a cstring to your function, and compare it with "MB_OK" (see strcmp link from Nick's post). If they equal, you would return the numeric value of MB_OK, which is 0x00000000L. See this for more details.

This post has been edited by sarmanu: 15 June 2010 - 07:05 AM

Was This Post Helpful? 0
  • +
  • -

#9 Banfa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 109
  • Joined: 07-June 10

Re: UINT

Posted 15 June 2010 - 07:42 AM

Or more efficiently create you own enum of allowed values pass that and convert using a switch statement

enum m4unot_BoxType
{
    m4unot_OK,
    m4unot_ERROR
// etc
};

void NewMessage(m4unot_BoxType type)
{
    UINT winType = MB_OK;

    switch(type)
    {
    default:
    case m4unot_OK:
        winType = MB_OK;
        break;
       
    case m4unot_ERROR:
        winType = MB_ERROR;
        break;

// etc
   }

   MessageBox(0, L"Hello", L"Dreamincode", L"Info", winType);
}



Strings are for communicating with people not passing parameter values around a computer program.
Was This Post Helpful? 0
  • +
  • -

#10 m4unot  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 163
  • Joined: 05-June 10

Re: UINT

Posted 15 June 2010 - 07:58 AM

Nice that's cool!

thank you :wheelchair:

I made the Messagebox code if someone need:



void NewMessage(char* one, char* two, char* type)
{

int size_two = strlen(one), size = strlen(two);

TCHAR unicode_string_one[30], unicode_string_two[30];


size_t length = 0;

mbstowcs_s(&length, unicode_string_one, one, size+1);

mbstowcs_s(&length, unicode_string_two, two, size+1);


if (type== "MB_ABORTRETRYIGNORE")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_ABORTRETRYIGNORE);

}

if (type=="MB_CANCELTRYCONTINUE")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_CANCELTRYCONTINUE);

}

if (type=="MB_HELP")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_HELP);

}

if (type=="MB_OK")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OK);

}
if (type=="MB_OKCANCEL")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OKCANCEL);

}
if (type=="MB_RETRYCANCEL")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_RETRYCANCEL);

}

if (type=="MB_YESNO")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNO);

}

if (type=="MB_YESNOCANCEL")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNOCANCEL);

}

if (type=="MB_ABORTRETRYIGNORE | MB_ICONHAND")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_ABORTRETRYIGNORE | MB_ICONHAND);

}

if (type=="MB_ABORTRETRYIGNORE | MB_IConstop")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_ABORTRETRYIGNORE | MB_IConstop);

}

if (type=="MB_ABORTRETRYIGNORE | MB_IConerror")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_ABORTRETRYIGNORE | MB_IConerror);

}

if (type=="MB_ABORTRETRYIGNORE | MB_ICONQUESTION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_ABORTRETRYIGNORE | MB_ICONQUESTION);

}

if (type=="MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION);

}

if (type=="MB_ABORTRETRYIGNORE | MB_MB_ICONWARNING")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_ABORTRETRYIGNORE | MB_ICONWARNING);

}

if (type=="MB_ABORTRETRYIGNORE | MB_ICONASTERISK")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_ABORTRETRYIGNORE | MB_ICONASTERISK);

}

if (type=="MB_ABORTRETRYIGNORE | MB_ICONINFORMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_ABORTRETRYIGNORE | MB_ICONINFORMATION);

}

if (type=="MB_CANCELTRYCONTINUE | MB_ICONHAND")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_CANCELTRYCONTINUE | MB_ICONHAND);

}

if (type=="MB_CANCELTRYCONTINUE | MB_IConstop")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_CANCELTRYCONTINUE | MB_IConstop);

}

if (type=="MB_CANCELTRYCONTINUE | MB_IConerror")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_CANCELTRYCONTINUE | MB_IConerror);

}

if (type=="MB_CANCELTRYCONTINUE | MB_ICONQUESTION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_CANCELTRYCONTINUE | MB_ICONQUESTION);

}

if (type=="MB_CANCELTRYCONTINUE | MB_ICONEXCLAMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_CANCELTRYCONTINUE | MB_ICONEXCLAMATION);

}

if (type=="MB_CANCELTRYCONTINUE | MB_MB_ICONWARNING")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_CANCELTRYCONTINUE | MB_ICONWARNING);

}

if (type=="MB_CANCELTRYCONTINUE | MB_ICONASTERISK")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_CANCELTRYCONTINUE | MB_ICONASTERISK);

}

if (type=="MB_CANCELTRYCONTINUE | MB_ICONINFORMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_CANCELTRYCONTINUE | MB_ICONINFORMATION);

}

if (type=="MB_HELP | MB_ICONHAND")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_HELP | MB_ICONHAND);

}

if (type=="MB_HELP | MB_IConstop")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_HELP | MB_IConstop);

}

if (type=="MB_HELP | MB_IConerror")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_HELP | MB_IConerror);

}

if (type=="MB_HELP | MB_ICONQUESTION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_HELP | MB_ICONQUESTION);

}

if (type=="MB_HELP | MB_ICONEXCLAMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_HELP | MB_ICONEXCLAMATION);

}

if (type=="MB_HELP | MB_MB_ICONWARNING")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_HELP | MB_ICONWARNING);

}

if (type=="MB_HELP | MB_ICONASTERISK")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_HELP | MB_ICONASTERISK);

}

if (type=="MB_HELP | MB_ICONINFORMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_HELP | MB_ICONINFORMATION);

}

if (type=="MB_OK | MB_IConstop")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OK | MB_IConstop);

}

if (type=="MB_OK | MB_IConerror")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OK | MB_IConerror);

}

if (type=="MB_OK | MB_ICONQUESTION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OK | MB_ICONQUESTION);

}

if (type=="MB_OK | MB_ICONEXCLAMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OK | MB_ICONEXCLAMATION);

}

if (type=="MB_OK | MB_MB_ICONWARNING")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OK | MB_ICONWARNING);

}

if (type=="MB_OK | MB_ICONASTERISK")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OK | MB_ICONASTERISK);

}

if (type=="MB_OK | MB_ICONINFORMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OK | MB_ICONINFORMATION);

}

if (type=="MB_OKCANCEL | MB_IConstop")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OKCANCEL | MB_IConstop);

}

if (type=="MB_OKCANCEL | MB_IConerror")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OKCANCEL | MB_IConerror);

}

if (type=="MB_OKCANCEL | MB_ICONQUESTION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OKCANCEL | MB_ICONQUESTION);

}

if (type=="MB_OKCANCEL | MB_ICONEXCLAMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OKCANCEL | MB_ICONEXCLAMATION);

}

if (type=="MB_OKCANCEL | MB_MB_ICONWARNING")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OKCANCEL | MB_ICONWARNING);

}

if (type=="MB_OKCANCEL | MB_ICONASTERISK")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OKCANCEL | MB_ICONASTERISK);

}

if (type=="MB_OKCANCEL | MB_ICONINFORMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_OKCANCEL | MB_ICONINFORMATION);

}

if (type=="MB_RETRYCANCEL | MB_IConstop")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_RETRYCANCEL | MB_IConstop);

}

if (type=="MB_RETRYCANCEL | MB_IConerror")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_RETRYCANCEL | MB_IConerror);

}

if (type=="MB_RETRYCANCEL | MB_ICONQUESTION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_RETRYCANCEL | MB_ICONQUESTION);

}

if (type=="MB_RETRYCANCEL | MB_ICONEXCLAMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_RETRYCANCEL | MB_ICONEXCLAMATION);

}

if (type=="MB_RETRYCANCEL | MB_MB_ICONWARNING")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_RETRYCANCEL | MB_ICONWARNING);

}

if (type=="MB_RETRYCANCEL | MB_ICONASTERISK")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_RETRYCANCEL | MB_ICONASTERISK);

}

if (type=="MB_RETRYCANCEL | MB_ICONINFORMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_RETRYCANCEL | MB_ICONINFORMATION);

}

if (type=="MB_YESNO | MB_IConstop")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNO | MB_IConstop);

}

if (type=="MB_YESNO | MB_IConerror")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNO | MB_IConerror);

}

if (type=="MB_YESNO | MB_ICONQUESTION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNO | MB_ICONQUESTION);

}

if (type=="MB_YESNO | MB_ICONEXCLAMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNO | MB_ICONEXCLAMATION);

}

if (type=="MB_YESNO | MB_MB_ICONWARNING")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNO | MB_ICONWARNING);

}

if (type=="MB_YESNO | MB_ICONASTERISK")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNO | MB_ICONASTERISK);

}

if (type=="MB_YESNO | MB_ICONINFORMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNO | MB_ICONINFORMATION);

}

if (type=="MB_YESNOCANCEL | MB_IConstop")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNOCANCEL | MB_IConstop);

}

if (type=="MB_YESNOCANCEL | MB_IConerror")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNOCANCEL | MB_IConerror);

}

if (type=="MB_YESNOCANCEL | MB_ICONQUESTION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNOCANCEL | MB_ICONQUESTION);

}

if (type=="MB_YESNOCANCEL | MB_ICONEXCLAMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNOCANCEL | MB_ICONEXCLAMATION);

}

if (type=="MB_YESNOCANCEL | MB_MB_ICONWARNING")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNOCANCEL | MB_ICONWARNING);

}

if (type=="MB_YESNOCANCEL | MB_ICONASTERISK")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNOCANCEL | MB_ICONASTERISK);

}

if (type=="MB_YESNOCANCEL | MB_ICONINFORMATION")
{

MessageBox(0, unicode_string_one, unicode_string_two, MB_YESNOCANCEL | MB_ICONINFORMATION);

}

}



This post has been edited by m4unot: 15 June 2010 - 08:43 AM

Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • Joined: 18-February 07

Re: UINT

Posted 15 June 2010 - 09:37 AM

if (type== "MB_ABORTRETRYIGNORE") -- SHould not work... c-strings can not be compared using the == operator (that will compare pointers not the actual string content) -- you will need to use strcmp()


BTW: Are you in C++ or C? If you are in C++ I have a dumb little snippet that will let you use the == operator with c-strings
Was This Post Helpful? 1
  • +
  • -

#12 m4unot  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 163
  • Joined: 05-June 10

Re: UINT

Posted 15 June 2010 - 09:49 AM

I've tried and tested everything to ensure me that there are no errors. And everything works fine my friend.

my compiler is: Microsoft Visual C++ 2008 Express Edition


just do:


NewMessage("hello", "haha", "MB_ABORTRETRYIGNORE");




PS: i will take a look at : [url="http://www.dreamincode.net/code/snippet3894.htm"]dumb little snippet[/url]

This post has been edited by m4unot: 15 June 2010 - 10:00 AM

Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • Joined: 18-February 07

Re: UINT

Posted 15 June 2010 - 10:13 AM

maybe you compiler has some super secret way of knowing the difference between what you told it to do and what you wanted... but I doubt it

:) Actually I know why it is working for you. Visual C++ is using a unique instance for each string literal. Take my word for it:

Char arrays are NOT properly compared using the == operator in C++ -- the == operator compares pointers but NOT the contents of the strings.

I know it "seems" to be working but that is only because your compiler is being nice -- you code is broken! *trust me*

done want to trust me? well try this:

char type[] = "MB_OK";
NewMessage("hello", "haha", type);


Was This Post Helpful? 1
  • +
  • -

#14 m4unot  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 163
  • Joined: 05-June 10

Re: UINT

Posted 15 June 2010 - 10:28 AM

It works if you do so:


char* type = ("MB_OK");

NewMessage("hello", "haha", type);




I trust you and I am sure you are right .... although my compiler shows no errors. will now try with eclipse compiler and see what happens :angel:

This post has been edited by m4unot: 15 June 2010 - 10:39 AM

Was This Post Helpful? 0
  • +
  • -

#15 Banfa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 109
  • Joined: 07-June 10

Re: UINT

Posted 15 June 2010 - 10:44 AM

I would like to repeat, strings should not be used for passing round parameter values. Computers in general are optimised for working with numbers, strings are generally a construct that is artificially created for communication, normally with humans.

Passing parameter values as strings is really poor practice, especially if you are not going to test the strings correctly and doesn't play to the computers strengths.

I have to say that if I was presented with a library with an interface like this the very first thing I would do would be look for a different library and the second thing I would do would be see if I could do my work without it.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2