0

if I have:

 char[] hexText = new char[9]; //5A3F0000A
 char c =   hexText[9]; //A              
 int lastD = // will be 10;

I tried to convert it but it failed.. how to do it?

Thank you!

Kate
  • 25
  • 5

1 Answers1

0

You can handle this base conversion using an ordered string of your hex characters and the IndexOf string method.

string hexabet = "0123456789ABCDEF";
char c = 'A';
int lastD = hexabet.IndexOf(c);
Console.WriteLine(lastD); // 10
Jonathon Chase
  • 8,915
  • 16
  • 36
  • Thank you! I don't know what is 'c' because I am reading the data from a file. I need 'c' to get the last digit from hexabet. in your example F. – Kate May 31 '18 at 23:24
  • @Kate I don't think you understood how this works. – Rivasa May 31 '18 at 23:26
  • @Kate I set it to a constant 'A' for the purposes of this example. `hexabet` is your entire hexadecimal alphabet. I'm taking advantage of the fact that the index of each character is the same as it's decimal value. If the last value of whatever string you read was '8', then `hexabit.IndexOf('8')` will be 8. if it's D, then `hexabit.IndexOf('D')` will be 13. – Jonathon Chase May 31 '18 at 23:32
  • my case is different. I need to get the hex number from a file and use the last digit as a length - that's why I want to convert char[] to int – Kate May 31 '18 at 23:32
  • now I understand. I appreciate your help! – Kate May 31 '18 at 23:34
  • @Jonathon-chase can I use the place of the last digit instead of the value? – Kate May 31 '18 at 23:37
  • @Kate yes, just like you are in your example. You could set `char c = hexText[9];` or whatever the last element of your character array is, then `hexabet.IndexOf(c);` will give the decimal value of that individual hex character. – Jonathon Chase May 31 '18 at 23:38
  • @Jonathon-chase Great. Thank you – Kate May 31 '18 at 23:39