1

Currently I am working on a simple binary viewer(hex viewer), which opens binary files and output the code as hex values. I also want to make an offset which shows the position of the first hex value in each line. The offset should be of course also in hex format. Now I have the problem that I don't know how to add zeros to the offset and how many I need to add. It should like that:

enter image description here

You can see that the offset has some zeros at the beginning. I want to calculate how many zeros I need to add and how to do that.

kame
  • 20,848
  • 33
  • 104
  • 159
Thomas5897
  • 61
  • 2
  • 9
  • Possible duplicate of [How can I format a number into a string with leading zeros?](https://stackoverflow.com/questions/5418324/how-can-i-format-a-number-into-a-string-with-leading-zeros) – FCin Mar 19 '18 at 11:27

1 Answers1

1

By adding '0's you mean padding right?

You can achieve that by using string.Pad

uint offset = 0x0000000F;
string offsetPadded = offset.ToString("X").PadLeft(8,'0'); // The 'X' formats to hex

The resulting string should be "0000000F"

The ammount of zeroes is usually either 8 or 16 (32-64 bits). If you want to support files larger than 4GB, you should use 16 zeros (64bits)

Liam
  • 27,717
  • 28
  • 128
  • 190
babu646
  • 989
  • 10
  • 22
  • Oh thanks. But how can I calculate how many zeros I need to add? I have the amount of bytes but idk how to calculate the amount of zeros. – Thomas5897 Mar 19 '18 at 11:32
  • The ammount of zeroes is usually either 8 or 16 (32-64 bits). If you want to support files larger than 4GB, you should use 16 zeros (64bits) – babu646 Mar 19 '18 at 11:34