"how to convert hex string to decimal" Code Answer


decimal is floating point type, like single, double and so you can't parse by standard means strings like

  4b414.d000000011613c3eaf // <- '.' decimal point; 'e' exponent sign  

on the other hand, decimal is close to int128 and we don;t have that super long int. if your value is not that big (less than 2^63 which about 9.2e18) you can try something

// it's ok in your case:
// 4b414d000000011613c3 (hex) = 5422700088726126870 (dec) < 9223372036854775808 
// use long.parse() or ulong.parse(): int is too small
decimal result = long.parse(columns[1], system.globalization.numberstyles.hexnumber);

in case of exceeding the uint64 you can split your value:

// to simplify the idea, i remove negative values support
string source = "4b414d000000011613c3";

string highpart = source.remove(source.length - 16);
string lowpart = source.substring(source.length - 16);

decimal result =
  ulong.parse(highpart, system.globalization.numberstyles.hexnumber);

result = result * ulong.maxvalue + ulong.parse(lowpart,  system.globalization.numberstyles.hexnumber);
By Jack Kelly on May 15 2022

Answers related to “how to convert hex string to decimal”

Only authorized users can answer the Search term. Please sign in first, or register a free account.