fork download
  1. using static System.Console;
  2. using System.Text;
  3. using System.Diagnostics;
  4.  
  5. public class Program {
  6. public static void Main() {
  7. var sw = new Stopwatch();
  8. sw.Start();
  9. for (var i = 0; i < 1000; i++) ASCII_binary("teste");
  10. sw.Stop();
  11. WriteLine(sw.ElapsedTicks);
  12. sw.Restart();
  13. for (var i = 0; i < 1000; i++) ASCII_binaryIneficiente("teste");
  14. sw.Stop();
  15. WriteLine(sw.ElapsedTicks);
  16. }
  17. public static string ASCII_binary(string texto) {
  18. var converted = "";
  19. byte[] byteArray = Encoding.ASCII.GetBytes(texto);
  20. for (var i = 0; i < byteArray.Length; i++) {
  21. for (var j = 0; j < 8; j++) {
  22. converted += (byteArray[i] & 0x80) > 0 ? "1" : "0";
  23. byteArray[i] <<= 1;
  24. }
  25. }
  26. return converted;
  27. }
  28. public static string ASCII_binaryIneficiente(string texto) {
  29. var converted = "";
  30. byte[] byteArray = Encoding.ASCII.GetBytes(texto);
  31. for (var i = 0; i < byteArray.Length; i++) {
  32. for (var j = 0; j < 8; j++) {
  33. converted += (byteArray[i]) > 127 ? "1" : "0";
  34. byteArray[i] *= 2;
  35. }
  36. }
  37. return converted;
  38. }
  39. }
  40.  
  41. //https://pt.stackoverflow.com/q/101050/101
Success #stdin #stdout 0.03s 21200KB
stdin
Standard input is empty
stdout
57594
49659