Author Topic: ATH HTA  (Read 166 times)

0 Members and 1 Guest are viewing this topic.

John

  • Hero Member
  • *****
  • Posts: 3249
ATH HTA
« on: May 02, 2018, 10:28:50 PM »
Code: OxygenBasic
  1. '2018-05-02 T 15:07:27
  2. %filename "t.exe"
  3. uses rtl64
  4. uses corewin
  5.  
  6. function StringToHex(string s) as string
  7. ========================================
  8. int le=len s
  9. int i,j
  10. string r=nuls le*2
  11. indexbase 1
  12. for i=1 to le
  13.   j++
  14.   mid r,j,hex(asc(s,i),2)
  15.   j++
  16. next
  17. return r
  18. end function
  19.  
  20.  
  21. function HexToString(string s) as string
  22. ========================================
  23. int le=len s
  24. int i,j
  25. string r=nuls le\2
  26. byte rb at strptr r
  27. indexbase 1
  28. for i=1 to le step 2
  29.   j++
  30.   rb[j]=val "0x"+mid(s,i,2)
  31. next
  32. return r
  33. end function
  34.  
  35. 'TEST
  36. '====
  37. string h=StringToHex "ABCDEF"
  38. string s=HexToString h
  39. print h
  40. print s
  41.  

A couple of similar functions I translated to Script BASIC from Business BASIC.

Code: Script BASIC
  1. FUNCTION BB_ATH(HexStr)
  2.   LOCAL LenHex, AsciiStr, HexTable, ScanPos, HiByte, LowByte
  3.   LenHex = LEN(HexStr)
  4.   IF LenHex % 2 = 0 THEN
  5.     HexTable = "0123456789ABCDEF"
  6.     FOR ScanPos = 1 TO LenHex STEP 2
  7.       HiByte = INSTR(HexTable,UCASE(MID(HexStr, ScanPos, 1))) - 1
  8.       LowByte = INSTR(HexTable,UCASE(MID(HexStr, ScanPos + 1, 1))) - 1
  9.       IF ISINTEGER(HiByte) AND ISINTEGER(LowByte) THEN
  10.         AsciiStr &= CHR(HiByte * 16 + LowByte)
  11.       ELSE
  12.         AsciiStr = ""
  13.         GOTO Exit_For
  14.       END IF
  15.     NEXT ScanPos
  16.     Exit_For:
  17.   ELSE
  18.     AsciiStr = ""
  19.   END IF
  20.   BB_ATH = AsciiStr
  21. END FUNCTION
  22.  
  23. FUNCTION BB_HTA(AsciiStr)
  24.   LOCAL AsciiLen,ScanPos,HexStr
  25.   AsciiLen = LEN(AsciiStr)
  26.   IF AsciiLen THEN
  27.     FOR ScanPos = 1 TO AsciiLen
  28.       HexStr &= RIGHT("0" & HEX(ASC(MID(AsciiStr, ScanPos, 1))),2)
  29.     NEXT ScanPos
  30.   ELSE
  31.     HexStr = ""
  32.   END IF
  33.   BB_HTA = HexStr
  34. END FUNCTION
  35.  
  36. hstr = BB_HTA("0123456789ABCDEF")
  37. PRINT hstr,"\n"
  38. PRINT BB_ATH(hstr),"\n"
  39.  


jrs@jrs-laptop:~/sb/examples/test$ scriba a2hh2a.sb
30313233343536373839414243444546
0123456789ABCDEF
jrs@jrs-laptop:~/sb/examples/test$