|
-
March 18th, 2011, 10:07 AM
#1
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.
-
March 18th, 2011, 12:34 PM
#2
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.
-
March 18th, 2011, 12:44 PM
#3
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
-
March 18th, 2011, 01:38 PM
#4
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.
-
March 18th, 2011, 02:16 PM
#5
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.
-
March 18th, 2011, 02:16 PM
#6
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
-
March 18th, 2011, 02:18 PM
#7
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.
-
March 18th, 2011, 02:36 PM
#8
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.
-
March 18th, 2011, 04:56 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|