ASN.1 / DER Encoding of IntegersASN Basic Encoding Rule of an integerWhat is base 64 encoding used for?Encode URL in JavaScript?How should I interpret the ASN.1 BER standard for REALs?How to do Base64 encoding in node.js?Two's complement in ASN.1 Unaligned PERASN Basic Encoding Rule of an integerEncoding and decoding ASN.1 REAL with BERReading encode asn file manuallyASN.1 BER Handling Tag Number 31 or HigherInterpreting ASN.1 indefinite-lenght encoding with multiple encapsulated octet-strings
3D cursor orientation
Looking for physical intuition into the Electromagnetic Tensor:
1025th term of the given sequence.
is FIND WORDS in P?
I am a dual citizen of United States and Mexico, can I use my Mexican license in california when visiting?
What does the following chess proverb mean: "Chess is a sea where a gnat may drink from and an elephant may bathe in."
Pass USB 3.0 connection through D-SUB connector
Why does airflow separate from the wing during stall?
Finding Greatest Common Divisor using LuaLatex
How to create a new string without spaces from another string using a loop
Why is the UH-60 tail rotor canted?
sed '5innn' myfile inserts first n as a literal n, and the other two nn as new lines, is it possible to insert all 3 new lines?
Found old paper shares of Motorola Inc that has since been broken up
creating recursive, self-similar patterns with Adobe Illustrator
Why are Oscar, India, and X-Ray (O, I, and X) not used as taxiway identifiers?
Has Iron Man made any suit for underwater combat?
How old is the Italian word "malandrino"?
"Bootstrapping" an unbounded class of inaccessible cardinals
Project Euler # 25 The 1000 digit Fibonacci index
Is there any direct train from LHR Airport to Newcastle Gateshead?
Is there a smart way to pick up Page Design other than Template to Design mapping?
Killing a star safely
My current job follows "worst practices". How can I talk about my experience in an interview without giving off red flags?
Is it better to deliver many low-value stories or few high-value stories?
ASN.1 / DER Encoding of Integers
ASN Basic Encoding Rule of an integerWhat is base 64 encoding used for?Encode URL in JavaScript?How should I interpret the ASN.1 BER standard for REALs?How to do Base64 encoding in node.js?Two's complement in ASN.1 Unaligned PERASN Basic Encoding Rule of an integerEncoding and decoding ASN.1 REAL with BERReading encode asn file manuallyASN.1 BER Handling Tag Number 31 or HigherInterpreting ASN.1 indefinite-lenght encoding with multiple encapsulated octet-strings
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm currently starting to work with DER (Distinguished Encoding Rules) encoding and have problems understanding the encoding of integers.
In the reference document https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf this encoding is defined as follows:
8.3.1 The encoding of an integer value shall be primitive. The contents octets shall consist of one or more octets.
8.3.2 If the contents octets of an integer value encoding consist of more than one octet, then the bits of the first octet and bit 8 of the second octet:
shall not all be ones; and
shall not all be zero.
NOTE – These rules ensure that an integer value is always encoded in the smallest possible number of octets.
8.3.3 The contents octets shall be a two's complement binary number equal to the integer value, and consisting of bits 8 to 1 of the first octet, followed by bits 8 to 1 of the second octet, followed by bits 8 to 1 of each octet in turn up to and including the last octet of the contents octets.
On another site, https://docs.microsoft.com/en-us/windows/desktop/seccertenroll/about-integer, it is explained that for positive numbers whose binary representation starts with a 1, a zero byte is added at the front. This is also mentioned in the answers to a former question on stackoverflow: ASN Basic Encoding Rule of an integer.
Unfortunately, from these answers I cannot see how this latter instruction can be deduced from the rules of the reference document.
For example, if I want to encode the number 128, why can't I do this as
[tag byte] [length byte] 10000000?
I know that the correct encoding would be [tag byte] [length byte] 00000000 10000000, but which condition is injured by the variant above? Probably it has something do to with the two's complement, but isn't the two's complement of 128 again 10000000?
I hope you can help me understand why the description on the Microsoft site is equivalent to the original definition. Thank you.
encoding asn.1 der
add a comment |
I'm currently starting to work with DER (Distinguished Encoding Rules) encoding and have problems understanding the encoding of integers.
In the reference document https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf this encoding is defined as follows:
8.3.1 The encoding of an integer value shall be primitive. The contents octets shall consist of one or more octets.
8.3.2 If the contents octets of an integer value encoding consist of more than one octet, then the bits of the first octet and bit 8 of the second octet:
shall not all be ones; and
shall not all be zero.
NOTE – These rules ensure that an integer value is always encoded in the smallest possible number of octets.
8.3.3 The contents octets shall be a two's complement binary number equal to the integer value, and consisting of bits 8 to 1 of the first octet, followed by bits 8 to 1 of the second octet, followed by bits 8 to 1 of each octet in turn up to and including the last octet of the contents octets.
On another site, https://docs.microsoft.com/en-us/windows/desktop/seccertenroll/about-integer, it is explained that for positive numbers whose binary representation starts with a 1, a zero byte is added at the front. This is also mentioned in the answers to a former question on stackoverflow: ASN Basic Encoding Rule of an integer.
Unfortunately, from these answers I cannot see how this latter instruction can be deduced from the rules of the reference document.
For example, if I want to encode the number 128, why can't I do this as
[tag byte] [length byte] 10000000?
I know that the correct encoding would be [tag byte] [length byte] 00000000 10000000, but which condition is injured by the variant above? Probably it has something do to with the two's complement, but isn't the two's complement of 128 again 10000000?
I hope you can help me understand why the description on the Microsoft site is equivalent to the original definition. Thank you.
encoding asn.1 der
Maybe this will help
– pepo
Mar 26 at 14:49
Unfortunately it does not so far. I had already considered this document, but it also does not seem to mention that for negative integers there is an additional zero byte added. Instead, the document says "Encoding of negative integers has its own rules" and then describes the Two's complement in its own words. But in the reference document for ASN.1 the word "negative" only appears three times, and none of these refers to the encoding of integers. So I still wonder where these "own rules" come from.
– Bilbo
Mar 26 at 16:13
add a comment |
I'm currently starting to work with DER (Distinguished Encoding Rules) encoding and have problems understanding the encoding of integers.
In the reference document https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf this encoding is defined as follows:
8.3.1 The encoding of an integer value shall be primitive. The contents octets shall consist of one or more octets.
8.3.2 If the contents octets of an integer value encoding consist of more than one octet, then the bits of the first octet and bit 8 of the second octet:
shall not all be ones; and
shall not all be zero.
NOTE – These rules ensure that an integer value is always encoded in the smallest possible number of octets.
8.3.3 The contents octets shall be a two's complement binary number equal to the integer value, and consisting of bits 8 to 1 of the first octet, followed by bits 8 to 1 of the second octet, followed by bits 8 to 1 of each octet in turn up to and including the last octet of the contents octets.
On another site, https://docs.microsoft.com/en-us/windows/desktop/seccertenroll/about-integer, it is explained that for positive numbers whose binary representation starts with a 1, a zero byte is added at the front. This is also mentioned in the answers to a former question on stackoverflow: ASN Basic Encoding Rule of an integer.
Unfortunately, from these answers I cannot see how this latter instruction can be deduced from the rules of the reference document.
For example, if I want to encode the number 128, why can't I do this as
[tag byte] [length byte] 10000000?
I know that the correct encoding would be [tag byte] [length byte] 00000000 10000000, but which condition is injured by the variant above? Probably it has something do to with the two's complement, but isn't the two's complement of 128 again 10000000?
I hope you can help me understand why the description on the Microsoft site is equivalent to the original definition. Thank you.
encoding asn.1 der
I'm currently starting to work with DER (Distinguished Encoding Rules) encoding and have problems understanding the encoding of integers.
In the reference document https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf this encoding is defined as follows:
8.3.1 The encoding of an integer value shall be primitive. The contents octets shall consist of one or more octets.
8.3.2 If the contents octets of an integer value encoding consist of more than one octet, then the bits of the first octet and bit 8 of the second octet:
shall not all be ones; and
shall not all be zero.
NOTE – These rules ensure that an integer value is always encoded in the smallest possible number of octets.
8.3.3 The contents octets shall be a two's complement binary number equal to the integer value, and consisting of bits 8 to 1 of the first octet, followed by bits 8 to 1 of the second octet, followed by bits 8 to 1 of each octet in turn up to and including the last octet of the contents octets.
On another site, https://docs.microsoft.com/en-us/windows/desktop/seccertenroll/about-integer, it is explained that for positive numbers whose binary representation starts with a 1, a zero byte is added at the front. This is also mentioned in the answers to a former question on stackoverflow: ASN Basic Encoding Rule of an integer.
Unfortunately, from these answers I cannot see how this latter instruction can be deduced from the rules of the reference document.
For example, if I want to encode the number 128, why can't I do this as
[tag byte] [length byte] 10000000?
I know that the correct encoding would be [tag byte] [length byte] 00000000 10000000, but which condition is injured by the variant above? Probably it has something do to with the two's complement, but isn't the two's complement of 128 again 10000000?
I hope you can help me understand why the description on the Microsoft site is equivalent to the original definition. Thank you.
encoding asn.1 der
encoding asn.1 der
edited Mar 26 at 13:17
Bilbo
asked Mar 26 at 13:05
BilboBilbo
235 bronze badges
235 bronze badges
Maybe this will help
– pepo
Mar 26 at 14:49
Unfortunately it does not so far. I had already considered this document, but it also does not seem to mention that for negative integers there is an additional zero byte added. Instead, the document says "Encoding of negative integers has its own rules" and then describes the Two's complement in its own words. But in the reference document for ASN.1 the word "negative" only appears three times, and none of these refers to the encoding of integers. So I still wonder where these "own rules" come from.
– Bilbo
Mar 26 at 16:13
add a comment |
Maybe this will help
– pepo
Mar 26 at 14:49
Unfortunately it does not so far. I had already considered this document, but it also does not seem to mention that for negative integers there is an additional zero byte added. Instead, the document says "Encoding of negative integers has its own rules" and then describes the Two's complement in its own words. But in the reference document for ASN.1 the word "negative" only appears three times, and none of these refers to the encoding of integers. So I still wonder where these "own rules" come from.
– Bilbo
Mar 26 at 16:13
Maybe this will help
– pepo
Mar 26 at 14:49
Maybe this will help
– pepo
Mar 26 at 14:49
Unfortunately it does not so far. I had already considered this document, but it also does not seem to mention that for negative integers there is an additional zero byte added. Instead, the document says "Encoding of negative integers has its own rules" and then describes the Two's complement in its own words. But in the reference document for ASN.1 the word "negative" only appears three times, and none of these refers to the encoding of integers. So I still wonder where these "own rules" come from.
– Bilbo
Mar 26 at 16:13
Unfortunately it does not so far. I had already considered this document, but it also does not seem to mention that for negative integers there is an additional zero byte added. Instead, the document says "Encoding of negative integers has its own rules" and then describes the Two's complement in its own words. But in the reference document for ASN.1 the word "negative" only appears three times, and none of these refers to the encoding of integers. So I still wonder where these "own rules" come from.
– Bilbo
Mar 26 at 16:13
add a comment |
1 Answer
1
active
oldest
votes
The Two's complement rule (8.3.3) says that if the high bit of the first (lowest index) content byte is set that the number is negative.
02 01 80 has contents 0b1000_0000. Since the high bit is set, the number is negative.
Flip all the bits (0b0111_1111), then add one: 0b1000_0000; meaning that it represents negative 128.
For a less degenerate example, 0b1000_0001 => 0b0111_1110 => 0b0111_1111, showing that 0x81 is negative 127.
For the number (positive) 127, since the high bit isn't set, the number is interepreted as positive, so the contents are just 0b0111_1111 aka 0x7F, resulting in 02 01 7F
Sorry, but I don't see how this answers my question. Maybe I failed to make my point clear. I basically understand how the representation of numbers in Two's complement works. My question refers to the sentence from the Microsoft website "If the integer is positive but the high order bit is set to 1, a leading 0x00 is added to the content to indicate that the number is not negative." I still don't see where this comes from?
– Bilbo
Mar 26 at 16:03
The extra 0 byte means the high bit of the first byte is not set, so the value is the positive 15 but number 0b000_0000_1000_0000 (128). Without it the high bit is set, so the number is negative: the two’s complement of the 7 bit value 0b000_0000, which is (in 8-bit land)-128.
– bartonjs
Mar 26 at 16:21
Ok, I think now I see where I was wrong. I thought as 1000 0000 being the binary representation of 128; but this is NOT the representation as a Two's complement number. If we consider signed bytes, we NEED a 0 at the start, so we would like to put a zero bit at the start. And since we must use full bytes in our encoding, we then must put a complete zero byte at the start.
– Bilbo
Mar 26 at 18:19
@Bilbo Yep, that’s pretty much it.
– bartonjs
Mar 26 at 18:20
Just to add another piece that contributed to my confusion: The Microsoft article says "The Value field of the TLV triplet contains the encoded integer if it is positive, or its two's complement if it is negative." Here the OPERATION of taking the two's complement (by switching bits and adding 1) must be meant; apparently in contrast, the reference document says that always(!) "the contents octets shall be a two's complement binary number equal to the integer value". Here not the operation, but the REPRESENTATION in two's complement is meant, which switches the bits only for negative numbers.
– Bilbo
Mar 26 at 21:53
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55357924%2fasn-1-der-encoding-of-integers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The Two's complement rule (8.3.3) says that if the high bit of the first (lowest index) content byte is set that the number is negative.
02 01 80 has contents 0b1000_0000. Since the high bit is set, the number is negative.
Flip all the bits (0b0111_1111), then add one: 0b1000_0000; meaning that it represents negative 128.
For a less degenerate example, 0b1000_0001 => 0b0111_1110 => 0b0111_1111, showing that 0x81 is negative 127.
For the number (positive) 127, since the high bit isn't set, the number is interepreted as positive, so the contents are just 0b0111_1111 aka 0x7F, resulting in 02 01 7F
Sorry, but I don't see how this answers my question. Maybe I failed to make my point clear. I basically understand how the representation of numbers in Two's complement works. My question refers to the sentence from the Microsoft website "If the integer is positive but the high order bit is set to 1, a leading 0x00 is added to the content to indicate that the number is not negative." I still don't see where this comes from?
– Bilbo
Mar 26 at 16:03
The extra 0 byte means the high bit of the first byte is not set, so the value is the positive 15 but number 0b000_0000_1000_0000 (128). Without it the high bit is set, so the number is negative: the two’s complement of the 7 bit value 0b000_0000, which is (in 8-bit land)-128.
– bartonjs
Mar 26 at 16:21
Ok, I think now I see where I was wrong. I thought as 1000 0000 being the binary representation of 128; but this is NOT the representation as a Two's complement number. If we consider signed bytes, we NEED a 0 at the start, so we would like to put a zero bit at the start. And since we must use full bytes in our encoding, we then must put a complete zero byte at the start.
– Bilbo
Mar 26 at 18:19
@Bilbo Yep, that’s pretty much it.
– bartonjs
Mar 26 at 18:20
Just to add another piece that contributed to my confusion: The Microsoft article says "The Value field of the TLV triplet contains the encoded integer if it is positive, or its two's complement if it is negative." Here the OPERATION of taking the two's complement (by switching bits and adding 1) must be meant; apparently in contrast, the reference document says that always(!) "the contents octets shall be a two's complement binary number equal to the integer value". Here not the operation, but the REPRESENTATION in two's complement is meant, which switches the bits only for negative numbers.
– Bilbo
Mar 26 at 21:53
|
show 1 more comment
The Two's complement rule (8.3.3) says that if the high bit of the first (lowest index) content byte is set that the number is negative.
02 01 80 has contents 0b1000_0000. Since the high bit is set, the number is negative.
Flip all the bits (0b0111_1111), then add one: 0b1000_0000; meaning that it represents negative 128.
For a less degenerate example, 0b1000_0001 => 0b0111_1110 => 0b0111_1111, showing that 0x81 is negative 127.
For the number (positive) 127, since the high bit isn't set, the number is interepreted as positive, so the contents are just 0b0111_1111 aka 0x7F, resulting in 02 01 7F
Sorry, but I don't see how this answers my question. Maybe I failed to make my point clear. I basically understand how the representation of numbers in Two's complement works. My question refers to the sentence from the Microsoft website "If the integer is positive but the high order bit is set to 1, a leading 0x00 is added to the content to indicate that the number is not negative." I still don't see where this comes from?
– Bilbo
Mar 26 at 16:03
The extra 0 byte means the high bit of the first byte is not set, so the value is the positive 15 but number 0b000_0000_1000_0000 (128). Without it the high bit is set, so the number is negative: the two’s complement of the 7 bit value 0b000_0000, which is (in 8-bit land)-128.
– bartonjs
Mar 26 at 16:21
Ok, I think now I see where I was wrong. I thought as 1000 0000 being the binary representation of 128; but this is NOT the representation as a Two's complement number. If we consider signed bytes, we NEED a 0 at the start, so we would like to put a zero bit at the start. And since we must use full bytes in our encoding, we then must put a complete zero byte at the start.
– Bilbo
Mar 26 at 18:19
@Bilbo Yep, that’s pretty much it.
– bartonjs
Mar 26 at 18:20
Just to add another piece that contributed to my confusion: The Microsoft article says "The Value field of the TLV triplet contains the encoded integer if it is positive, or its two's complement if it is negative." Here the OPERATION of taking the two's complement (by switching bits and adding 1) must be meant; apparently in contrast, the reference document says that always(!) "the contents octets shall be a two's complement binary number equal to the integer value". Here not the operation, but the REPRESENTATION in two's complement is meant, which switches the bits only for negative numbers.
– Bilbo
Mar 26 at 21:53
|
show 1 more comment
The Two's complement rule (8.3.3) says that if the high bit of the first (lowest index) content byte is set that the number is negative.
02 01 80 has contents 0b1000_0000. Since the high bit is set, the number is negative.
Flip all the bits (0b0111_1111), then add one: 0b1000_0000; meaning that it represents negative 128.
For a less degenerate example, 0b1000_0001 => 0b0111_1110 => 0b0111_1111, showing that 0x81 is negative 127.
For the number (positive) 127, since the high bit isn't set, the number is interepreted as positive, so the contents are just 0b0111_1111 aka 0x7F, resulting in 02 01 7F
The Two's complement rule (8.3.3) says that if the high bit of the first (lowest index) content byte is set that the number is negative.
02 01 80 has contents 0b1000_0000. Since the high bit is set, the number is negative.
Flip all the bits (0b0111_1111), then add one: 0b1000_0000; meaning that it represents negative 128.
For a less degenerate example, 0b1000_0001 => 0b0111_1110 => 0b0111_1111, showing that 0x81 is negative 127.
For the number (positive) 127, since the high bit isn't set, the number is interepreted as positive, so the contents are just 0b0111_1111 aka 0x7F, resulting in 02 01 7F
answered Mar 26 at 15:22
bartonjsbartonjs
15.6k1 gold badge30 silver badges64 bronze badges
15.6k1 gold badge30 silver badges64 bronze badges
Sorry, but I don't see how this answers my question. Maybe I failed to make my point clear. I basically understand how the representation of numbers in Two's complement works. My question refers to the sentence from the Microsoft website "If the integer is positive but the high order bit is set to 1, a leading 0x00 is added to the content to indicate that the number is not negative." I still don't see where this comes from?
– Bilbo
Mar 26 at 16:03
The extra 0 byte means the high bit of the first byte is not set, so the value is the positive 15 but number 0b000_0000_1000_0000 (128). Without it the high bit is set, so the number is negative: the two’s complement of the 7 bit value 0b000_0000, which is (in 8-bit land)-128.
– bartonjs
Mar 26 at 16:21
Ok, I think now I see where I was wrong. I thought as 1000 0000 being the binary representation of 128; but this is NOT the representation as a Two's complement number. If we consider signed bytes, we NEED a 0 at the start, so we would like to put a zero bit at the start. And since we must use full bytes in our encoding, we then must put a complete zero byte at the start.
– Bilbo
Mar 26 at 18:19
@Bilbo Yep, that’s pretty much it.
– bartonjs
Mar 26 at 18:20
Just to add another piece that contributed to my confusion: The Microsoft article says "The Value field of the TLV triplet contains the encoded integer if it is positive, or its two's complement if it is negative." Here the OPERATION of taking the two's complement (by switching bits and adding 1) must be meant; apparently in contrast, the reference document says that always(!) "the contents octets shall be a two's complement binary number equal to the integer value". Here not the operation, but the REPRESENTATION in two's complement is meant, which switches the bits only for negative numbers.
– Bilbo
Mar 26 at 21:53
|
show 1 more comment
Sorry, but I don't see how this answers my question. Maybe I failed to make my point clear. I basically understand how the representation of numbers in Two's complement works. My question refers to the sentence from the Microsoft website "If the integer is positive but the high order bit is set to 1, a leading 0x00 is added to the content to indicate that the number is not negative." I still don't see where this comes from?
– Bilbo
Mar 26 at 16:03
The extra 0 byte means the high bit of the first byte is not set, so the value is the positive 15 but number 0b000_0000_1000_0000 (128). Without it the high bit is set, so the number is negative: the two’s complement of the 7 bit value 0b000_0000, which is (in 8-bit land)-128.
– bartonjs
Mar 26 at 16:21
Ok, I think now I see where I was wrong. I thought as 1000 0000 being the binary representation of 128; but this is NOT the representation as a Two's complement number. If we consider signed bytes, we NEED a 0 at the start, so we would like to put a zero bit at the start. And since we must use full bytes in our encoding, we then must put a complete zero byte at the start.
– Bilbo
Mar 26 at 18:19
@Bilbo Yep, that’s pretty much it.
– bartonjs
Mar 26 at 18:20
Just to add another piece that contributed to my confusion: The Microsoft article says "The Value field of the TLV triplet contains the encoded integer if it is positive, or its two's complement if it is negative." Here the OPERATION of taking the two's complement (by switching bits and adding 1) must be meant; apparently in contrast, the reference document says that always(!) "the contents octets shall be a two's complement binary number equal to the integer value". Here not the operation, but the REPRESENTATION in two's complement is meant, which switches the bits only for negative numbers.
– Bilbo
Mar 26 at 21:53
Sorry, but I don't see how this answers my question. Maybe I failed to make my point clear. I basically understand how the representation of numbers in Two's complement works. My question refers to the sentence from the Microsoft website "If the integer is positive but the high order bit is set to 1, a leading 0x00 is added to the content to indicate that the number is not negative." I still don't see where this comes from?
– Bilbo
Mar 26 at 16:03
Sorry, but I don't see how this answers my question. Maybe I failed to make my point clear. I basically understand how the representation of numbers in Two's complement works. My question refers to the sentence from the Microsoft website "If the integer is positive but the high order bit is set to 1, a leading 0x00 is added to the content to indicate that the number is not negative." I still don't see where this comes from?
– Bilbo
Mar 26 at 16:03
The extra 0 byte means the high bit of the first byte is not set, so the value is the positive 15 but number 0b000_0000_1000_0000 (128). Without it the high bit is set, so the number is negative: the two’s complement of the 7 bit value 0b000_0000, which is (in 8-bit land)
-128.– bartonjs
Mar 26 at 16:21
The extra 0 byte means the high bit of the first byte is not set, so the value is the positive 15 but number 0b000_0000_1000_0000 (128). Without it the high bit is set, so the number is negative: the two’s complement of the 7 bit value 0b000_0000, which is (in 8-bit land)
-128.– bartonjs
Mar 26 at 16:21
Ok, I think now I see where I was wrong. I thought as 1000 0000 being the binary representation of 128; but this is NOT the representation as a Two's complement number. If we consider signed bytes, we NEED a 0 at the start, so we would like to put a zero bit at the start. And since we must use full bytes in our encoding, we then must put a complete zero byte at the start.
– Bilbo
Mar 26 at 18:19
Ok, I think now I see where I was wrong. I thought as 1000 0000 being the binary representation of 128; but this is NOT the representation as a Two's complement number. If we consider signed bytes, we NEED a 0 at the start, so we would like to put a zero bit at the start. And since we must use full bytes in our encoding, we then must put a complete zero byte at the start.
– Bilbo
Mar 26 at 18:19
@Bilbo Yep, that’s pretty much it.
– bartonjs
Mar 26 at 18:20
@Bilbo Yep, that’s pretty much it.
– bartonjs
Mar 26 at 18:20
Just to add another piece that contributed to my confusion: The Microsoft article says "The Value field of the TLV triplet contains the encoded integer if it is positive, or its two's complement if it is negative." Here the OPERATION of taking the two's complement (by switching bits and adding 1) must be meant; apparently in contrast, the reference document says that always(!) "the contents octets shall be a two's complement binary number equal to the integer value". Here not the operation, but the REPRESENTATION in two's complement is meant, which switches the bits only for negative numbers.
– Bilbo
Mar 26 at 21:53
Just to add another piece that contributed to my confusion: The Microsoft article says "The Value field of the TLV triplet contains the encoded integer if it is positive, or its two's complement if it is negative." Here the OPERATION of taking the two's complement (by switching bits and adding 1) must be meant; apparently in contrast, the reference document says that always(!) "the contents octets shall be a two's complement binary number equal to the integer value". Here not the operation, but the REPRESENTATION in two's complement is meant, which switches the bits only for negative numbers.
– Bilbo
Mar 26 at 21:53
|
show 1 more comment
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55357924%2fasn-1-der-encoding-of-integers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Maybe this will help
– pepo
Mar 26 at 14:49
Unfortunately it does not so far. I had already considered this document, but it also does not seem to mention that for negative integers there is an additional zero byte added. Instead, the document says "Encoding of negative integers has its own rules" and then describes the Two's complement in its own words. But in the reference document for ASN.1 the word "negative" only appears three times, and none of these refers to the encoding of integers. So I still wonder where these "own rules" come from.
– Bilbo
Mar 26 at 16:13