1

I'm working with RFID Reader, and it became with a software demo that has some different types of reading a rfid tag, like:

Hexadecimal,Decimal,Ascii, Abatrack etc...

The Abatrack documentation says:

Shows the CardID converted to decimal with 14 digits.

I have a CardID = 01048CABFB then with this protocol it shows me 00004371295227
where the first four zeroes were added by the software

It converts a string with letters and numbers to decimal with only numbers. how may I do that ?

I've found THIS , but it's in VB.

Community
  • 1
  • 1
Ghaleon
  • 1,186
  • 5
  • 28
  • 55

2 Answers2

3

To convert from hexadecimal to decimal, you can do this:

string hexString = "01048CABFB";
long intVal = Int64.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
// intVal = 4371295227
Mike Chamberlain
  • 39,692
  • 27
  • 110
  • 158
  • The edited answer is good up to `9223372036854775807` or, in hex `7FFFFFFFFFFFFFFF`. Comfortably exceeding the 14 digits specified in the question, hence my +1. – Jodrell Jun 13 '13 at 14:49
  • @MikeChamberlain The default value of the ID `01048CABFB` isn't `Ascii` ? Because now I have to conver this value to `Hexadecimal` and there is only numbers... – Ghaleon Jun 13 '13 at 18:41
  • Not really sure what you mean. A hex number may not contain any letters at all but still be hexadecimal. For instance, 99 in hex is 153 in decimal. If the default value "isn't Ascii" then what is it? – Mike Chamberlain Jun 14 '13 at 04:23
2

You can also use Convert.ToInt64() which allows you to specify base 16 (hexadecimal):

        string hexFromRFID = "01048CABFB";
        Int64 decFromRFID = Convert.ToInt64(hexFromRFID, 16);
        Console.WriteLine("Hex: " + hexFromRFID + " = Dec: " + decFromRFID);
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Worked. But could you explain to me what's the difference between your method and Mike's ? – Ghaleon Jun 13 '13 at 14:46
  • 2
    http://stackoverflow.com/questions/199470/whats-the-main-difference-between-int-parse-and-convert-toint32 - short answer is that Convert.ToInt32 calls Int32.Parse internally, with an extra check to return 0 if the argument is null. – Mike Chamberlain Jun 13 '13 at 14:49