If I move funds from an ESA into a 529 plan, does the cost basis get reset?

I know I can withdraw money from an Education Savings Account and then contribute that money to a 529 plan. That is considered a qualified withdrawal. Does anyone know if the cost basis gets reset when doing such a pair of transactions? I ask because I may want to use a 529 to pay for a student loan (up to $10K allowed). I live in NY where using a 529 for student loans, although not taxable on a federal level, is taxable on the gains on a New York State level. But if the cost basis gets reset, I can withdraw from the ESA, contribute to the 529 and then withdraw from the 529 to pay the student loan and not pay NYS taxes.