Hi Kennedy,
well, that's seems pretty complicated.
I can see yout input value is <int>, but I'm not sure about what you
need as output... Do you want a byte array to send out?
Maybe something like this helps you:
public static int Long2BCD(long val, byte[] buf)
{
int idx = buf.Length;
do {
idx--;
buf[idx] = (byte) ((val % 10) | (((val % 100) / 10) << 4));
val /= 100;
} while (val != 0);
while (idx > 0) { idx--; buf[idx] = 0; }
return idx;
}
The function above converts a long to a byte array, like this:
input: val = 1234567890;
output:
buf[0] = 0x00
buf[1] = 0x00
buf[2] = 0x00
buf[3] = 0x12
buf[4] = 0x34
buf[5] = 0x56
buf[6] = 0x78
buf[7] = 0x90
HTH,
Stefan
At the moment I seem to be doing the following:
int ------> string
string ----> char []
foreach (char c in char [])
{
c -------> byte
byte ----> binary string
binary string -----> string []
}
then combine the items in the string [] into 8 character strings and convert
to hex. I am getting the conversion but it seems like a lot of work for
something I thought the framework would provide.
Any thoughts?
Stefan L said:
Hmmm...
What exactly do you mean with a smart way? Whether there's a function in
the framework doing it for you? I guess ... no. But I'm not sure here.
On the other hand: Why worrying about a function that requires 10 lines
of code?
regards,
Stefan
Kenney schrieb:
Hi All,
I work with communications protocols (namely TCPIP) and I have been asked to
transmit the length of a message in Binary Coded Decimal. For example a
message of length (int) 356 will be transmitted as 0x03, 0x56.
Is there a smart way to to this?
Cheers,
Kenney