简体   繁体   中英

How to represent a 5 digit decimal value as a 24 bit value?

I'm trying to convert a 5 digit decimal value (ranging from 00001 to 99999) and somehow represent it as a 24-bit value split into 3 bytes but have tried every conversion and bitshift tactic I know, but keep getting stuck :/

Example: decimal value is 12345, and I need to send 3 hex values [aa][bb][cc], which would consist of:

[aa] - least significant | [bb] - middle | [cc] - most significant

I'm hoping I'm not in over my head and that there is a simple answer, thanks in advance!

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Decimal number = new Decimal() { number = 12345 };
            string output = number.ToString();
        }
    }
    public class Decimal
    {
        public int number { get; set; }
        public override string ToString()
        {
            string output = string.Format("[{0}][{1}][{2}]",
                (number & 0xFF).ToString("X2"),
                ((number >> 8) & 0xFF).ToString("X2"),
                ((number >> 16) & 0xFF).ToString("X2"));
            return output;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM