in Education by
I want to convert an int to a byte[2] array using BCD. The int in question will come from DateTime representing the Year and must be converted to two bytes. Is there any pre-made function that does this or can you give me a simple way of doing this? example: int year = 2010 would output: byte[2]{0x20, 0x10}; JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
static byte[] Year2Bcd(int year) { if (year < 0 || year > 9999) throw new ArgumentException(); int bcd = 0; for (int digit = 0; digit < 4; ++digit) { int nibble = year % 10; bcd |= nibble << (digit * 4); year /= 10; } return new byte[] { (byte)((bcd >> 8) & 0xff), (byte)(bcd & 0xff) }; } Beware that you asked for a big-endian result, that's a bit unusual.

Related questions

0 votes
    In C#, int and Int32 are the same thing, but I've read a number of times that int is preferred ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 31, 2022 in Education by JackTerrance
0 votes
    In C#, int and Int32 are the same thing, but I've read a number of times that int is preferred ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    In C#, int and Int32 are the same thing, but I've read a number of times that int is preferred ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I want to convert the matched expression in string or int. But in .NET Framework I do not find ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Which of the following is a method of wrapper Float for converting the value of an object into byte? ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Thinking like a simple array: Console.WriteLine("Number: "); int x = Convert.ToInt32(Console.ReadLine()) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    Thinking like a simple array: Console.WriteLine("Number: "); int x = Convert.ToInt32(Console.ReadLine()) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    Thinking like a simple array: Console.WriteLine("Number: "); int x = Convert.ToInt32(Console.ReadLine()) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    Thinking like a simple array: Console.WriteLine("Number: "); int x = Convert.ToInt32(Console.ReadLine()) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    An expression involving byte, int, and literal numbers is promoted to which of these? (a) int (b) ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 28, 2021 in Education by JackTerrance
0 votes
    Which of the following is method of wrapper Integer for converting the value of an object into int? (a ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    My application is receiving email through SMTP server. There are one or more attachments in the email and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    My application is receiving email through SMTP server. There are one or more attachments in the email and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    My application is receiving email through SMTP server. There are one or more attachments in the email and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    what is the easiest way to convert a Blob into a byte array?I am using MYSQL and i want to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
...