Treat visual representation of integer as hex for placement in guid

We have an obsolete requirement to store what has recently transferred identifier values intto a type guidfor use in types of agnostic ID attributes (mostly old code that used the "globally unique" part guidto contain all possible identifiers in one column / field) .

In connection with this requirement needed to implement integer identifier entites in guida human-readable way . This is important and is currently what prevents me from working directly with byte values.

I currently have the following:

    public static byte[] IntAsHexBytes(int value)
    {
        return BitConverter.GetBytes(Convert.ToInt64(value.ToString(), 16));
    }

    public static Guid EmbedInteger(int id)
    {
        var ib = IntAsHexBytes(id);

        return new Guid(new byte[]
            {
                0,0,0,0,0,0,1,64,ib[7],ib[6],ib[5],ib[4],ib[3],ib[2],ib[1],ib[0]
            });
    }

int (value.ToString()), long (Convert.ToInt64(value.ToString(), 16)) long byte[] guid .

, int 42, 42 long, 66, 66 , guid :

"00000000-0000-4001-0000-000000000042"

int of 379932126 :

"00000000-0000-4001-0000-000379932126"

, , guid 12 , 42 ( 66).

30% -40% , new Guid(string), , , - .

, , , .

, , . , SO, , , , , .

0 int.MaxValue.

: , , :

string s = string.Format("00000000-0000-4001-0000-{0:D12}", id);
return new Guid(s);

, 30%.

+5
2

, , . , .:)

public static Guid EmbedInteger(int id)
{
    byte[] bytes = new byte[8];
    int i = 0;

    while (id > 0)
    {
        int remainder = id%100;
        bytes[i++] = (byte)(16*(remainder/10) + remainder%10);
        id /= 100;
    }

    return new Guid(0, 0, 0x4001, bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]);
}

Adam Houldsworth: : :

int remainder = id % 100;
bytes[0] = (byte)(16 * (remainder / 10) + remainder % 10);
id /= 100;
if (id == 0) return;
remainder = id % 100;
bytes[1] = (byte)(16 * (remainder / 10) + remainder % 10);
id /= 100;
if (id == 0) return;
remainder = id % 100;
bytes[2] = (byte)(16 * (remainder / 10) + remainder % 10);
id /= 100;
if (id == 0) return;
remainder = id % 100;
bytes[3] = (byte)(16 * (remainder / 10) + remainder % 10);
id /= 100;
if (id == 0) return;
remainder = id % 100;
bytes[4] = (byte)(16 * (remainder / 10) + remainder % 10);
+1

, , . , , , .:)

public static Guid EmbedInteger(int id)
{
    string guid = string.Format("00000000-0000-4001-0000-{0,12:D12}", id);
    return new Guid(guid);
}

12: D12, 12 .

+1

All Articles