That is pretty much what the function replaceEntities__callback will do ... I replaced those hideous nested ternary create functions with permanent functions since the former hurt my brain 
So just copy that function to the importer and do
preg_replace_callback('~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~', 'replaceEntities__callback', $buf);
Now that second regex one seems a bit mysterious to me, I'm trying to see a pattern that that the first does not cover ... well it would capture &#xx123; but thats not a valid code ... maybe it was added for some damaged input? I'm tempted to say just the first one is enough as the function checks for hex numbers.