unit uComUtil;

interface

  function St_H( i: word ): string;
  function St_HL( I: LongInt ): STRING;
  function St_HB( I: byte ): string;
  function ACode( S: string ): string;
  function HCode( S: string ): string;
  function BCode( S: array of byte; truesize: integer ): string;
  function HexCharToByte( H: char ): byte;
  function HexCharPairToByte( s: string ): byte;
  function IsHex( C: char ): boolean;
  procedure lSwapEndian( var i: integer );
  FUNCTION NEMA2Ext( S: String ): extended;
implementation

  uses SysUtils;

  FUNCTION NEMA2Ext( S: String ): extended;
  var
    dp: integer;
    sDeg, sMin: string;
  begin
    dp := Pos('.',S);
    sDeg := Copy( S, 1, dp-3 );
    sMin := Copy( S, dp-2, 255 );
    if (Length(sDeg)>0) AND (Length(sMin)>0) then
      result := StrToFloat( sDeg ) + StrToFloat( sMin ) / 60.0
    else
      result := 0.0;
  end;

  function St_H( i: word ): string;
  // Convert two bytes to a hex string representation:
  //    $1234 => '1234'
   const Hex: array [0..15] of Char='0123456789ABCDEF';
  begin
    St_H:= Hex[ (hi(I) shr 4) and $F ] + Hex[ hi(I) and $F ] +
           Hex[ (lo(I) shr 4) and $F ] + Hex[ lo(I) and $F ];
  end;

  function St_HL( I: LongInt ): STRING;
  // Convert four bytes to a hex string representation:
  //    $12345678 => '12345678'
  BEGIN
    St_HL := ST_H( (I SHR 16) AND $ffff ) + ST_H( I AND $FFFF );
  END;

  function St_HB( I: byte ): string;
  // Convert one byte to a hex string representation:
  //    $12 => '12'
   const Hex: array [0..15] of Char='0123456789ABCDEF';
  begin
    St_HB := Hex[ (lo(I) shr 4) and $F ] + Hex[ lo(I) and $F ];
  end;

  function ACode( S: string ): string;
  // return a ascii encoded string of S. Converts #13 to ^M and #255 to [FF]
  var
    i: integer;
  begin
    result := '';
    for i:= 1 to length(S) do
     case S[i] of
       ' '..'~': result := result + S[i];
       #0..#26:  result := result + '^' + char( ord(S[i]) + ord('A') - 1 );
       else      result := result + '[' + St_HB( ord(S[i]) ) + ']';
     end;
  end;

  function HCode( S: string ): string;
  // return a ascii encoded string of S. Converts #13 to ^M and #255 to [FF]
  var
    i: integer;
  begin
    result := '';
    for i:= 1 to length(S) do
       result := result + '[' + St_HB( ord(S[i]) ) + ']';
  end;

  function BCode( S: array of byte; truesize: integer ): string;
  // return a ascii encoded string of S. Converts #13 to ^M and #255 to [FF]
  var
    i: integer;
  begin
    result := ^M^J;
    for i:= 0 to truesize-1 do
       result := result + ' ' + St_HB( S[i] );
    result := result + ^M^J;
    for i:= 0 to truesize-1 do
    begin
     case Char(S[i]) of
       ' '..'~': result := result + '  ' + Char( S[i] );
       #0..#26:  result := result + ' ^' + char( ord(S[i]) + ord('A') - 1 );
       else      result := result + ' ' + St_HB( ord(S[i]) );
     end;
    end;
  end;

  function HexCharToByte( H: char ): byte;
  // Convert a single (presumed hex character) to the byte equivalent
  //     'A' => 10
  begin
    case H of
      '0':      result := 0;
      '1':      result := 1;
      '2':      result := 2;
      '3':      result := 3;
      '4':      result := 4;
      '5':      result := 5;
      '6':      result := 6;
      '7':      result := 7;
      '8':      result := 8;
      '9':      result := 9;
      'a', 'A': result := 10;
      'b', 'B': result := 11;
      'c', 'C': result := 12;
      'd', 'D': result := 13;
      'e', 'E': result := 14;
      'f', 'F': result := 15;
      else result := 0;
    end;
  end;

  function HexCharPairToByte( s: string ): byte;
  // convert the first two bytes of s to the byte equivalent using HEX
  // notation:
  //          '55' => 85. or $55
  begin
    result := 0;
    if length( s ) <> 2 then exit;
    result := HexCharToByte( s[1] ) * 16 + HexCharToByte( s[2] );
  end;

  function IsHex( C: char ): boolean;
  // return true if the passed characer is a valid HEX character
  begin
    result := C IN ['0'..'9','A'..'F','a'..'f'];
  end;

  procedure lSwapEndian( var i: integer );
  type
    tI = record
      case T: integer of
        0: ( i: integer; );
        1: ( b: array[0..3] of byte; );
        2: ( w: array[0..1] of word; );
    end;
  var
    b: byte;
    T: tI;
  begin
    T.i := i;
    b := T.b[0];
    T.b[0] := T.b[3];
    T.b[3] := b;
    b := T.b[1];
    T.b[1] := T.b[2];
    T.b[2] := b;
    i := T.i;
  end;


end.
 
