Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Given 3 Bytes of Data - How to convert to 6 digit ID code?

Posted on 2013-06-20
11
404 Views
Last Modified: 2013-06-21
I am given 3 bytes defined as:

MSB_ID
ID
LSB_ID

Obviously, MSB and LSB stand for Most Significant Byte and Least Significant Byte.

How can I derive a 6-digit integer ID code from this in C#?

I have a similar operation in the program that calculates from just an MSB and an LSB to get a resultant integer:

int timer = Convert.ToInt32(msb, 16) << 8 | Convert.ToInt32(lsb, 16);

Open in new window


However, with the added 'middle' byte, I am a bit lost.
0
Comment
Question by:BullfrogSoftware
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 27

Expert Comment

by:d-glitch
ID: 39262611
The formula to convert three bytes (24 bits) to binary is:

          MSB_iD*256 ²  +  MID_ID*256  +  LSB_ID
0
 
LVL 1

Author Comment

by:BullfrogSoftware
ID: 39262622
To binary?  I am looking for a 6-digit id code, such as '123456' .. to be derived from the 3 given bytes.
0
 
LVL 27

Accepted Solution

by:
d-glitch earned 250 total points
ID: 39262629
The largest number you can specify is (2^24 - 1) ==>  16,777,215 which is more than 6 digits.

Perhaps you are supposed to convert each byte into two hexadecimal digits.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39262632
You can't guarantee to be able to do it for all possible ids using only digits.
With three bytes, there are 16777216 different possible IDs. So you would need 8 digits unless you knew something special about the data (like MSB_ID is only ever 1-50).
If you want a 6 character hex number (0-9 and A-F), then that would work for three bytes.

If neither of the above apply, it is literally impossible. The other thing you could do (possibly) is just assign IDs and keep a mapping somewhere. But there is no way to guarantee a unique six digit decimal number for three bytes.
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 39262643
An error in the first comment:

     The formula to convert three bytes (24 bits) to decimal is ....
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 39262650
To convert a byte into two hex digits you do an integer division by 16 and take the quotient and the remainder.
0
 
LVL 84

Expert Comment

by:ozo
ID: 39263283
Does it matter what 6 digits you get out of what MSB_ID ID LSB_ID values?
If so, can you give us an example of the conversion you want?
0
 
LVL 1

Author Comment

by:BullfrogSoftware
ID: 39263312
I will actually close this question.  What the customer wanted was as follows:

3 bytes given, convert bytes to strings, concatenate to create hex string, convert string to int

string hexVal = msb + b + lsb;
int i = Convert.ToInt32(hexVal, 16);

Open in new window


It was that simple.
0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 250 total points
ID: 39263368
That will be up to 8 digits. If that's fine with the customer, then great!

Um, if msb, b, and lsb are bytes, won't they get converted to integers when you add them?
3 4 5 and 2 2 8 will yield the same result.
You probably want something like msb.ToString("000") + b.ToString("000") + lsb.ToString("000")

Or msb*256*256 + b*256 + lsb
0
 
LVL 1

Author Comment

by:BullfrogSoftware
ID: 39263374
I mentioned before the code "convert bytes to strings" ... this takes place before they are passed to the code above.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39263385
So you were saying that the conversion happened before your code. Got it. Glad you got it working.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question