Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Unsigned Char to char*

Unsigned Char to char*

Scheduled Pinned Locked Moved C / C++ / MFC
c++questioncsharpdata-structureshelp
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wender Oliveira
    wrote on last edited by
    #1

    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

    R A J 3 Replies Last reply
    0
    • W Wender Oliveira

      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

      R Offline
      R Offline
      Rodrigo Pinto Pereira de Souza
      wrote on last edited by
      #2

      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

      T 1 Reply Last reply
      0
      • R Rodrigo Pinto 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

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • W Wender Oliveira

          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

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          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.

          J 1 Reply Last reply
          0
          • A Anonymous

            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.

            J Offline
            J Offline
            John R Shaw
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • W Wender Oliveira

              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

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups