C#idiomatic C#
ca1872fundamentals/code-analysis/quality-rules

Code analysis

CA1872: Prefer 'Convert.ToHexString' and 'Convert.ToHexStringLower' over call chains based on 'BitConverter.ToString'

Learn about code analyzer rule CA1872 - Prefer 'Convert.ToHexString' and 'Convert.ToHexStringLower' over call chains based on 'BitConverter.ToString'

CA1872 Performance enabled by default in .NET 10: As suggestion
Fix is breaking or non-breaking Non-breakingApplicable languages C# and Visual Basic

Cause

A call to System.BitConverter.ToString followed by a call to System.String.Replace to remove dashes is used to encode bytes to a hexadecimal string representation. This rule also fires if System.String.ToLower is used in the call chain.

Rule description

Use System.Convert.ToHexString or System.Convert.ToHexStringLower when encoding bytes to a hexadecimal string representation. These methods are more efficient and allocation-friendly than using System.BitConverter.ToString in combination with System.String.Replace to remove dashes and System.String.ToLower.

How to fix violations

To fix a violation of this rule, replace the call chain with either System.Convert.ToHexString or System.Convert.ToHexStringLower.

Example

The following code snippet shows a violation of CA1872:

csharp
using System;
using System.Text;

class HelloWorldEncoder
{
    private readonly byte[] _data = Encoding.ASCII.GetBytes("Hello World");

    public string Encode()
    {
        return BitConverter.ToString(_data).Replace("-", "");
    }

    public string EncodeToLower()
    {
        return BitConverter.ToString(_data).Replace("-", "").ToLower();
    }
}
vb
Imports System
Imports System.Text

Class HelloWorldEncoder
    Private ReadOnly _data As Byte() = Encoding.ASCII.GetBytes("Hello World")

    Public Function Encode() As String
        Return BitConverter.ToString(_data).Replace("-", "")
    End Function

    Public Function EncodeToLower() As String
        Return BitConverter.ToString(_data).Replace("-", "").ToLower()
    End Function
End Class

The following code snippet fixes the violation:

csharp
using System;
using System.Text;

class HelloWorldEncoder
{
    private readonly byte[] _data = Encoding.ASCII.GetBytes("Hello World");

    public string Encode()
    {
        return Convert.ToHexString(_data);
    }

    public string EncodeToLower()
    {
        return Convert.ToHexStringLower(_data);
    }
}
vb
Imports System
Imports System.Text

Class HelloWorldEncoder
    Private ReadOnly _data As Byte() = Encoding.ASCII.GetBytes("Hello World")

    Public Function Encode() As String
        Return Convert.ToHexString(_data)
    End Function

    Public Function EncodeToLower() As String
        Return Convert.ToHexStringLower(_data)
    End Function
End Class

When to suppress warnings

It is safe to suppress a warning from this rule; however, we recommend that you use either System.Convert.ToHexString or System.Convert.ToHexStringLower.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

csharp
#pragma warning disable CA1872
// The code that's violating the rule is on this line.
#pragma warning restore CA1872

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

ini
[*.{cs,vb}]
dotnet_diagnostic.CA1872.severity = none

For more information, see How to suppress code analysis warnings.