Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to convert 3 byte information to 2 byte

vishweshgm

Member level 4
Joined
Dec 17, 2021
Messages
74
Helped
5
Reputation
10
Reaction score
7
Trophy points
8
Activity points
949
I have a 3byte unique ID number for chip. I want to read this unique chip number and convert it to 2byte and assign this same chip a new 2Byte ID. I know that it is not logical to fit 3byte info in 2 byte, but I want to know if any mathematical way available where I can convert most 3byte numbers to 2 byte number. I also want to know, in the mathematical way proposed, what combination of 3 byte numbers might result in same 2byte number after conversion. Based on that drawback I want to take decision if I need to convert to 2 byte or not.

For eg.

eg1: Assume I have 3byte number as {B2,B1,B0}. I can make rule that, when 3bytes are read in code, if B2>0, then do not do conversion. else, convert it to 2 bytes.

So if 2 chips having 3byte unique id as 0xAB1234 and 0x001234, only chip2's unique ID will be changed to 2 bytes.

Now if I have 100,000 chips with me out of which I take 60 chips and run this rule. Now if all chips had B2>0, then I will end up having no chip converted to 2 bytes at all, which is bad.

So I am looking for a some other method where my probability to convert to unique ID increases.
 
Hi,

Using the original 3 byte UID will result in non unique IDs. This means it is quite expectable that there will be devices with identical IDs.
You can't predict which ones are identical. It may be the first with the 8th, or the 12345th (both random).

With 2 bytes you can have up to 65536 different UID, this is the limit.
--> So if you want to be sure that all your generated IDs are unique, then I recommend to use your own UID generator (independent of the original UIDs).

Klaus
 
Hello!

You can certainly find a way to convert a 3 byte ID into 2 bytes. But you have to loose something.

Now what you are saying is not clear. In your example {B2, B1, B0}. If B2 is not 0 you don't do the
conversion. Fine. Now what can you do with these IDs? Nothing. For instance, if you want it to become
an ID of some hardware, you have to give to that piece of hardware 3 bytes in case the ID is 3 bytes,
and then if you have "converted" it to 2 bytes, then it will be stored in 3 bytes anyway.

Beside this, you have 2^24 possible 3 byte identifiers. And among all these, you can save one byte
only for the 2^16 first IDs (i.e. those which anyway can fit in 2 bytes), So you will have 2^16 unique
2-byte IDs, and 2^24-2^16 3-byte ids. You save nothing.

It's just a pipe dream of the allmighty data compression algorithm, the specs of which states that it can compress any sequence of bytes by at least one bit. It's obvious that it can't exist.

By the way, what's the purpose of all this headache?

Dora.
 
Hi,

In general, you could go about converting bytes to integers and back to bytes as follows:

var input = new byte [] { 0x45, 0x67, 0x89 };

// Depending on byte order, use either the first or second conversion
var converted1 = ((int)input[0] << 16) | ((int)input[1] << 8) |
(int)input[2];
var converted2 = ((int)input[2] << 16) | ((int)input[1] << 8) |
(int)input[0];

// Not sure what kind of scaling you want, here I just shift right
var scaled1 = converted1 >> 8;
var scaled2 = converted2 >> 8;

// Convert back to a byte array
var output1 = new byte [] { (byte)(scaled1 >> 8), (byte)(scaled1 & 0xff) };
var output2 = new byte [] { (byte)(scaled2 & 0xff), (byte)(scaled2 >> 8) };

Hope it helps!
 

LaTeX Commands Quick-Menu:

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top