CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2011
    Posts
    59

    I dont want my 00 truncated!!

    Hello,

    Im currently attempting to read data from a serial port and manipulate that data to show what it represents. Although when I try to display the data my 0X00 is being converted into 0. I need the value to be 00.

    I tried reading each byte into an array then checking if they were = 0. Then made a string = 00 (this also gets truncated to 0) in hopes to display the true value.

    Any help would be appreciated, if I cant get these values right my project fails. Thank you again.

  2. #2
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: I dont want my 00 truncated!!

    made a string = 00 (this also gets truncated to 0)
    I don't see how this could ever happen. Perhaps you could post some of your problematic code.

  3. #3
    Join Date
    Mar 2011
    Posts
    59

    Re: I dont want my 00 truncated!!

    private void hBoxData(byte[] h, bool s) // function to write to a text box using an outside thread
    {
    int j = 0;
    string[] str = new string[18];

    for (j = 0; j < 18; j++)
    {
    if (s == true)
    {

    if (j == 3)
    {
    if (h[2] < 10)
    {
    str[2] += h[2];
    }
    if (h[3] < 10)
    {
    str[3] = "0";
    str[3] += h[3].ToString();
    str[2] += str[3];
    }


    tempBox.Text = str[2];
    }
    else if (j == 5)
    {
    if (h[4] < 10)
    {
    str[4] += h[4];
    }
    if (h[5] < 10)
    {
    str[5] = "0";
    str[5] += h[5].ToString();
    }

    pTempBox.Text += (str[4] += str[5]);
    // pTempBox.Text = (h[4] += h[j]).ToString("X");
    }
    else if (j == 7)
    {
    if (h[6] < 10)
    {
    str[6] += h[6];
    }
    if (h[7] < 10)
    {
    str[7] = "0";
    str[7] += h[7].ToString();
    }
    // currentBox.Text = (h[6] += h[j]).ToString();
    currentBox.Text += (str[6] += str[7]);

    }
    else if (j == 9)
    {
    if (h[8] < 10)
    {
    str[8] += h[8];
    }
    if (h[9] < 10)
    {
    str[9] = "0";
    str[9] += h[9].ToString();
    }
    //errorBox.Text = (h[8] += h[j]).ToString();
    errorBox.Text += (str[8] += str[9]);
    }
    else if (j == 10)
    {

    powerBox.Text = h[j].ToString();
    }
    heartBox.Text = " ";

    }
    else if (s == false)
    {
    heartBox.Text += h[j].ToString("x"); // writes each byte to heartBox in hex
    }

    }

    }

    The vars are being passed from a thread that is sending and receiving data every 20ms.
    EG:

    serialPort1.Read(heart, 0, 18);
    heartBox.Invoke(new mydelegate(hBoxData), heart, sw);

    Either way I allow it to display in DEC or in HEX I lose that 0, well in DEC i dont but when I convert it back to hex then try to display I get the 0 truncated.
    EG:
    04 becomes 4, 00 becomes 0 and so on and so forth

    Im pretty new to coding C# so I dont know how to stop the truncating and display the full value. I do understand advanced concepts and learn very quickly so even if you think I wont follow throw it at me!!

    also with the code I have listed my first data display to tempBox the first number is truncated. it should read 400 but reads 00 I have debugged and found that str[2] == 400 so why would it truncate the 4?

    Also any criticism is welcome.
    Last edited by DKS1282; March 18th, 2011 at 01:38 PM. Reason: code update

  4. #4
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: I dont want my 00 truncated!!

    I haven't had time to play with the code yet, but would suggest that you read further on the formatting options with the ToString method. I'm sure there are ways to specify the number of digits.

  5. #5
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: I dont want my 00 truncated!!

    I think zips' suggestion was right on target ....
    http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

    that oughtta getcha goin', DK.

  6. #6
    Join Date
    Mar 2011
    Posts
    59

    Re: I dont want my 00 truncated!!

    Ive tried it. Unless there are ones im missing.


    C,D,E,F,G,N,P,X
    None of thoes work... If there are others I cant seem to find them on MSDN



    EDIT: After reading the link you gave me, it appears .toString("D2"); could work! Thank you both for your time and effort! We will see if this soon works
    Last edited by DKS1282; March 18th, 2011 at 02:21 PM. Reason: I may have been wrong

  7. #7
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: I dont want my 00 truncated!!

    the "x2" doesn't work ?

    I've used it on numerous apps and it's always worked for me in the past ... maybe I need to take a look at the code before revealing my ignorance yet again.

  8. #8
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: I dont want my 00 truncated!!

    Hey DK,

    try it again ... I downloaded and executed your code and the statement

    Code:
    textBox1.Text += h[j].ToString("x2"); // writes each byte to heartBox in hex
    appears to be working for me. I think Zips was correct.

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    Re: I dont want my 00 truncated!!

    Strings don't automatically truncate zeros because they are not zeros in the numerical sense, they are characters. Honestly, your code is really messy, error prone, filled with 'magic numbers, and just plain hard to read. I don't mean that as an insult, but if you want people to review your code it should be easier to comprehend.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured