Integer conversion to Binary Coded Decimal

  • Thread starter Thread starter Kenney
  • Start date Start date
K

Kenney

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
 
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
 
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?
 
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
 
Thanks stefan,

I will try your suggestion and get back to you.

Stefan L said:
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
 
Stefan said:
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;
}

Or you might want to loop the length of the buffer, to avoid getting an
IndexOutOfRangeException if the value doesn't fit in the buffer:

public static void Long2BCD(long value, byte[] buffer) {
for (int i = buf.Length - 1; i >= 0; i--) {
buffer = (byte) ((value % 10) | (((value % 100) / 10) << 4));
value /= 100;
}
if (value != 0) throw new FormatException("BCD represetiaton is too
large for the buffer.");
}

:)
 
Actually you're right Göran, throwing an exception is right if you are
not sure about the value domain you expect the values to be in. Anyways,
an IndexOutOfRange-Exception will be thrown anyways... ;-)

I assumed that the caller does know the maximum length of her output
(i.e. for the length of IP packets 4 bytes is enough, as you must not
exceed 1500 bytes...), as you normally also have to know how many bytes
you do have to send out.

After all, the complete fill-the-buffer approach aims at fast
processing, that is why I disregarded exception-throwing and expect the
caller to do all right (the design-by-contract-thing...).

Regards,
Stefan



Göran Andersson said:
Stefan said:
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;
}

Or you might want to loop the length of the buffer, to avoid getting an
IndexOutOfRangeException if the value doesn't fit in the buffer:

public static void Long2BCD(long value, byte[] buffer) {
for (int i = buf.Length - 1; i >= 0; i--) {
buffer = (byte) ((value % 10) | (((value % 100) / 10) << 4));
value /= 100;
}
if (value != 0) throw new FormatException("BCD represetiaton is too
large for the buffer.");
}

:)
 
Back
Top