//***************************************************************************************** // Copyright (c) 2000 Michael Kurz // All rights reserved. // If you want to use this source in your applications conatct: // // $FileName : mStrConv.Pkg // $ProjectName : Sahred Methods and Functions // $Author : Michael Kurz // $Created : 01-25-2001 @ 19:00 // // Contents: // Functions for converting strings to a string with its ASCII values and back. // // // $Rev History // //***************************************************************************************** // Flips a string Left-Right Function FlipStrLR Global String sStr Returns String Integer iC iL String sRet Move (Length(sStr)) To iL For iC From 1 To iL Move (Append(Mid(sStr,1,iC),sRet)) To sRet Loop Function_Return sRet End_Function // Converts a String to Ascii Code Function ConvertBuffToAscii Global String sBuff Returns String String sCh sRet Integer iC iLength Move (Length(sBuff)) To iLength For iC From 1 To iLength Move (Mid(sBuff,1,iC)) To sCh Move (Ascii(sCh)) To sCh Move (Right(Append("000",sCh),3)) To sCh Append sRet sCh Loop Function_Return sRet End_Function // Converts a String from Ascii back to str. Function ConvertAsciiToBuff Global String sBuff Returns String String sRet Integer iCh iC iLength Move (Length(sBuff)) To iLength For iC From 1 To iLength Move (Mid(sBuff,3,iC)) To iCh Append sRet (Character(iCh)) Move (iC+2) To iC // 1+2=3 Loop Function_Return sRet End_Function Function ConvertIntegerArrayToAscii Integer[] Data Returns String String sCh sResult Integer iC iLength Integer iValue Integer iLSB iMSB UShort usLSB usMSB Move "" To sResult Move (SizeOfArray(Data)) To iLength If (iLength>0) Begin For iC From 0 To (iLength-1) Move 0 To usLSB Move 0 To usMSB Move 0 To iLSB Move 0 To iMSB Move Data[iC] To iValue Move (Low(iValue)) To iLSB Move (Hi(iValue)) To iMSB // Write out Low data part of integer Move (Mod(iLSB,256)) To usLSB Move ((iLSB-usLSB)/256) To usMSB Move (Right(Append("000",String(usLSB)),3)) To sCh Append sResult sCh Move (Right(Append("000",String(usMSB)),3)) To sCh Append sResult sCh // Write out High data part of integer Move (Mod(iMSB,256)) To usLSB Move ((iMSB-usLSB)/256) To usMSB Move (Right(Append("000",String(usLSB)),3)) To sCh Append sResult sCh Move (Right(Append("000",String(usMSB)),3)) To sCh Append sResult sCh Loop End Function_Return sResult End_Function // The Hammer string format is LSB/MSB in string with 3 characters per digit // Total is 4 characters per integer value. // There can be a bogus "000" at the end with which we do nothing. Function ConvertAsciiToIntegerArray Global String sBuff Returns Integer[] Integer iCh iC iLength Integer iValue Integer iByte Integer[] Results Move (ResizeArray(Results,0)) To Results Move (Length(sBuff)) To iLength Move 0 To iByte Move 0 To iValue For iC From 1 To iLength Move (Mid(sBuff,3,iC)) To iCh Move (iValue+(iCh*(256^iByte))) To iValue Increment iByte If (iByte=4) Begin Move iValue To Results[SizeOfArray(Results)] Move 0 To iByte Move 0 To iValue End Move (iC+2) To iC // 1+2=3 Loop Function_Return Results End_Function // Converts one Digit from Decimal to Hex. Function IntToHexDigit Global Integer iInt Returns String If iInt Lt 0 ; Function_Return 0 If iInt Lt 10 ; Function_Return iInt If iInt Eq 10 ; Function_Return "A" If iInt Eq 11 ; Function_Return "B" If iInt Eq 12 ; Function_Return "C" If iInt Eq 13 ; Function_Return "D" If iInt Eq 14 ; Function_Return "E" If iInt Eq 15 ; Function_Return "F" Function_Return "" End_Function // Converts one Digit from Decimal to Hex. Function HexToIntDigit Global String sHEX Returns Integer If sHex Eq "F" ; Function_Return 15 If sHex Eq "E" ; Function_Return 14 If sHex Eq "D" ; Function_Return 13 If sHex Eq "C" ; Function_Return 12 If sHex Eq "B" ; Function_Return 11 If sHex Eq "A" ; Function_Return 10 Function_Return sHex End_Function #IFNDEF GET_IntToHex // Converts a decimal value (0-255) Function IntToHex Global Integer iInt Returns String Integer iL iH String sRet Move (iInt/16) To iH Move (Mod(iInt,16)) To iL Append sRet (IntToHexDigit(iH)) (IntToHexDigit(iL)) Function_Return sRet End_Function #ENDIF // Converts a hex value to decimal (0-255) Function HexToInt Global String sHex Returns Integer Function_Return ((HexToIntDigit(Left(sHex,1))*16)+(HexToIntDigit(Right(sHex,1)))) End_Function // Converts a character to an Ascii value in Hex Function AsciiHex Global String sCH Returns String Function_Return (IntToHex(Ascii(sCH))) End_Function // Converts a ascii hex value to a character. Function CharacterHex Global String sHex Returns String Function_Return (Character(HexToInt(sHex))) End_Function // Converts a String to Ascii Code Function ConvertBuffToAsciiHex Global String sBuff Returns String String sCh sRet Integer iC iLength Move (Length(sBuff)) To iLength For iC From 1 To iLength Move (Mid(sBuff,1,iC)) To sCh Move (AsciiHex(sCh)) To sCh Append sRet sCh Loop Function_Return sRet End_Function // Converts a String from Ascii back to str. Function ConvertAsciiToBuffHex Global String sBuff Returns String String sCh sRet Integer iC iLength Move (Length(sBuff)) To iLength For iC From 1 To iLength Move (Mid(sBuff,2,iC)) To sCh Append sRet (CharacterHex(sCh)) Move (iC+1) To iC // 1+1=2 Loop Function_Return sRet End_Function // Converts some special characters in a string. Function fsConvertString Global String sStr Returns String String sCR Append sCR (Character(13)) (Character(10)) Move (Replaces("\n",sStr,sCR)) To sStr Move (Replaces("\t",sStr,Character(9))) To sStr Move (Replaces("\C34",sStr,Character(34))) To sStr Function_Return sStr End_Function