![]() |
От: | jyuyjiyuijyu | |
Дата: | 19.01.14 06:00 | ||
Оценка: | -1 |
String^ str = "Nish wrote this book for Manning Publishing";
interior_ptr<Char> ptxt = const_cast< interior_ptr<Char> >(
PtrToStringChars(str));
interior_ptr<Char> ptxtorig = ptxt;
while((*ptxt++)++);
Console::WriteLine(str);
while((*ptxtorig++)--);
Console::WriteLine(str);
You go through the string using a while-loop that increments the pointer as well as
each character until a nullptr is encountered, because the underlying buffer of a
String object is always nullptr-terminated. Next, when you use Console::Write-
Line on the String object, you can see that the string has changed to
Ojti!xspuf!uijt!cppl!gps!Nboojoh!Qvcmjtijoh
You’ve achieved encryption! (Just kidding.) Because you saved the original pointer
in ptxtorig, you can use it to convert the string back to its original form using
another while loop. The second while loop increments the pointer but decrements
each character until it reaches the end of the string (determined by the
nullptr). Now, when you do a Console::WriteLine, you get the original string:
Nish wrote this book for Manning Publishing