Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Conversion of types in c++

Posted on 2003-03-04
Medium Priority
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??


Question by:Excalibur81
  • 3
  • 2

Author Comment

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....

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

Author Comment

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!!!


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


Accepted Solution

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!!"

Author Comment

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 ;)

Expert Comment

ID: 8070888
you're wellcome :)

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

564 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