0

I've got an extension method that converts me ulong into a string value with some kind of encryption. I want to output them as they are just by using Console.WriteLine, in most scenarios it works but there is a problem with values with escapes characters. For example "(V\\\|RN" outputs just "(V\|RN".

var result = id.IdToCode();
Console.WriteLine(result);  

or

Console.WriteLine(id.IdToCode()); 

The method IdToCode returns stringBuilder.ToString()

I've tried many combinations with putting somewhere @ to return the string as it is but without any result. Maybe I should override the default behavior of Console.WriteLine or the stringBuilder.ToString() is the problem here?

Here is a screen of what I mean.

Console output

And below the code of IdToCode method:

        public static string IdToCode(this ulong value)
    {
        const string charArray = @"1234890qwertyuiopbnmQWERTYUasdfghjklzxcvIOPASDFGHJKLZXCVBNM!+={}[]|\<>?@#567$%^&*()-_";
        StringBuilder stringBuilder = new StringBuilder();
        ulong num = value;
        while (num != 0UL)
        {
            ulong index = num % (ulong)charArray.Length;
            num /= (ulong)charArray.Length;
            stringBuilder.Insert(0, charArray[(int)index].ToString());
        }
        return stringBuilder.ToString();
    }

I've changed the char array into different one but the general method it's the same as above.

Community
  • 1
  • 1
Kacper Werema
  • 448
  • 2
  • 5
  • 18

1 Answers1

1

The problem is you need to use the @ in front of the string literal which actually adds the backslash to the StringBuilder.

There is no point in doing @id.IdToCode(), because when the string is returned, it already contains (V\|RN. The tooltip shows \\ because it shows the escaped eversion - meaning the single backslash.

One thing that is certain is that the problem can't be resolved here, but only inside the IdToCode method, where it actually originates.

Compare this (same as your code):

static void Main(string[] args)
{
    var str = IdToCode();
    Console.WriteLine();
}

public static string IdToCode()
{
    return "(\\VN";
}

Hovering over str I see (\\VN - two backslashes, output is just one backslash - which is correct.

And this:

static void Main(string[] args)
{
    var str = IdToCode();
    Console.WriteLine();
}

public static string IdToCode()
{
    return @"(\\VN";
}

Here the tooltip shows "(\\\\VN" which is again correct - there are two actual backslashes and console output is the desired (\\VN

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • "The problem can't be resolved here" . Not true -> https://stackoverflow.com/questions/323640/can-i-convert-a-c-sharp-string-value-to-an-escaped-string-literal – sagi Oct 10 '18 at 10:03
  • Ok, proper wording should probably be "should not be resolved here" :-D . If you are a receiver of a escaped string, how can you tell which backslashes are actual backslashes and which are escapes. If the string is created inside the `IdToCode` method, it should return the correct string itself, otherwise the method is poorly written. – Martin Zikmund Oct 10 '18 at 10:06
  • The thing is the IdToCode method actually returns just one backslash. It is **displayed** as two in the tooltip, but it is there just once. – Martin Zikmund Oct 10 '18 at 10:08