Char[1] to UINT ???
-
When you define a variable as char[1] rather than just char:
char CharArray[1];
rather thanchar SingleChar;
you have changed a lot! Even though the array you defined has only 1 element (making it the same as a single char memory usage-wise), it is referenced the same way as if you had defined it as char[1000] - ie as the memory address of its first (zero-eth) element. Since the value of CharArray is a memory address (in other words CharArray is a pointer), it is not compatible for direct assignment to an UINT. So, to convert a char[1] to an UINT you need to specify which element of the array you are talking about. In the case of an array of chars with 1 element, the only valid reference is the zeroeth element - CharArray[0]. Whenever you assign a variable that has a data type that is different from the one you are assigning it to, you need to cast it. "Cast" just means that you are basically telling the compiler "Thanks for the warnings, but I know what I'm doing. I know they were defined as different types, but I really want to do this, so butt out!". Casting will only work if the 2 data types are compatible. Since an UINT is large enough to hold the largest possible char and both data types are non-abstract, the types are compatible and your casting will succeed:char CharArray[1]; // has only 1 valid element - referenced as CharArray[0] char SingleChar; UINT x; // to assign a value to this array you must reference which element you are assigning CharArray[0] = 'A'; // since this is a char and not an array you can assign it directly SingleChar = 'A'; // Just cast the assignment by placing the data type you are casting (converting it to) in parentheses before the assignment. x = (UINT)SingleChar; x = (UINT)C[0];
-
When you define a variable as char[1] rather than just char:
char CharArray[1];
rather thanchar SingleChar;
you have changed a lot! Even though the array you defined has only 1 element (making it the same as a single char memory usage-wise), it is referenced the same way as if you had defined it as char[1000] - ie as the memory address of its first (zero-eth) element. Since the value of CharArray is a memory address (in other words CharArray is a pointer), it is not compatible for direct assignment to an UINT. So, to convert a char[1] to an UINT you need to specify which element of the array you are talking about. In the case of an array of chars with 1 element, the only valid reference is the zeroeth element - CharArray[0]. Whenever you assign a variable that has a data type that is different from the one you are assigning it to, you need to cast it. "Cast" just means that you are basically telling the compiler "Thanks for the warnings, but I know what I'm doing. I know they were defined as different types, but I really want to do this, so butt out!". Casting will only work if the 2 data types are compatible. Since an UINT is large enough to hold the largest possible char and both data types are non-abstract, the types are compatible and your casting will succeed:char CharArray[1]; // has only 1 valid element - referenced as CharArray[0] char SingleChar; UINT x; // to assign a value to this array you must reference which element you are assigning CharArray[0] = 'A'; // since this is a char and not an array you can assign it directly SingleChar = 'A'; // Just cast the assignment by placing the data type you are casting (converting it to) in parentheses before the assignment. x = (UINT)SingleChar; x = (UINT)C[0];
Thanks for your reply.
CString aStr = "中文";//a Chinese CString char cChinese[3]; cChinese[2] = '\0'; int i = 0; while (i= 0xA1 && (byte)aStr[i] <=0xFE) { cChinese[0] = aStr[i]; cChinese[1] = aStr[i+1]; UINT aWord = cChinese // is it possible to assign the arrary to UINT ++i; } ++i; }
-
Thanks for your reply.
CString aStr = "中文";//a Chinese CString char cChinese[3]; cChinese[2] = '\0'; int i = 0; while (i= 0xA1 && (byte)aStr[i] <=0xFE) { cChinese[0] = aStr[i]; cChinese[1] = aStr[i+1]; UINT aWord = cChinese // is it possible to assign the arrary to UINT ++i; } ++i; }
wow9999 wrote:
char cChinese[3];
UINT aWord = cChinese;
Here, you are trying to put the address of your chinese character into the UINT variable. You could force the compiler to do that, but it probably makes no sense here. What are you trying to achive here? Assign one chinese letter to the UINT? Then you need to write something likeUINT aWord = cChinese[i];
That means you need to say which letter you want to assign.
Who is 'General Failure'? And why is he reading my harddisk?!?
-
Thanks for your reply.
CString aStr = "中文";//a Chinese CString char cChinese[3]; cChinese[2] = '\0'; int i = 0; while (i= 0xA1 && (byte)aStr[i] <=0xFE) { cChinese[0] = aStr[i]; cChinese[1] = aStr[i+1]; UINT aWord = cChinese // is it possible to assign the arrary to UINT ++i; } ++i; }
wow9999 wrote: UINT aWord = cChinese // is it possible to assign the arrary to UINT Yes (at least under VC++ 6.0). You would need to write it as so: UINT aWord = (UINT)cChinese; This is what I said in my previous post. You need to cast it. But the real question is why? When you assign the value of cChinese to an UINT you are not assigning the value of any char in cChinese - you are only copying the address of the zeroeth element. If you are trying to look at or manipulate the character codes for the Chinese character set, then you need to assign cChinese[0] to aWord and not cChinese.