1
I need help converting an Hex string into an Hex string by applying two add-on.
Example: 0x00FEB5AB 0x00014A55
My Hex. 00FF17DB ? (represents a negative latitude)
and 00FCFF75 ? (represents a negative longitude)
Thank you all.
1
I need help converting an Hex string into an Hex string by applying two add-on.
Example: 0x00FEB5AB 0x00014A55
My Hex. 00FF17DB ? (represents a negative latitude)
and 00FCFF75 ? (represents a negative longitude)
Thank you all.
4
A simple way may be to calculate the complement to 1 and add 1 as described in the entry Complement to two wikipedia.
I say it’s a simple way because it’s easy to calculate the complement of 1, just for this to apply the operation bitwise NOT at value:
const string valor = "00FEB5AB";
//Converte para inteiro
uint valorInteiro = Convert.ToUInt32(valor, 16);
//Aplica o bitwise NOT e soma 1
uint complemento = ~valorInteiro + 1;
//Converte para string no formato hexadecimal
string complementoHex = string.Format("{0:X}", complemento);
Note that the calculated value is "FF014A55" and not "00014A55" as you say, because the result is calculated considering the amplitude (number of bytes) of the input.
By what I was given to realize the calculators existing on the net only consider the bytes significant, making the calculation truncating the non-significant bytes (00’s left).
Using the above code, the following method allows the calculation of the two forms:
public string ComputeTwosComplement(string hexValue, bool areAllBytesSignificant)
{
const int maxNumberOfBytes = 8;
int lenght = hexValue.Length;
//Não permite mais de 64bits
if (lenght > maxNumberOfBytes*2) throw new ArgumentOutOfRangeException(hexValue);
//Converte hexadecimal para long
ulong intValue = Convert.ToUInt64(hexValue, 16);
//Calcula o complemento para 2
ulong complement = (~intValue + 1);
//Converte para hexadecimal
string twosComplement = string.Format("{0:X16}", complement);
if (areAllBytesSignificant)
{
//Ajusta para o mesmo número de bytes do valor passado
twosComplement = twosComplement.Substring(maxNumberOfBytes*2 - lenght);
}
else
{
//Representação hexadecimal do valor passado sem 00's à esquerda
string significantBytes = string.Format("{0:X}", intValue);
//Ajusta para o número de bytes significativos do valor passado
twosComplement = twosComplement.Substring(maxNumberOfBytes*2 - significantBytes.Length);
}
return twosComplement;
}
To maintain amplitude and get "FF014A55" use:
ComputeTwosComplement("00FEB5AB", true);
To adjust the amplitude and get "014A55" use:
ComputeTwosComplement("00FEB5AB", false);
Note that if the value passed does not have 00’s on the left ("FEB5AB") it is indifferent to pass true
or false
, the result is always "014A55".
Browser other questions tagged c#
You are not signed in. Login or sign up in order to post.
I used this code.
string hexPosition = "00FF17DB";
uint intHexPosition = Convert.ToUInt32(hexPosition, 16);
uint twosComp = ~intHexPosition + 1;
hexPosition = string.Format("{0:X}", twosComp);
But me returns FF00E825 the correct would be E825 pq?– Rafael L Ramon
I had already pointed this out in the answer. I think it may have something to do with the fact that 32 bits are being used in the calculation. I’ll edit the answer.
– ramaral
I used an online converter and hit just right but I passed the values in Hex then converted into bin soon after applied the 2 add-on and then converted back to Hex.
00FF17DB -> 111111110001011111011011 -> 2C -> 000000001110100000100101 - > E825.
– Rafael L Ramon
Please confirm that for 0x00FEB5AB is 0x00014A55 or is 0x00004A55
– ramaral
Correct for Hex value "00FF17DB". However the Hex value "00FCFF75" returns incorrect (0000008B). The expected value for "00FCFF75" would be FF03008B.
– Rafael L Ramon
00FEB5AB = 00004A55
– Rafael L Ramon
If for the value "00FCFF75" is "FF03008B" then the initial version is right. I don’t understand why sometimes it works.
– ramaral
I will revert to the initial version.
– ramaral
Sorry the value for "00FCFF75" = 3008B but returns 0000008B. The value "00FF17DB" is correct E825. Also did not understand why work for "00FF17DB" and not work for "00FCFF75". =/
– Rafael L Ramon
0x0F7EF73D 0x008108C3 (Correct) Applying the second example returns 000008C3 (?) Manual of the protocol page 73 says 32bits. https://drive.google.com/file/d/0B82vmwUnG1x8bXZrbUw3ZTZnTjA/view?usp=sharing
– Rafael L Ramon
@ramaral problem is only depending on the number of bytes used. If you used 3 bytes in the 1st value, it would work. I think the question is problematic in this sense, it is necessary to know the right length of the value before applying the complement AA -> 56, 00AA -> FF56, etc. - To complement, specify the size is fundamental.
– Bacco
@Bacchus If I understood your comment the result will have to be truncated to have the same length of input not considering the pairs of zeros on the left (number of significant bytes): input = 00AA => -AA, length = 2, result 56. This clear to go against what the AP asks.
– ramaral
I thought about truncating but I was afraid. I will do some tests with other data. This link returns perfectly. (http://www.hobbyprojects.com/calculators/binary_decimal_hex_converter.html)
– Rafael L Ramon
Perfect! I am very grateful.
– Rafael L Ramon