I apologise beforehand if this has been asked many times but the problem is I cannot find the exact match to my question on here or Google in general.
I am trying to use VS C++ 2010 (windows form project) and cannot find a reasonable way to display a simple string (using char *variablename) in a textbox.
I pretty much worked out how to get anything else into the textbox:
String^ word1 = "hello";
int num1 = 23;
double num2 = 24.6;
float num3 = 25.9;
//The above 4 work but the next 2 do not
char *word2 = "hello";
string word3 = "hello"; //I have included <string>!
textBox1->Text = Convert::ToString(variablename);
If I use char* it outputs "true" and if I use string it just does not seem to be able to convert it.
The reason why I want to use either char* or string is because you cannot use String^ in a class! If I could use String^ in a class then I would but it is managed code.
Could anyone inform me as to how I get around this problem?
Related
I'm trying to convert the String "10.20" to Int without converting it to Double before.
I want to get the value: 10
At the moment what is working is:
let valueString = "10.20"
let valueInt = Int(Double(valueString)!)
print(valueInt)
But, is there any better way to do it?
I was trying first to do it with this command but was returning nil:
let valueString = "10.20"
let valueInt = Int(valueString)
print(valueInt)
Thanks!
You could just put an extension on the Int type that makes it StringLiteralConvertable, producing the expected result via a Float cast behind the scenes.
This was the original code
int main(void)
{
char hello[] = "hello ", world[] = "world!\n", *s;
s = strcat(hello,world);
printf(s);
return 0;
}
char hello[] = "hello ", world[] = "world!\n", *s;
strcat(hello,world);
printf(hello);
i changed it to what it is below
i am positive i fixed that code, but my instructor marked me off.
like i told him it doesn't even use the pointer, so this is fine. he said he doesn't think it's correct
am i wrong?
like i ran it 50 times and it still works.
Your instructor is correct. hello is only big enough to hold 6 characters (plus a null-terminator). So trying to strcat something into it writes past the end, causing undefined behaviour.
I'm beginner in Win32Api, I tried to make calculator but I failed because of conversion of data types between each other
Example:
int N1 = GetDlgItemText(WID,IDC_N1,NULL,NULL);
int N2 = GetDlgItemText(WID,IDC_N2,NULL,NULL);
int RESULT = N1+N2;
MessageBox(NULL,RESULT,L"Message",MB_OK);
The example in above tell me the following error
(cannot convert parameter 2 from 'int' to 'LPCWSTR')
And the reason for this error is conversion of data types between each other
Please anybody help me
Here is correct code for your task:
wchar_t Str1[100], Str2[100], ResStr[100];
GetDlgItemText(WID, IDC_N1, Str1, 100);
GetDlgItemText(WID, IDC_N2, Str2, 100);
int N1 = _wtoi(Str1);
int N2 = _wtoi(Str2);
int RESULT = N1 + N2;
_itow(RESULT, ResStr, 10);
MessageBox(NULL, ResStr, L"Message",MB_OK);
Useful links:
http://msdn.microsoft.com/en-us/library/ms645489(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/ms645505(v=vs.85).aspx
You need to pass unicode string instead of int to MessageBox.
wchar_t ResStr[100]; //define string
_itow(RESULT, ResStr, 10); //convert int result to string
MessageBox(NULL, ResStr, L"Message",MB_OK); //now display string
There is an API for this. Use GetDlgItemInt.
Your project isn't set to use Unicode, but you're passing a wide string to MessageBox. You can:
1) Change your project settings so that it defaults to Unicode; or
2) Explicitly call MessageBoxW; or
3) Remove the L, and use the non-Unicode API.
Looks like you just need to go to project>properties>configuration properties>and change 'character set' to Multi-Byte. It will probably be at Unicode, I think this will work because that's the error I always get when I try to use the WinAPI MessageBox() before changing the character set. Maybe you're trying to do something different? But this should help...
I'm trying to convert a char* to a BSTR*, and my char* has special characters in it from being encrypted. I have tried several approaches found on the web, but back in the calling vb code, I always end up with something different. I'm pretty sure this has to do with the special characters, because if I don't have them in, it seems to be ok....
my code is something along these lines...
_export myFunction(BSTR *VBtextin, BSTR *VBpassword, BSTR *VBtextout, FPINT encrypt) {
BSTR password = SysAllocString (*VBpassword);
char* myChar;
myChar = (char*) password //is this ok to cast? it seems to remain the same when i print out.
//then I encrypt the myChar in some function...and want to convert back to BSTR
//i've tried a few ways like below, and some other ways i've seen online...to no avail.
_bstr_t temp(myChar);
SysReAllocString(VBtextout, myChar);
any help would be greatly greatly appreciated!!!
Thanks!!!!
If you're manipulating the buffer, you probably don't want manipulate the char * directly. First make a copy:
_export myFunction(BSTR *VBtextin, BSTR *VBpassword, BSTR *VBtextout, FPINT encrypt) {
UINT length = SysStringLen(*VBpassword) + 1;
char* my_char = new char[length];
HRESULT hr = StringCchCopy(my_char, length, *VBpassword);
If that all succeeds, perform your transformation. Make sure to handle failure as well, as appropriate for you.
if (SUCCEEDED(hr)) {
// Perform transformations...
}
Then make a copy back:
*VBtextout = SysAllocString(my_char);
delete [] my_char;
}
Also, have a read of Eric's Complete Guide to BSTR Semantics.
I have a simple program and I get access violation at *(str + start). Why? I should be able to change it. Right?
void fn()
{
char *str = "Hello wordl!";
int end = strlen(str);
int start = 0;
end--;
while(start < end)
{
*(str + start) = *(str + end); <--- Access violation writing location *(str + Start).
end--;
start++;
}
}
char *str = "Hello World"; is a const string, and cannot be modified. The compiler is free to put it into a non-writable location, resulting in the crash you see.
Replacing the declaration with char str[] = "Hello World"; should do what you want, putting the string into a modifiable array on the stack.
No, you should not. "Hello world" is a constant string literal, you need to allocate memory using malloc() in C, or new in C++ if you want memory you are free to modify.
As others have pointed out, literal strings may be stored in a read-only area of memory. Are you compiling with warnings turned on? You should get a warning about discarding the constness of the string literal.
What you can do instead is:
char *str = strdup("Hello, world!");
// Modify the string however you want
free(str);
It's because you're writing to a string literal's storage, which may be in a protected area of memory.
In your example, Hello wordl! is constant string, any attempt to modify this constant string will result in an exception.
Instead, You can do this -
string s = "Hello wordl!";
char* ptr = &s[0];
and then play around with ptr.