Erroneous Coding in TextBox

0

A curious topic with a TextBox in Windows Forms is happening to me. I use a windows form for encrypting and decrypting strings, the case is that I have seen that if in the TextBox I write a character "\" internally saved two! For example if I enter 12 \\ 34 it encrypts me: the value that is stored internally in the textbox is 12 \\\\ 34

Is there any way to prevent the TextBox control from performing this type of behavior, that is, to display on the screen the real value that contains the .Text property and not its "escaped" values?

    
asked by U. Busto 10.08.2017 в 10:11
source

2 answers

2

It is a question of how to represent "the text that is stored internally".

In String / character literals, the representation of the string \ is \ , but that does not mean that there are two bars; is because \ is a control character and is always associated with the following character ( \n is line break, \" is the character " - to prevent the compiler from confusing it with the end of the String literal -, and \ is the character \ ).

The debugger / inspector simply shows you the same representation, as if it were a literal (note that adds the quotes at the beginning and at the end, which are not part of the entered value either).

A check of what I say would be to show the length of the text you are dealing with (in your example, it would be 6 and not 8).

    
answered by 10.08.2017 в 10:29
1

Good morning,

The solution to your problem is to use the Replace method by putting another character other than '\' for example '/':

//A la hora de encriptar reemplazamos los '\' por '/'
txt_destino.Text = AESThenHMAC.encripta(txt_origen.Text.Replace('\','/'));

//A la hora de desencriptar lo hacemos a la inversa
txt_origen.Text = AESThenHMAC.desencripta(txt_destino.Text).Replace('/','\');

In this way you always get the string with the values you want without interfering with the ones you add the code to distinguish them from normal characters.

    
answered by 10.08.2017 в 10:47