How to read textbox text property into an unsigned char array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Rubin
    New Member
    • Aug 2010
    • 5

    How to read textbox text property into an unsigned char array

    I am trying to figure out how to get the contents of a textbox text property into an unsigned char array. I have been researching this via google for a few days without any success unfortunately.

    I was finally successful at converting an unsigned char array to a string that can be written to a label text property but cannot figure out how to do the reverse.

    The successful code I wrote that writes the unsigned char array to a label control text property is...
    Code:
    private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
    	ULONG BytesWritten = 0;
    	ULONG BytesRead = 0;
    
    	//Allocate a memory buffer which will contain data to send to the USB device
    	unsigned char OutputPacketBuffer[64];	
    	//Allocate a memory buffer for the data which we will read from the USB device
    	unsigned char InputPacketBuffer[64];	
    	unsigned int x;
    	unsigned char y;
    
    	y=0x41;
    	for(x=1;x<=10;x++){ //write test data into the buffer
    	OutputPacketBuffer[x]=y;
    	y++;
    	}
    
    	//0x83 is the "TOGGLE LED 4" command in the firmware
    	OutputPacketBuffer[0] = 0x83;
    	//Null terminator
    	OutputPacketBuffer[11] = 0x00;
    
    	// Output first 11 chars of the output buffer to label1 text property
    	std::string s( reinterpret_cast< char const* >( OutputPacketBuffer ),11) ;
    	label1->Text =  gcnew System::String(s.c_str());
    
    	WinUsb_WritePipe(MyWinUSBInterfaceHandle, 0x01, &OutputPacketBuffer[0], 64, &BytesWritten, NULL);
    	WinUsb_ReadPipe(MyWinUSBInterfaceHandle, 0x81, &InputPacketBuffer[0], 64, &BytesRead, NULL); 
    
    	// Output first 11 chars of the input buffer to label2 text property
    	std::string s2( reinterpret_cast< char const* >( InputPacketBuffer ),11) ;
    	label2->Text =  gcnew System::String(s2.c_str());
    
    	}
    I just cannot figure out how to use reinterpret_cas t to do the reverse, as in reading the text property back into an unsigned char buffer. I realize that the example given is using a label control, and for the reverse I will be using a textbox control.

    Thank you for your time!

    Gary Rubin
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    Can you give use the exact error?

    Also, I see you use the keyword const (which is good), however, just be sure and double check you don't use it fallaciously.

    Comment

    • Gary Rubin
      New Member
      • Aug 2010
      • 5

      #3
      My first attempt without using reinterpret_cas t didn't work, pretty much as expected. I used sprintf as I found in a thread somewhere. I have tried a few other approaches as well. Again, I'm not sure how to approach it with reinterpret_cas t.
      Code:
      	ULONG BytesWritten = 0;
      	int dummy = 0;
      	
      	//Allocate a memory buffer which will contain data to send to the USB device
      	unsigned char OutputPacketBuffer[64];	
      	//Allocate a memory buffer which will contain data to send to the SLAVE device via USART
      	unsigned char SlaveDataBuffer[64];	
      
      	
      	dummy=sprintf(SlaveDataBuffer,"%s",lcd_tb1->Text);
      	
      	sendUSARTpacket(1,101,dummy,0x80,&SlaveDataBuffer[0],&OutputPacketBuffer[0]);
      	
      	WinUsb_WritePipe(MyWinUSBInterfaceHandle, 0x01, &OutputPacketBuffer[0], 64, &BytesWritten, NULL);
      Code:
      error C2664: 'sprintf' : cannot convert parameter 1 from 'unsigned char [64]' to 'char *'
      1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        This may sound sleazy, but try the following:

        Code:
        dummy = sprintf(reinterpret_cast<char *>&SlaveDataBuffer, "%s", lcd_tb1->Text);
        I believe that it'll work because taking the address of SlaveDataBuffer gives you a pointer to the buffer its self.

        Comment

        • Gary Rubin
          New Member
          • Aug 2010
          • 5

          #5
          Your suggestion threw a syntax error and I am not sure why. The use of '&' should be valid, shouldn't it?
          Code:
          1>\Form1.h(1316) : error C2059: syntax error : '&'
          1>\Form1.h(1316) : error C2143: syntax error : missing '(' before '&'

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            Yeah, unless I forget the parenthesis.

            Code:
            dummy = sprintf(reinterpret_cast<char *>(&SlaveDataBuffer), "%s", lcd_tb1->Text);

            Comment

            • Gary Rubin
              New Member
              • Aug 2010
              • 5

              #7
              Oh, ok. I see that now. Corrected and it compiled fine albeit for a warning about using sprintf...
              Code:
              warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
              Thank you for the help! I will look at that in more detail to see what is happening so I can figure these things out for myself in the future.

              Regards,
              Gary Rubin

              Comment

              • Oralloy
                Recognized Expert Contributor
                • Jun 2010
                • 988

                #8
                You're doing fine, Gary.

                Good luck getting it done.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  The SlaveDataBuffer array of unsigned char is not the name as an array of char since the array of char may be signed.

                  In the first case SlaveDataBuffer is the address of an unisgned char so you need an unsigned char* to contain that address. That's the sprintf error.

                  By doing the cast, any sign values in the data are going to show up in your SlaveDataBuffer as data bits and not sign bits.

                  Unless there is a compelling reason for an unsigned char array, I would just use a char array and omit the cast.

                  Actually, I woulf skip the sprintf and the C stuff and use a string stream object.

                  Comment

                  • Gary Rubin
                    New Member
                    • Aug 2010
                    • 5

                    #10
                    "Actually, I woulf skip the sprintf and the C stuff and use a string stream object. "

                    I will eventually. Moving into C++ from C is a gradual process for me. I have been learning C via programming for my embedded projects and now exposed to C++ while I write windows applications to communicate with those embedded systems.

                    My project is working now though, albeit 'C-Stuff', and I look forward to improving that function using a string stream. I just haven't attempted anything like that yet.
                    Last edited by Gary Rubin; Aug 4 '10, 04:25 PM. Reason: grammatical issue

                    Comment

                    • Oralloy
                      Recognized Expert Contributor
                      • Jun 2010
                      • 988

                      #11
                      I believe the issue is that one of his APIs [WinUsb_WritePip e, et.al.] requires unsigned char * arguments, and that's why his buffers are declared thusly. Then comes the question of what is the most meaningful expression of code's meaning. And we can go on forever about that.

                      I don't know if this is the controlling issue in variable definition or not. Only Gary can tell us.

                      Still, it would do you some good to look into stream I/O, Gary. It's pretty simple, and you can do most of your work with simpler constructs than printf/sprintf.

                      Frankly, the 64 byte fixed length arrays bother me, although they may be unavoidable.

                      You might want to use the "sizeof name" construct in the function calls for the length arguments, or base the array lengths and arguments on a manifest (#define) constant.

                      Cheers!

                      Comment

                      Working...