473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

type conversions

im a noob with all the c++ stuff, working on the maya API, stuff have been flowing pretty smoothly, but my main problem all the time is the type conversions! i cant believe its that complicated, is there like an easy way that i dont know about? i need to do a conversion from a string to a wchar_t, and its not working at all, and all tutorials are kind of advanced and assume quite a previous knowledge of c++, so could anyone simplify stuff for me??

thanks alot
Sep 12 '07 #1
3 1501
weaknessforcats
9,208 Expert Mod 8TB
A string has char elements. You need to convert those to wchar_t. If you are using Windows, you can call mbstowcs().

If effect you are converting a string to a wstring.

Type conversions are not complicated. They are simple an orderly transformation of one to to another. And yes, you may need some knowledge of C++ in order to do this. I'm sure Google and MSDN are loaded wirth info.

A type cast is entirely different. A cast is an assertion by you that an thing of type A is really a thing of type B. It is an official way of lying to the compiler.

Type conversions are to be embraced and type casts are to be shunned.
Sep 12 '07 #2
i didnt come here before having looked around a LOT, and after, i managed to convert and it does compile, but when i print the results out (after conversion) thru the usual cout<< i get some kind of hexadecimal printing everytime "0103F544"

thanks again
Sep 13 '07 #3
let me cut it short, im trying to open files over a for loopwith each loop a new file opens, adds wwatever i want and closes, im doing this so far

Expand|Select|Wrap|Line Numbers
  1.  
  2. for (int j=0;j<24;j++){
  3.  
  4.     /////////int to string
  5.             string jAsString;
  6.             stringstream smStrngStrm;
  7.             smStrngStrm << j;
  8.             jAsString = smStrngStrm.str();
  9.  
  10.     /////////////// file name
  11.  
  12.     //file path in string format, to be cnverted to wchar_t
  13.     string filePath = "geo";
  14.     filePath += jAsString;
  15.     filePath += ".cache";
  16.  
  17.  
  18.  
  19.  
  20.     // First convert to a char*
  21.     string cstring = filePath;
  22.     const size_t newsize = 100;
  23.     char pathChar[newsize];
  24.     strcpy_s(pathChar, cstring.c_str());
  25.     strcat_s(pathChar, " (char *)");
  26.  
  27.     // Then convert to a wchar_t*
  28.     size_t origsize = strlen(cstring.c_str()) + 1;
  29.     size_t convertedChars = 0;
  30.     wchar_t wcstring[newsize];
  31.     mbstowcs_s(&convertedChars, wcstring, origsize, cstring.c_str(), _TRUNCATE);
  32.     wcscat_s(wcstring, L" (wchar_t *)");
  33.  
  34.     const char *pathAsChar = filePath.c_str();
  35.  
  36.  
  37.  
  38.  
  39.  
  40. fopen(filePath,"w");
  41.  
  42.         //print verts to file
  43.          // the problem is right here! the wcstring doesnt seem to be right, and               //        //therefore doesnt open the file to write...
  44.  
  45.     out.open(wcstring, ios::out);
  46.  
  47.     out<<wcstring[0]<<"\n";
  48.  
  49.  
  50.  
  51.  
  52.  
  53.         for(int i=0;i!=meshVertices.length();++i) {
  54.  
  55. // code here is maya api, works well on a single file
  56.                 out        //<< pathToGeo
  57.                         << i << " "
  58.                         << meshVertices[i].x <<" "
  59.                         << meshVertices[i].y <<" "
  60.                         << meshVertices[i].z <<"\n";
  61.  
  62.                 //cout<< meshVertices[i].x ;
  63.  
  64.                 }
  65.  
  66.  
  67. //    out.close();
  68.  
  69.     fclose(pFile);
  70.  
  71.     }
  72.  
  73.  
  74. }


anyone????
Sep 13 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Terje Slettebø | last post by:
Hi. I'm new here, and sorry if this has been discussed before; I didn't find it searching the PHP groups. (I've also read recommendations to cross-post to the other PHP groups, but if that is...
8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
15
by: buda | last post by:
Let me see if I got this :) 1. I know the rules for type conversions in arithmetic expressions 2. I know that an implicit type conversion is done at assignment, so float x = 1.23; int t = (int)...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
2
by: Sam Sungshik Kong | last post by:
Hello! I studied C# a little bit and am trying to compare it with VB.Net. There's 'Option Strict' in VB.Net. I thought that if I turn it on, it is as strict as C# when checking types. See...
3
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object...
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
16
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
5
by: Akaketwa | last post by:
guys help me on this rather nubish question.I have created a new data type in java and i also want to create a new data type object in db2 that will store the money data object. The money...
23
by: neha_chhatre | last post by:
which is the best format specifier(data type) if i have to work with decimal number. also please tell me the syntax for truncating a decimal number please reply as soon as possible
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.