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

走远了吗. 提交于 2021-01-29 09:20:26

问题


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!


回答1:


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;
        }
    }
}


来源:https://stackoverflow.com/questions/59977551/how-to-represent-a-5-digit-decimal-value-as-a-24-bit-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!