Unsigned Char to char*
-
Hello people, I'm trying to learn a few more of C++ once that I know another language. My question is... I have an array of unsigned char like this "unsigned char myArr[6]" and I want to get these values and show them into a Static Text (Visual C++) and I don't know how to convert these unsigned char array to char* or any type of "string". Can anyone help me? thanks! Wender Oliveira .NET Programmer
-
Hello people, I'm trying to learn a few more of C++ once that I know another language. My question is... I have an array of unsigned char like this "unsigned char myArr[6]" and I want to get these values and show them into a Static Text (Visual C++) and I don't know how to convert these unsigned char array to char* or any type of "string". Can anyone help me? thanks! Wender Oliveira .NET Programmer
Try it. char myArr[6]; SetDlgItemText(ID_EDIT, myArr); If you are beginner with Visual C, I suggest you to use MFC classes, Then, you will get CString class, that are similar to String from .Net Enjoy it. :cool: Rodrigo Pinho Pereira de Souza
-
Try it. char myArr[6]; SetDlgItemText(ID_EDIT, myArr); If you are beginner with Visual C, I suggest you to use MFC classes, Then, you will get CString class, that are similar to String from .Net Enjoy it. :cool: Rodrigo Pinho Pereira de Souza
pinhopro wrote: SetDlgItemText(ID_EDIT, myArr); no way. it is only if you consider that one of the 6 chars of the array contain a '\0' no, to display each char, prefer that :
CString str;
char myArr[6];
//...
str.format("%c%c%c%c%c%c", myArr[0], myArr[1], myArr[2], myArr[3], myArr[4], myArr[5]);
//...
((CStatic*)GetDlgItem(IDC_MYSTATIC))->SetWindowText(str);
TOXCCT >>> GEII power
-
Hello people, I'm trying to learn a few more of C++ once that I know another language. My question is... I have an array of unsigned char like this "unsigned char myArr[6]" and I want to get these values and show them into a Static Text (Visual C++) and I don't know how to convert these unsigned char array to char* or any type of "string". Can anyone help me? thanks! Wender Oliveira .NET Programmer
Wender, I have not logged many hours on Visual C++ .net but I think what you are asking to do is called a cast. Changing one type to another. However, an array really is a pointer as your myArr[0] would be the address of the array. So if you said printf("%s\n", myArr); your getting the all 6 items as opposed to: printf("%s\n", myArr[1]); where you will get the first character.
-
Wender, I have not logged many hours on Visual C++ .net but I think what you are asking to do is called a cast. Changing one type to another. However, an array really is a pointer as your myArr[0] would be the address of the array. So if you said printf("%s\n", myArr); your getting the all 6 items as opposed to: printf("%s\n", myArr[1]); where you will get the first character.
Anonymous wrote: myArr[0] would be the address of the array myArr would be the address, it is an unsigned char*. myArr[0] is the first character in the array. Anonymous wrote: So if you said printf("%s\n", myArr); your getting the all 6 items No! There is only room for 6 characters in array and the last character must be '\0', so you would be printing only the first characters Anonymous wrote: printf("%s\n", myArr[1]); where you will get the first character. This is just wrong! myArr[1] is the second, not the first charater and it is not a string pointer, so the results will be undefined. The %s expects a string pointer not a character value. INTP
-
Hello people, I'm trying to learn a few more of C++ once that I know another language. My question is... I have an array of unsigned char like this "unsigned char myArr[6]" and I want to get these values and show them into a Static Text (Visual C++) and I don't know how to convert these unsigned char array to char* or any type of "string". Can anyone help me? thanks! Wender Oliveira .NET Programmer
Assuming you want to display all 6 characters then you need to set the array size to 7, since the last character must be '\0'. Exmaples: 1) unsigned char myArr[7]={"012345\0"}; 2) unsigned char myArr[7]; myArr[0] = '0'; myArr[1] = '1'; myArr[2] = '2'; myArr[3] = '3'; myArr[4] = '4'; myArr[5] = '5'; myArr[6] = '\0'; Now it you want a char* insted of a unsigned char* just cast it: (char*)myArr. This is safe because both unsigned char and char are the same size. There are other ways, but why make life difficult. INTP