Use this method.
public static byte[] ToBcd(int value){
if(value<0 || value>99999999)
throw new ArgumentOutOfRangeException("value");
byte[] ret=new byte[4];
for(int i=0;i<4;i++){
ret[i]=(byte)(value%10);
value/=10;
ret[i]|=(byte)((value%10)<<4);
value/=10;
}
return ret;
}
This is essentially how it works.
- If the value is less than 0 or greater than 99999999, the value will not match four bytes. More formally, if the value is less than 0 or equal to 10 ^ (n * 2) or more, where n is the number of bytes, the value will not correspond to n bytes.
- :
- , -10 . ( [] .)
- 10.
- 16 10 . ( .)
- 10.
( , 0 , .NET, , , 0. , . , , /, , , , .)