Question

I have generated a block of MAC addresses and need to store them in a Oracle 11gR2 table. I created a schema via SQL Developer Version 4.0.0.13 that looks like the following:

CREATE TABLE "USER"."MAC_DB" 
(   
    "MAC_ADDRESS" VARCHAR2(20 BYTE) NOT NULL ENABLE, 
    "STATUS" VARCHAR2(20 BYTE) DEFAULT 'Available' NOT NULL ENABLE, 
    "PO_NUMBER" VARCHAR2(20 BYTE), 
     CONSTRAINT "MAC_DB_PK" PRIMARY KEY ("MAC_ADDRESS")

) 

Inserting my generated data has resulted in a series of duplicate values, of which I am very confused about.

Below is the code I used to generate the mac addresses. It's not pretty, can be improved upon and contains a bug.

I am looking for some guidance into where the bug happens to be:

public List<string> generateMACAddresses(string prefix)
{
    List<string> addresses = new List<string>();
    for (int i = 0x00000; i <= 0xFFFFF; i++)
    {                
        addresses.Add(prefix+i.ToString("X").PadRight(5,'0'));

        using (var sw = new StreamWriter("macAddresses.txt", append: true))
        {                     
            sw.WriteLine(mac);
        }
    }         

    return addresses;            
}

The method is called with:

generateMACAddresses("AB2B3B4");

I'm expecting to generate 1,048,576 but after importing the resulting txt file I end up with 646,574 only.

Was it helpful?

Solution

I think you should be using PadLeft instead of PadRight.

Take for instance:

  1.PadRight(5, '0'); // 10000
 10.PadRight(5, '0'); // 10000 (again)
100.PadRight(5, '0'); // 10000 (and again)

You should do PadLeft in order to avoid duplicates

  1.PadLeft(5, '0'); // 00001
 10.PadLeft(5, '0'); // 00010
100.PadLeft(5, '0'); // 00100
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top