> I have encoded an Integer of value 4294967290 > > in two ways(BER Encoding).Please tell me which method > is correct and why not the other one. > > First Method : 02 04 FF FF FF FA > > Second Method : 02 05 00 FF FF FF FA > > In the second method, third byte (00) represents sign > of the integer.(that is 00 is positive and 01 is > negative).
The second is the correct encoding but not for the reason you give. 00 is not a sign; only the first bit is the sign. The value, as has been said often recently, is in 2's complement notation. It appears, from the insistent INTEGER questions of late, that it is not obvious to everyone what 2's complement means, so permit me to try to explain. My apologies in advance to those who cannot bear dealing with trivial matters on the ASN.1 list. In 2's complement, the negative of a number is arrived at by complementing the bits (changing all 0s to 1s and all 1s to 0s) and adding 1. Thus, since the hexadecimal octet 01 represents +1, complementing it gives FE and adding 1 leaves us with FF, which represents -1. We can also do the same for larger fields: 0001 = +1, FFFF = -1. Of course, there is no negative 0. It is perhaps clearer to you now that the first bit (0 in the case of 00, and 1 in the case of FF) represents the sign, and the rest of the value represents the integer. Let's take a simple case and represent the entire range of possible integers. If we limit our field to 4 bits, it should be clear that we have sixteen possibilities in all, ranging from 0000 to 1111. Here's the complete list and the integers that would be represented. 000 0 001 +1 010 +2 011 +3 100 -4 101 -3 110 -2 111 -1 It's perhaps easier to view it (since we're doing 2's complement) as 100 -4 101 -3 110 -2 111 -1 000 0 001 1 010 2 011 3 with the list starting at the smallest and continuing through 0 and on to the largest value. Notice that there's always one more negative number than positive ones. Also notice that while at the high end of a positive number, 0 bits are just filler (that is, 0001 has the same value as 00000001), the filler for negative numbers is a 1 (that is, 1111 has the same value as 11111111). Here's the quiz: 1) What's the largest positive value you can fit into 8 bits? 2) What would you do if you had to represent a larger value? The answers: 1) 127 2) Use more bits. Let's take the value 4294967290. It's hex value is FFFFFFFA, but the first bit is a 1, so that can't be the two's complement form because it would mean it's negative. So we use more bits (actually another octet when we're working with BER) and the value becomes 00FFFFFFFA. The length is 5 octets, so the BER representation is 020500FFFFFFFA (assuming a universal tag of 2). Homework: what would 0204FFFFFFFA decode to? ===================================================================== Conrad Sigona Toll Free : 1-888-OSS-ASN1 OSS Nokalva Voice Mail : 1-732-302-9669 x400 [EMAIL PROTECTED] Fax : 1-419-831-5035 http://www.oss.com My direct line : 1-315-845-1773
