c# - Converting between negative Hexadecimal and negative Decimal gives wrong results -
i attempting manually convert numbers between decimal , hexadecimal. have working positive numbers , converting negative decimal 'negative' hexadecimal can't convert 'negative' hexadecimal negative decimal.
here code attempting work with:
private string hextodecimal(char[] toconvert) { if (negativevalue) { negativevalue = false; long var = convert.toint64(hextodecimal(resultlabel.text.tochararray())); long valuetohex = var - (long)math.pow(16, 15); return resultlabel.text = valuetohex.tostring(); } else { double total = 0; //convert hex decimal hexordecimallabel.text = "decimal"; //todo: create int array indivial int char[] chararray = toconvert; long[] numberarray = hexswitchfunction(chararray); //todo: reverse array array.reverse(numberarray); //loop array, times value 16^i++, adding total. method used convert hex decimal double power = 0; foreach (int in numberarray) { total += (i * (math.pow(16, power))); power++; } //set result label total ishex = false; allowhexbuttons(); return resultlabel.text = total.tostring(); } }
for instance, can turn - 10 fffffffffffffff6, when attempt turn decimal, 1.15292150460685e+18, can't equations with.
does know of way around this?
this because double
uses different representation negative numbers. changing type of total
, power
double
long
fix problem.
Comments
Post a Comment