-3

I want to convert decimals into Hex:

string hex = IntToString(dezimal,
             new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                          'A', 'B', 'C', 'D', 'E', 'F'});
textBoxHexa.Text = hex;

But I get an error:

Input string was not in a correct format

Is this even the best way to do it? If not, what better ways are there?

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
GerN
  • 1
  • 2
  • What's `IntToString`? – Sergey Kalinichenko Feb 26 '18 at 17:19
  • What is the implementation for IntToString? – Blake Thingstad Feb 26 '18 at 17:19
  • Possible duplicate of [int to hex string](https://stackoverflow.com/questions/4690480/int-to-hex-string) – PaulF Feb 26 '18 at 17:22
  • There is no need to convert. Decimals are stored as numbers in binary. You just need to change display method is you need a hex output. – jdweng Feb 26 '18 at 17:24
  • you have to show the code thats failing. ie IntToString. Turn on 'break on exception thrown' in the Debug->Exception settings to see which line is failing – pm100 Feb 26 '18 at 17:27
  • As the error message seems to bear no relation to the code shown - are you taking a user input string & then trying to parse that to an integer first. You would get "Input string was not in a correct format" exception if the user typed an invalid integer string (maybe a decimal) & you tried parsing with the Int32.Parse() method. – PaulF Feb 26 '18 at 17:53

2 Answers2

-1

Decimals can't be represented directly by hexadecimal, there's no floating point, for that you need a representation of the floating point number, the most common is the IEEE-754 representation which uses the actual processors.

In case you want that representation you can do this (using unsafe):

decimal m = 3.4353M;
byte* data = (byte*)&m;

string hex = "";

for(int buc = 0; buc < 16; buc++)
    hex += data[buc].ToString("X2");

//Result: 00000400000000003186000000000000
Gusman
  • 14,905
  • 2
  • 34
  • 50
  • Look into `BitConverter.GetBytes()` [MSDN](https://msdn.microsoft.com/en-us/library/yhwsaf3w(v=vs.110).aspx). It provides a managed way to get a byte representation of a value as a byte array, without the pointer arithmetic. – Trevor Feb 26 '18 at 18:10
  • @Trevor Look at it by yourself, there's no conversion from decimal (decimal is not the same as float). – Gusman Feb 26 '18 at 18:25
  • True, `GetBytes()` does not have an overload for .NET's `decimal` data type. But NET's [`decimal`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/decimal) data type is not an [IEEE-754](https://en.wikipedia.org/wiki/IEEE_754) defined type like you suggest. The overload is likely missing because the internal representation of a framework-specific type would be of limited use. To be honest, it's all moot until the OP provides more information. – Trevor Feb 26 '18 at 19:07
-3

First of all IntToString converts an integer to string, not from decimal to hex.

Check here for IntToString use

TO convert from decimal to hex use the method: ToString("X")

// Store integer 50
int decValue = 50;

// Convert integer 182 as a hex in a string variable
string hexValue = decValue.ToString("X");

Or you could use the following method as well:

string.format("{0:x}", decValue);

Refer to this answer for more details

whatthefish
  • 347
  • 1
  • 17
  • You need to be more precise in your wording - you seem to imply that you can convert from "decimal" type to a hex string with _ToString("X")_. You will find this throws a FormatException. You can only use that formatting for integral types (short, int, long, ushort etc). – PaulF Feb 26 '18 at 17:40
  • 1
    How do you know what IntToString does? We do not know the type of `dezimal` , it could be anyting – pm100 Feb 26 '18 at 18:04
  • Stop posting random answers and test it by yourself. It fails miserably also. – Gusman Feb 26 '18 at 18:28
  • for all we know dezimal is a string – pm100 Feb 26 '18 at 19:05
  • @pm100 It cannot be string since the first argument for `IntToString` is an int argument. – whatthefish Feb 26 '18 at 23:37
  • @Ramesh how do you know what the first argument is? OP has not provided the definition for that function. You’re literally guessing. This question is unanswerable until the OP provides a [MCVE] – maccettura Feb 27 '18 at 00:02
  • @maccettura I am not guessing it but that is what is mentioned in the arguments of the function. Here is the definition of the `IntToString` function: `int IntToString(int num, char[] str, int maxlength)` – whatthefish Feb 27 '18 at 00:15
  • @Ramesh you’ve linked a random c++ library that includes an IntToString function. How on earth is that helpful or relevant? – maccettura Feb 27 '18 at 00:19