[Solved] Please help me with pascal to c# code conversion [closed]


Existing Delphi code translation:

public static string ByteToHex(Byte InByte) {
  char[] Digits = new char[] {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
  };  

  return string.Concat(Digits[InByte >> 4], Digits[InByte & 0x0F]);
}

Better implementation (all you want is formatting):

public static string ByteToHex(Byte InByte) => InByte.ToString("X2");

Edit: For Delphi’s Hi and Lo (see comments) we have C#

// Second lowerest byte
public static byte Hi(int value) => (byte) ((value >> 8) & 0xFF);
// Lowerest byte
public static byte Lo(int value) => (byte) (value & 0xFF);

In general, the procedure is

  1. Shift to the right (if required): e.g. value >> 8 turns second last byte into last one
  2. Mask with 0xFF: we want one byte only: & 0xFF
  3. Cast int to byte: (byte)

3

solved Please help me with pascal to c# code conversion [closed]