Cep Posted February 11, 2008 Share Posted February 11, 2008 Hello, I am trying to create a unique reference number with the following convention, first char = region code 4 hex digits = decimal id number final char = check digit I have come up with a little function that I want to test out but I am having a problem type juggling the string into a hex number in order to perform the calculation. Can anyone help me because I do not understand what is wrong? <?php $a = "FA45C6"; function breakdown($code) { $region = substr($code, 0); $check = substr($code, -1, 1); $hex = "0x".substr($code, 1, -1); $calc = (int)$hex % 10; if ($calc==$check) { echo "We have a correct check digit"; } else { echo "Houston we have a problem - calc is {$calc}"; } } breakdown($a); ?> Link to comment https://forums.phpfreaks.com/topic/90514-creating-check-digit-from-hex-code-number/ Share on other sites More sharing options...
btherl Posted February 12, 2008 Share Posted February 12, 2008 For base conversion, it's best to use base_convert() The reason is that "0x" is part of php's syntax, and is not considered when turning strings into integers. You could use eval() to force it to interpret it as php syntax, but that's overkill. Link to comment https://forums.phpfreaks.com/topic/90514-creating-check-digit-from-hex-code-number/#findComment-464472 Share on other sites More sharing options...
Cep Posted February 12, 2008 Author Share Posted February 12, 2008 Thanks btherl, I see what you mean. Link to comment https://forums.phpfreaks.com/topic/90514-creating-check-digit-from-hex-code-number/#findComment-464706 Share on other sites More sharing options...
mainewoods Posted February 20, 2008 Share Posted February 20, 2008 hexdec() http://us2.php.net/manual/en/function.hexdec.php $dec = hexdec(substr($code, 1, -1)); $calc = $dec % 10; Link to comment https://forums.phpfreaks.com/topic/90514-creating-check-digit-from-hex-code-number/#findComment-471405 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.