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
- Shift to the right (if required): e.g.
value >> 8
turns second last byte into last one - Mask with
0xFF
: we want onebyte
only:& 0xFF
- Cast
int
tobyte
:(byte)
3
solved Please help me with pascal to c# code conversion [closed]