PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 11, 2022

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

 August 11, 2022     binary, bit, c#, decimal, hex     No comments   

Issue

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!


Solution

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


Answered By - jdweng
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing