Thread: TexBox user Input to Hex Value

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    53

    TexBox user Input to Hex Value

    So I've got this form that a user puts in a numeric value into a text box. This value then has to be placed into a byte string of data so that it can be transmitted over as a packet. Bellow is what I have so far:

    Code:
    Byte[] OUTBuffer = new byte[65];	//Allocate output memory buffer
    Byte[] INBuffer = new byte[65];    //Allocate input memory buffer
    
    if (setmSpeed == true)
                            {
                                OUTBuffer[0] = 0;        //Not used, must be set to 0                          
                                OUTBuffer[1] = 0x85;   //Command mode
                                
                                int decValue = Convert.ToInt32(textBox4.Text);  //Get textbox contents and vconvert it to an integer
                                byte[] intBytes = BitConverter.GetBytes(decValue);
                                OUTBuffer[2] = decValue;   //Data to be sent 
    
                                for (uint i = 3; i < 65; i++)	 //Set rest of data bytes to 1.
                                OUTBuffer[i] = 0xFF;		
    
                                WriteFile(WriteHandleToUSBDevice, OUTBuffer, 65, ref BytesWritten, IntPtr.Zero);	//Blocking function, unless an "overlapped" structure is used
                                ToggleLEDsPending = false;
                            }
    I've put in red the area where I'm having issues. I've tried different methods and have not been able to get this working yet. It only works if I speciffy the value manually as with OUTBuffer[1] = 0x85;, but I want byte 2 of OUTBuffer[] to be set with what I put in the text box. Hope this is understandable, any help will be appreciated.
    Last edited by drkidd22; 09-02-2012 at 01:34 PM.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Isn't outbuffer[2] supposed to take intbytes?

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    53
    YeWS

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If you're sure the input value will fit in a single byte, then why are you converting it to an int? You can replace the entire red section with just this:
    Code:
    OUTBuffer[2] = Convert.ToByte(textBox4.Text);
    If it doesn't fit in a single byte, then you'll have to copy all of the bytes instead of just one.
    Code:
    int decValue = Convert.ToInt32(textBox4.Text);
    byte[] intBytes = BItConverter.GetBytes(decValue);
    Array.Copy(intBytes, 0, OUTBuffer, 2, intBytes.Length);
    And then you'd have to change your for() loop of course. Your comment says you're setting the rest of the bytes to 1, but 0xFF isn't 1...:
    Code:
    for(int i = 2 + intBytes.Length;i < 65;++i)
        OUTBuffer[i] = 0xFF;
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    53
    Quote Originally Posted by itsme86 View Post
    If you're sure the input value will fit in a single byte, then why are you converting it to an int? You can replace the entire red section with just this:
    Code:
    OUTBuffer[2] = Convert.ToByte(textBox4.Text);
    If it doesn't fit in a single byte, then you'll have to copy all of the bytes instead of just one.
    Code:
    int decValue = Convert.ToInt32(textBox4.Text);
    byte[] intBytes = BItConverter.GetBytes(decValue);
    Array.Copy(intBytes, 0, OUTBuffer, 2, intBytes.Length);
    And then you'd have to change your for() loop of course. Your comment says you're setting the rest of the bytes to 1, but 0xFF isn't 1...:
    Code:
    for(int i = 2 + intBytes.Length;i < 65;++i)
        OUTBuffer[i] = 0xFF;
    Very nice thanks. I'm more of an Electrical guy and been learning C# little by little.

    Thanks. I'm now able to control the stepper motor over usb using the C# app I've made. You also answered what was going to be my next question about if the input value wouldn't fit into a single byte thanks for your help, this will help a lot in the long run.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. End of user input must be 999
    By naspek in forum C Programming
    Replies: 23
    Last Post: 07-22-2009, 11:04 PM
  2. using user input as a var.
    By nubi in forum C++ Programming
    Replies: 13
    Last Post: 05-06-2003, 12:27 PM
  3. user input
    By unregisterd in forum C Programming
    Replies: 5
    Last Post: 12-30-2002, 05:22 AM
  4. user input
    By hen in forum C Programming
    Replies: 4
    Last Post: 06-29-2002, 04:10 PM
  5. user input
    By hen in forum C Programming
    Replies: 16
    Last Post: 06-27-2002, 11:09 PM