fix https://sourceforge.net/p/asn1c/bugs/51/
diff --git a/skeletons/constr_CHOICE.c b/skeletons/constr_CHOICE.c
index a9eb719..2be69ab 100644
--- a/skeletons/constr_CHOICE.c
+++ b/skeletons/constr_CHOICE.c
@@ -913,6 +913,7 @@
 	asn_per_constraint_t *ct;
 	void *memb_ptr;
 	int present;
+	int present_enc;
 
 	if(!sptr) _ASN_ENCODE_FAILED;
 
@@ -934,15 +935,17 @@
 	else
 		present--;
 
-	/* Adjust if canonical order is different from natural order */
-	if(specs->canonical_order)
-		present = specs->canonical_order[present];
-
 	ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present);
 
+	/* Adjust if canonical order is different from natural order */
+	if(specs->canonical_order)
+		present_enc = specs->canonical_order[present];
+	else
+		present_enc = present;
+
 	if(ct && ct->range_bits >= 0) {
-		if(present < ct->lower_bound
-		|| present > ct->upper_bound) {
+		if(present_enc < ct->lower_bound
+		|| present_enc > ct->upper_bound) {
 			if(ct->flags & APC_EXTENSIBLE) {
 				if(per_put_few_bits(po, 1, 1))
 					_ASN_ENCODE_FAILED;
@@ -966,7 +969,7 @@
 	}
 
 	if(ct && ct->range_bits >= 0) {
-		if(per_put_few_bits(po, present, ct->range_bits))
+		if(per_put_few_bits(po, present_enc, ct->range_bits))
 			_ASN_ENCODE_FAILED;
 
 		return elm->type->uper_encoder(elm->type, elm->per_constraints,
@@ -975,7 +978,7 @@
 		asn_enc_rval_t rval;
 		if(specs->ext_start == -1)
 			_ASN_ENCODE_FAILED;
-		if(uper_put_nsnnwn(po, present - specs->ext_start))
+		if(uper_put_nsnnwn(po, present_enc - specs->ext_start))
 			_ASN_ENCODE_FAILED;
 		if(uper_open_type_put(elm->type, elm->per_constraints,
 			memb_ptr, po))