Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi I need to read an int from excel cell and send it in one byte but when I do it with this code:
cert1 = Encoding.Default.GetBytes(
"write" + flg +
dataRow[3].ToString() +
dataRow[4].ToString() +
dataRow[5].ToString().Substring(0,4) +
dataRow[5].ToString().Substring(5,2) +
dataRow[5].ToString().Substring(8,2) +
dataRow[6].ToString() +
dataRow[7].ToString() +
dataRow[8].ToString() +
dataRow[10].ToString().Substring(0, 4) +
dataRow[10].ToString().Substring(5, 2) +
dataRow[10].ToString().Substring(8, 2) +
dataRow[11].ToString());
comport.Write(cert1, 0, cert1.Length);
and debug it, I get two byte for number 12 which in excel cell. It means it thinks 12 as string and returns 1 as one byte and 2 as another. I assume I need to use this Int.Parse() to look at 12 as one int and convert it to binary and after that to byte.
Am I right? If yes how to do it? Thanks
you are getting the bytes from the characters not the ints
for example if you have dataRow[3] = 12
then in your case you will get the bytes for 1 and 2 as you saw.
you need to use
BitConverter.GetBytes(Int.Parse(dataRow[3].ToString()));
for the field
so you have to do this for all the fields and then combine the arrays.
you can use System.Buffer.BlockCopy for that.
in this article there are some examples how to do that
I think that the conversion Byte.Parse() is probably what you are looking for: http://msdn.microsoft.com/en-us/library/4eszwye3.aspx
If you call GetBytes for string 12 you're going to get two bytes: '1' and and '2'. If you want to get a byte with value 12, the you have to convert the string to integer first and then get the bytes of it. There are different ways to parse an integer from a string (int.Parse() or int.TryParse() are two of them).
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am working on modbus communication. I am trying to get the length of a frame which is in actual a string.
while (reader.Read())
{
data.Add(reader["read_param"].ToString());
}
var single = string.Join("",data);
The resultant string is
4A601933906620468040204220442040004200404020602260246
As per documentation, the length is 1B in hex and 27 in decimal
But when I try to get the length int length = combine.Length; I am getting 53. How to get it in HEX?
Any help would be highly appreciated.
You are converting byte to string which converts each single byte to 1 or 2 string characters. So the 53 you are getting from combine.Length is the length of the converted string, the 0x1B from modbus protocol is the length of the byte string. You are getting 53 string characters instead of 54 because one of the bytes was probably a 0x0X so the leading zero is stripped off.
I am unsure of which reader you are using, but if it reads in byte, you can add a counter to determine the length of the modbus message.
When a string is sent, the encoded string is sent in some standard format, the most popular is UTF-8 with which each character can occupy from 1 to 4 bytes.
So the encoded string can have more bytes than characters in the string.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
i have two textboxes 1 called 'mtb_NETPAIE02' and 2 called 'mtb_TAXE02'
the format in the two textboxes is currency/money (double) i want to fix the error on this Code :
if (double.Parse(mtb_NETPAIE02.Text) >= 100001 )
{
mtb_TAXE02.Text = (double.Parse(mtb_NETPAIE02.Text) / 5000 * double.Parse("12")
+ double.Parse("18").ToString("N2")).ToString();
}
12 (12,00) and 18 (18,00) are money but 5000 is int .
how to make this right ?
First of all, you should not be using double if you're working with money; you should be using decimal.
Secondly, you should use the literals for decimal, i.e. with the M suffix.
Finally, you are attempting to add the result of decimal.Parse("18") to the other numeric values, but you are converting it to a string first via ToString("N2"). You should move the latter outside your parens to convert the whole result from the calculation into a string:
if (decimal.Parse(mtb_NETPAIE02.Text) >= 100001M )
{
// notice the `M` in 5000M
mtb_TAXE02.Text = (decimal.Parse(mtb_NETPAIE02.Text) /
5000M * decimal.Parse("12") + decimal.Parse("18")).ToString("N2");
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to store some string code in SQL like 120.002.123 or EXP.120.555. How can I split those strings to increase only the rightmost integer part? I don't want to change other parts of the string.
Ex. 120.002.124 or EXP.120.556
How can I do this in C#?
This might do the trick for you
string str = "EXP.120.556"; //120.002.123
str = String.Join(
".",
str.Split('.')
.Take(
str.Split('.').Length - 1
)
)
+ "." +
(
Convert.ToInt32
(
str.Split('.').LastOrDefault()
) + 1
).ToString();
So here in the code the first String.Join will join the other part of the string with . except of the last part which you want to increament using Take. Then Convert.ToInt32 will convert the last part of the string to a integer using LastOrDefault and then we add 1 to it and again convert it back to string using ToString
What you can do is to use String.Split method and split by . to get each individual number.
string test = "120.002.123";
string[] allparts = test.Split('.');
Then take the last entry and convert it to an integer
int lastNumber = Convert.ToInt32(allparts.Last());
Now you can do what ever you want with that integer.
With a little more research effort you could have solved it on your own.
Like this + this
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm a newbie in C# programming and I need your help on this one.I have a string with a value of "12:45:00" and I would like to convert it into decimal with two decimal places.
Though you can actually use DateTime struct for your case, but since your string does not contain date info, I think the easiest way would be to use string.Split instead.
string[] strs = "12:45:00".Split(':'); //will give you 12, 45, and 00
double val = Convert.ToDouble(strs[0]) + Convert.ToDouble(strs[1]) / 60 + Convert.ToDouble(strs[2]) / 3600;
Then to print it in two decimal values, simply do:
val.ToString("F2"); //you will get 12.75
Or, to get 12.45, then simply do:
double val = Convert.ToDouble(strs[0]) + Convert.ToDouble(strs[1]) / 100; //note 100 here - second doesn't matter here
If your intention is to convert a duration in hours, minutes and (optionally) seconds into hours, you can do that like so:
double answer = TimeSpan.Parse("12:45:00", CultureInfo.InvariantCulture).TotalHours;
Console.WriteLine(answer); // Prints 12.75
This is the value that you can use along with an hourly rate to calculate a gross income.
Note: It would be incorrect to convert 12:45 (hh:mm) into 12.45, because 12:45 is 12.75 hours, not 12.45 hours.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am developing a windows forms application. I have a double value 1.5.That value I need to convert into a two byte array. But when I am converting using BitConverter.GetBytes, getting 8 bytes of data. Please refer my code below.
double d = 1.5;
byte[] array = BitConverter.GetBytes(d);
Double is 8 byte value, so if you have an arbitrary double there's no hope for you; however, if you have some restrictions, e.g. the value is in the [0..100] range and has at most 2 digits after the decimal point, you can encode it:
// presuming source in [0..100] with at most 2 digit after decimal point
double source = 1.5;
// short is 2 byte value
short encoded = (short) (source * 100 + 0.5);
byte[] array = BitConvertor.GetBytes(encoded);
// decode back to double
double decoded = encoded / 100.0;
A double is a 64 bit value that just so happens to be 8 bytes. There's nothing that can be done about this.
A double is 8 bytes in length, so unless you want a specific sub-array out of the 8 you're getting (you probably don't), then this is the wrong way to go about it.
You can cast your variable to a single-precision float. That will of course lose some precision, but you will get 4 bytes instead of 8.
If this is still unacceptable, you need to have an implementation of a half-precision float, which sadly doesn't come out-of-the-box with .NET.
I found an implementation here:
http://sourceforge.net/p/csharp-half/code/HEAD/tree/System.Half
You can use it like this:
var half = (Half)yourDouble;
var bytes = Half.GetBytes(half); // 2 bytes