Reputation:
I'm writing a code which has to save up as much space as possible.
for (int i = 0; i < Code.Length; i++){
CodeInHex[i] = String.Format("{0:X}", Code[i]); // e.g. Dec value = -95, hex = "FFFFFFA1"
}
Is there a way to make heximal value equal to -A1 instead of "FFFFFFA1"? (saw it is allowed in c# here).
Upvotes: 0
Views: 1897
Reputation: 1064114
Well, you could do it manually:
var val = Code[i];
CodeInHex[i] = val < 0 ? ("-" + (-val).ToString("X")) : val.ToString("X");
But! Negative hex is not a common way of representing negative numbers. It is only "allowed in C#" because it is the unary negation operator applied to a positive constant; i.e. when the question shows -0x1
, it is "negate (some expression)", where "some expression" is 0x1
.
Upvotes: 2