Skip to content

Guid.ToString() changes byte order  #514

@haeberle

Description

@haeberle

System.Guid.ToString() changes the order of the bytes.

nanoFramework area: lib-CoreLibrary/source/nanoFramework.CoreLibrary/System/Guid.cs

VS version:

VS extension version:

Target:

Firmware image version:1.2.6-preview.16

Device capabilities output:

Description

The first three marked (--->>) for iterations count backward, the last two forward. The issue is, that in the first 3 loops the chars array and the bytes array are running contrary. Means the first bytes in chars[] are from the third byte in the bytes array, this results in a wrong byte order.
`

       public override string ToString()
       {
        // default format is dddddddd-dddd-dddd-dddd-dddddddddddd

        byte[] bytes = ToByteArray();

        char[] chars = new char[36];

        int i = -1, j;

--->> for (j = 3; j >= 0; --j)
        {
            chars[++i] = HexToChar((bytes[j] & 0xF0) >> 4);
            chars[++i] = HexToChar((bytes[j] & 0x0F));
        }

        chars[++i] = '-';
--->> for (j = 5; j >= 4; --j)
        {
            chars[++i] = HexToChar((bytes[j] & 0xF0) >> 4);
            chars[++i] = HexToChar((bytes[j] & 0x0F));
        }

        chars[++i] = '-';
--->>for (j = 7; j >= 6; --j)
        {
            chars[++i] = HexToChar((bytes[j] & 0xF0) >> 4);
            chars[++i] = HexToChar((bytes[j] & 0x0F));
        }

        chars[++i] = '-';

--->> for (j = 8; j <= 9; ++j)
{
chars[++i] = HexToChar((bytes[j] & 0xF0) >> 4);
chars[++i] = HexToChar((bytes[j] & 0x0F));
}

        chars[++i] = '-';

--->> for (j = 10; j <= 15; ++j)
{
chars[++i] = HexToChar((bytes[j] & 0xF0) >> 4);
chars[++i] = HexToChar((bytes[j] & 0x0F));
}

        return new string(chars);
    }

`

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions