?
Solved

Conversion of types in c++

Posted on 2003-03-04
6
Medium Priority
?
490 Views
Last Modified: 2008-02-01
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(waveHandle, &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


0
Comment
Question by:Excalibur81
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Author Comment

by:Excalibur81
ID: 8069996
Ok, the question "How do you convert from char to int to string to char[] to byte??!!" is an exageration of what needs to be done in this particular example, but i would just like to know these 'common' conversions, cause its been quite annoying not knowing what sucessfully casts to what....

0
 
LVL 11

Expert Comment

by:Salim Fayad
ID: 8070093
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
0
 

Author Comment

by:Excalibur81
ID: 8070155
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!!!

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
cmaryus earned 200 total points
ID: 8070217
You cannot cast a string to an integer!
You can cast a char to an integer though.A char is a number on 1 byte, while integer is a number on 4 bytes.This means that a char goes up to 255, while int goes to 4294967295. To be more exctalty:
char : -128 to 128
unsigned char: 0 to 255
int: -2147483647 to 2147483647
unsigned int: 0 to 4294967295.

The VC compiler don't need no explicit cast, so you can do:
int x = 10;
char c = x; , it's ok in c you will have ASCII 10.

You cannot convert a string (array of chars) to integer. You must use a function for this, not a cast:
char szString = "123";
int x = atoi(szString); - x = 123 now

 But if you do this:
LPCTSTR s = "this works!!";
s - is a pointer to the string "this works!!"
int x = (int)s; - in this case x = value of the pointer address to s, so it doesn't have anything to do with the actual string "this works!!"
0
 

Author Comment

by:Excalibur81
ID: 8070861
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 ;)
0
 
LVL 3

Expert Comment

by:cmaryus
ID: 8070888
you're wellcome :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question