Excalibur81
asked on
Conversion of types in c++
Ok, ive been at this c++ thing for a few weeks now, and i have a question which has been bugging me (no pun intended :P)
How do you convert from char to int to string to char[] to byte??!!
For example, i am getting the seconds played in a wave file, using a MMTIME struct :-
MMTIME time;
time.wType = TIME_MS;
waveOutGetPosition(waveHan dle, &time, sizeof(time));
char c = time.u.smpte.sec;
TextOut(GetDC(hwnd), 420, 50, (char*)c, sizeof(c));
this shows nothing at all!
(yes the space at 420, 50 is clear :P)
So can someone just give me a quick example of how to do some type conversions??
regards,
Robert
How do you convert from char to int to string to char[] to byte??!!
For example, i am getting the seconds played in a wave file, using a MMTIME struct :-
MMTIME time;
time.wType = TIME_MS;
waveOutGetPosition(waveHan
char c = time.u.smpte.sec;
TextOut(GetDC(hwnd), 420, 50, (char*)c, sizeof(c));
this shows nothing at all!
(yes the space at 420, 50 is clear :P)
So can someone just give me a quick example of how to do some type conversions??
regards,
Robert
Hello Excallibur81,
about conversions, well, that's how u do it:
u can use : static_cast<type>(variable )
where the "type" is the type that u want and variable is the variable that u want to change the type
about conversions, well, that's how u do it:
u can use : static_cast<type>(variable
where the "type" is the type that u want and variable is the variable that u want to change the type
ASKER
thanx, but i kinda already new that...
It just seems that, like in the above case, some casts dont work...
If i created a LPCTSTR variable, such as
LPCTSTR s = "this works!!";
and then passed this to the TextOut() proc, it would work fine,
i.e. TextOut(GetDC(hwnd), 420, 50, s, 12);
if i cast the char c to a LPCTSTR such as:
TextOut(GetDC(hwnd), 420, 50, (LPCTSTR)c, 50);
it shows no text at all...
Presumably because the LPCTSTR cast is just a pointer (LP = LONG POINTER, CT = Character, STR = Star i think! )
so i think all that is being printed out is a pointer to a char.....
and what needs to be printed out is the byte returned by time.u.smpte.sec, which has a numerical value....
if i declare BYTE b = time.u.smpte.sec; and then pass this to TextOut, i also get no text output.....
However, if i pass an array of chars it works:
char d[10] = "123456789";
So you see my problem with weird casting and what actually 'fits' in what!!!
It just seems that, like in the above case, some casts dont work...
If i created a LPCTSTR variable, such as
LPCTSTR s = "this works!!";
and then passed this to the TextOut() proc, it would work fine,
i.e. TextOut(GetDC(hwnd), 420, 50, s, 12);
if i cast the char c to a LPCTSTR such as:
TextOut(GetDC(hwnd), 420, 50, (LPCTSTR)c, 50);
it shows no text at all...
Presumably because the LPCTSTR cast is just a pointer (LP = LONG POINTER, CT = Character, STR = Star i think! )
so i think all that is being printed out is a pointer to a char.....
and what needs to be printed out is the byte returned by time.u.smpte.sec, which has a numerical value....
if i declare BYTE b = time.u.smpte.sec; and then pass this to TextOut, i also get no text output.....
However, if i pass an array of chars it works:
char d[10] = "123456789";
So you see my problem with weird casting and what actually 'fits' in what!!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks cmaryus, thats just what i needed :)
From Delphi, you have StrToInt, and IntToStr, i knew there had to be some kind of C++ equivalent, but my documentation didn't have any listed....
It has a few minor examples that had 'itoa' and 'atoi', but i never thought to use those as keywords.....
Yes, the s = "this works!!" thing is as i expected in that you are only passing the address to the pointer, rather than the data.
thanks for making c++ that much easier to a humble beginner ;)
From Delphi, you have StrToInt, and IntToStr, i knew there had to be some kind of C++ equivalent, but my documentation didn't have any listed....
It has a few minor examples that had 'itoa' and 'atoi', but i never thought to use those as keywords.....
Yes, the s = "this works!!" thing is as i expected in that you are only passing the address to the pointer, rather than the data.
thanks for making c++ that much easier to a humble beginner ;)
you're wellcome :)
ASKER