Advertisement
GameEntity903

Language Conversion in AHK

Nov 26th, 2022 (edited)
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. /**
  2. 1st set of changes
  3. Added a function (binary2dec) as suggested by @MikeyWW
  4. Changed the binary values given as input as they were incorrect for an expected value of A (100001 to 1000001)
  5. Added another test sequence in the hotkey corresponding to the new function added to the script
  6. Removed the percent (%) signs around the pos in the SubStr function
  7. 2nd set of changes
  8. Added a function (binary2dec) as suggested by @MikeyWW and changed the name of the original binary2dec to binarytodec
  9. Added another test sequence in the hotkey corresponding to the new function added to the script
  10. 3rd set of changes
  11. Removed a function (bintodec)
  12. Added credits to the people who created the function
  13. Removed val 3
  14. Added 2 test sequences to ensure no problem is occuring
  15. Refined 2 functions (BintoASC and ASCtoBin)
  16. Removed percent (%) signs wherever they were causing problems
  17. 4th set of changes
  18. */
  19. !+t::
  20. val := ASCtoBin("HEloW")
  21. OutputDebug, The value of val in Binary is %val%
  22. val2 := BintoASC(val)
  23. OutputDebug, The value of val in ASCII is %val2%
  24. binary := " 110000 110001 110010 "
  25. For each, word in StrSplit(Trim(binary), " ")
  26. If (word > "")
  27. OutputDebug, % Value of bin2dec is Chr(bin2dec(word))
  28. OutputDebug, % Value of dec2bin is dec2bin((Asc(65)))
  29. OutputDebug, % bina7 := ASCIItoBin7("E")
  30. OutputDebug, % Bin7toASCII(bina7)
  31. OutputDebug, % Bin7toASCII(val2)
  32. Return
  33.  
  34. BintoASC(input) ; GameNtt
  35. {
  36. For each, word in StrSplit(Trim(input), " ")
  37. If (word > "")
  38. {
  39. ;idk how to find the number of sequences of bits and how to take each individual sequence of bits as input so ill assume that in2 is the word extracted and input is the input
  40. in2 := word
  41. lenw := StrLen(in2)
  42. OutputDebug, The value of lenw is %lenw%
  43. while(lenw<7)
  44. {
  45. in2 := 0 . in2
  46. lenw++
  47. OutputDebug, The value of lenw is %lenw%
  48. }
  49. OutputDebug, The value of lenw is %lenw%
  50. power := 6
  51. output := 0
  52. pos := 1
  53. loop 7
  54. {
  55. val := SubStr(in2, pos, 1)
  56. output2 := val * (2**power)
  57. output := output + output2
  58. power--
  59. pos++
  60. OutputDebug, In Binary to ASCII, The value of val is %val%, the value of output2 is %output2%, the value of output is %output%, the value of power is %power%, the value of pos is %pos%.
  61. }
  62. outputchr := Chr(output)
  63. outputstr := outputstr . outputchr
  64. ; this would be where the loop ends
  65. OutputDebug, The value of outputstr is %outputstr%, the value of outputchr is %outputchr%, the value of word is %word%.
  66. }
  67. Return outputstr
  68. }
  69.  
  70. ASCtoBin(input) ; GameNtt
  71. {
  72. lenstr := StrLen(input)
  73. outputfin := ""
  74. pos := 1
  75.  
  76. OutputDebug, %lenstr%
  77. Loop, %lenstr%
  78. {
  79. ;power := 6
  80. in2 := ""
  81. in2 := SubStr(input, pos, 1)
  82. in3 := Asc(in2)
  83. loop 7
  84. {
  85. outputw := ""
  86. pow := 2**(7-(A_Index))
  87. if (in3>=pow)
  88. {
  89. outputfin := outputfin . 1
  90. in3 := in3 - pow
  91. }
  92. Else
  93. {
  94. outputfin := outputfin . 0
  95. }
  96. ;power--
  97. OutputDebug, In ASCII to Binary, The value of in2 is %in2%, the value of in3 is %in3%, the value of outputfin is %outputfin%, the value of pos is %pos%, the value of pow is %pow%.
  98. }
  99. pos++
  100. outputfin := outputfin . " "
  101. OutputDebug, The value of outputfin is %outputfin%
  102. }
  103. Return outputfin
  104. }
  105.  
  106. bin2dec(x) ; https://www.autohotkey.com/board/topic/49990-binary-and-decimal-conversion/
  107. {
  108. Return (StrLen(x) > 1 ? bin2dec(SubStr(x, 1, -1)) << 1 : 0) | SubStr(x, 0)
  109. }
  110.  
  111. dec2bin(x) ; https://www.autohotkey.com/board/topic/49990-binary-and-decimal-conversion/
  112. {
  113. Return (StrLen(x)>1 ? dec2bin(SubStr(x,1,-1))<<1:0)|SubStr(x,0)
  114. }
  115.  
  116. ASCIItoBin7(asciiStr) ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=110891&p=493187#p493187
  117. {
  118. bin7 := ""
  119. Loop, parse, asciiStr
  120. {
  121. ascii := Asc(A_LoopField)
  122. if (ascii > 127)
  123. continue
  124. b := ""
  125. Loop 7
  126. b := (ascii >> (A_Index - 1)) & 1 . b
  127. bin7 .= b . " "
  128. }
  129. Return RTrim(bin7)
  130. }
  131.  
  132. Bin7toASCII(bin7) ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=110891&p=493187#p493187
  133. {
  134. asciiStr := ""
  135. while RegExMatch(RegExReplace(bin7, "[^10]"), "O)[10]{7}", m, m ? m.Pos + m.Len : 1) {
  136. ascii := 0
  137. Loop 7
  138. ascii |= SubStr(m[0], A_Index, 1) << 7 - A_Index
  139. asciiStr .= Chr(ascii)
  140. }
  141. Return asciiStr
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement