none
Output Byte and Bit RRS feed

  • Question

  • Hi,

    I have a byte, m_Msg.DATA[6] <--  part of an array

    I would like to output this to a textbox. --> 1E <--- What I would like to see in the text box.

    I would also like to output each bit so I can monitor it's changes. --> 0 0 0 1 1 1 1 0

    The byte changes and I would like to witness this change in both terms.

    The hardware and background program updates the changes in this message every millisecond.

    The changes don't happen every millisecond.

    I would like to see what changes as I make changes to the hardware.


    Alert for thread


    Wednesday, May 15, 2019 12:57 PM

All replies

  • Hello,

    Here is a mock that might work for you.

    byte[] bytes = { 1, 2, 3, 4 };
    bytesTextBox.Text = BitConverter.ToString(bytes).Replace("-", " ");


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, May 15, 2019 2:16 PM
    Moderator
  • To view a byte as a binary  string then change the base number in Convert.ToString. The following is an example of one byte incremented in a timer, each of the bytes in your array would need handling this way.

     byte mybyte;
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                var binval = Convert.ToString(mybyte, 2);//base2
                textBox1.Text = binval;
                textBox2.Text = mybyte.ToString("X");
                mybyte++;
            }


    • Edited by Jeff_T_1234 Wednesday, May 15, 2019 2:34 PM
    Wednesday, May 15, 2019 2:31 PM
  • You're not going to get ms update speed in a UI. It simply isn't going to happen in a non-real time OS like Windows. There are too many factors involved such as the threading scheduler, message queue behavior, etc. The fastest frequency I've ever seen on Windows with a soft realtime PLC control system (running at RT priority) was 10 ms to update all the data in memory + the time it took to update the UI. Windows 10 (from what I can remember) uses between 20 and 120 ms (depends on foreground/background and client/server) for each thread so your thread isn't even guaranteed to be running for that many ms intervals. Again, standard Windows isn't real time.

    To output in hex use the "X" specifier for string format.

    var hex = value.ToString("X");

    For binary there is no current specifier for it. However Convert's ToString method allows you to specify a base so you can do it that way (and hex as well). You'll have to manipulate the string after that to get the exact format you want.

    var binary = Convert.ToString(value, 2);


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, May 15, 2019 2:32 PM
    Moderator
  • What is going to be updating this data?  Is it coming from a piece of hardware, or from another thread in your process?

    > I am looking for changes in this message every millisecond

    I don't think you understand what you're asking.  The average LCD monitor only has about a 75Hz refresh rate, so you can't see screen changes any more often than about 13ms.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, May 15, 2019 6:18 PM

  • I would like to output this to a textbox. --> 1E

    I would also like to output each bit so I can monitor it's changes. --> 0 0 0 1 1 1 1 0


    Note that if you use ToString with the number base 2 as in the previous examples
    it will omit leading zeros. So the output from 0x1E will be 11110.

    To show all 8 bits you can pad the string:

    var binary = Convert.ToString(value, 2).PadLeft(8, '0');
    
    

    This will show 00011110

    - Wayne

    Wednesday, May 15, 2019 10:59 PM

  • The byte changes and I would like to witness this change on both levels.

    I am looking for changes in this message every millisecond

    In addition to the hardware and OS constraints mentioned by others, I have to 
    wonder if there is any human who can see, read and interpret *any* textual 
    visual image which is changing a thousand times per second.

    - Wayne

    Thursday, May 16, 2019 4:15 AM
  • This Doesn't Work

    It gives an Error saying cannot convert byte to byte[].


    Alert for thread

    Thursday, May 16, 2019 8:11 AM
  • This Doesn't Work

    It gives an Error saying cannot convert byte to byte[].

    Apparently you are referring to Karen's suggestion, which uses a byte array.

    If you want to show the hex for a single byte then use the examples that others
    posted which use "X" such as:

    textBox1.Text = m_Msg.DATA[6].ToString("X");
    

    - Wayne

    Thursday, May 16, 2019 8:46 AM
  • This Doesn't Work

    It gives an Error saying cannot convert byte to byte[].

    Apparently you are referring to Karen's suggestion, which uses a byte array.

    If you want to show the hex for a single byte then use the examples that others
    posted which use "X" such as:

    textBox1.Text = m_Msg.DATA[6].ToString("X");

    - Wayne

    Thank You all for your answers, I have solved my problem with a combination of your answers.

    var by6 = Convert.ToString(m_Msg.DATA[6], 2);
    textBox10.Text = by6.PadLeft(8, '0');
    textBox11.Text = m_Msg.DATA[6].ToString("X").PadLeft(2, '0');


    Alert for thread

    Thursday, May 16, 2019 9:21 AM
  • Note that if you want to zero pad numbers when calling ToString/String.Format you don't need to use PadLeft. The format specifiers support padding already.

    //Gets 05
    5.ToString("X2");
    
    //Gets 45
    0x45.ToString("X2");
    
    


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, May 16, 2019 1:45 PM
    Moderator