Handle the difference between PyNumber_ToBase(16) and PyString_Format("%x")
diff --git a/OpenSSL/crypto/x509.c b/OpenSSL/crypto/x509.c
index 0b54c8d..6156da0 100644
--- a/OpenSSL/crypto/x509.c
+++ b/OpenSSL/crypto/x509.c
@@ -95,6 +95,7 @@
     PyObject *hex = NULL;
     ASN1_INTEGER *asn1_i = NULL;
     BIGNUM *bignum = NULL;
+    char *hexbytes;
 
     if (!PyArg_ParseTuple(args, "O:set_serial_number", &serial)) {
         return NULL;
@@ -110,21 +111,17 @@
         goto err;
     }
 
-#ifdef PY3
-    {
-        PyObject *hexbytes = PyUnicode_AsASCIIString(hex);
-        Py_DECREF(hex);
-        hex = hexbytes;
-    }
-#endif
-
     /**
      * BN_hex2bn stores the result in &bignum.  Unless it doesn't feel like
      * it.  If bignum is still NULL after this call, then the return value
      * is actually the result.  I hope.  -exarkun
      */
-    /* +2 to skip the "0x" */
-    small_serial = BN_hex2bn(&bignum, PyBytes_AsString(hex) + 2);
+    hexbytes = PyBytes_AsString(hex);
+    if (hexbytes[2] == 'x') {
+        /* +2 to skip the "0x" */
+        hexbytes += 2;
+    }
+    small_serial = BN_hex2bn(&bignum, hexbytes);
 
     Py_DECREF(hex);
     hex = NULL;
diff --git a/OpenSSL/util.c b/OpenSSL/util.c
index 76a4a25..661bb1d 100644
--- a/OpenSSL/util.c
+++ b/OpenSSL/util.c
@@ -77,6 +77,14 @@
         goto err;
     }
 
+#ifdef PY3
+    {
+        PyObject *hexbytes = PyUnicode_AsASCIIString(hex);
+        Py_DECREF(hex);
+        hex = hexbytes;
+    }
+#endif
+
     return hex;
 
   err: