Jump to content

Creating check digit from Hex code number


Cep

Recommended Posts

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);
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.