I'm having trouble with my some output in hexadecimal math. The subtraction overload seems to work fine, but when I add an address to another it goes all wierd on me. :(

Here is a sample output:

00000000 + 0000055c = 0000000c // Incorrect
000a0000 + 00000180 = 000a000000000080 // Incorrect

I'm unsure as to where the problem is located, but I'm guessing it's in the overload I did on the operator for the custom class type I made. I forgot how I solved this problem originally so unfortunately I have to ask for help. The objective of this source is to copy one function in hex to another area of memory. I had a working version of it at one point, but now I can't remember how to get the algorithms to work. :(

All help is appreciated!

``````    // HexAddress.cs
public class HexAddress {
public string Address { get; private set; }

public HexAddress(string Address) {
if (!Illegals.CheckForIllegals(Address))
this.Address = Illegals.ReplaceWriteMode(Address);

else
this.Address = "00000000";
}

public string BytesFromTop(HexAddress FunctionTop) { return (this - FunctionTop).ToString(); }

public override string ToString() {
return Address;
}

public static HexAddress operator -(HexAddress Operand1, HexAddress Operand2) {
HexAddress temp = new HexAddress((Math.ToDecimal(Operand1.Address) - Math.ToDecimal(Operand2.Address)).ToString("x"));

while (temp.Address.Length < 8)
temp.Address = "0" + temp.Address;

return temp;
}

public static HexAddress operator +(HexAddress Operand1, HexAddress Operand2) {
HexAddress temp = new HexAddress((Math.ToDecimal(Operand1.Address) + Math.ToDecimal(Operand2.Address)).ToString("x"));

while (temp.Address.Length < 8)
temp.Address = "0" + temp.Address;

return temp;
}

// Function.cs
public class Function {
public string Name { get; private set; }

public HexAddress TopAddress { get; private set; }
public HexAddress JrRaPlus8 { get; private set; }

public string Bytes { get { return Math.ToHex(Math.ToDecimal(JrRaPlus8.Address) - Math.ToDecimal(TopAddress.Address)); } }

public List<HexAddress> Codes { get; private set; }

public Function(string Name, HexAddress FunctionTop, HexAddress JrRaPlus8) {
if (this.Name == "" || this.Name == " ")
this.Name = "Untitled";

this.TopAddress = FunctionTop;
this.JrRaPlus8 = JrRaPlus8;

this.Codes = new List<HexAddress>();
}

public override string ToString() {
return "Function Top: " + TopAddress.ToString() + "\n" + "Jr Ra + 8: " + JrRaPlus8.ToString() + "\n" + "Bytes: " + Bytes;
}

public void AddCode(HexAddress CodeAddress) {
this.Codes.Add(CodeAddress);
}

// CopyManager.cs
public class CopyManager {
public List<string> Stack { get; private set; }
public List<string> Codes { get; private set; }

private List<Function> Functions;

public CopyManager(List<Function> Functions) {
this.Functions = Functions;

this.Stack = new List<string>();
this.Codes = new List<string>();
}

public void Copy() {
for (int x = 0; x < Functions.Count; x++) {
string NewStack = "";

string add = "000a" + x.ToString("x");

while (add.Length < 8)
add += "0";

HexAddress NewAddress = new HexAddress(add);
HexAddress temp = new HexAddress("00000000") + new HexAddress(Functions[x].Bytes);

NewStack = "5" + Functions[x].TopAddress.Address.Remove(0, 1) + " " + temp.ToString();
NewStack += "\n" + "0" + NewAddress.Address.Remove(0, 1) + " 00000000";

this.Stack.Add(NewStack);

for (int y = 0; y < Functions[x].Codes.Count; y++) {
string NewCode = "";

NewCode = (NewAddress + Functions[x].Codes[y].BytesFromTop(Functions[x].TopAddress)) + " 00000000";

this.Codes.Add(NewCode);
}
}
}
}
``````

Thanks,
Jamie

Also please note that Math.ToDecimal is a custom method located in a custom class:

``````    // Math.cs
public class Math {
public static string ToHex(int NumberToConvert) { return NumberToConvert.ToString("x"); }
public static int ToDecimal(string Address) { return Convert.ToInt32(Address, 16); }
}
``````

Once again thanks,
Jamie

I found the problem, and it actually was not with the operators. It was in the constructor for the HexAddress class. The problem was that it was actually changing the values using the method ReplaceWriteMode that replaces the first character in the string with a zero. I have it working perfectly now. Thanks anyways!

Jamie

I'm curious as to how CopyManager.cs can set the HexAddress.Address property if the 'set' has been marked private.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.