// Read Track 2 Equivalent Data
bReadTrack2 = FALSE;
memset(sTemp, 0, sizeof(sTemp));
iRet = emv_get_data_element("\x57", 1, &iLength, sTemp);
iErrNo = iRet?errno:0;
if( iErrNo==EMV_RET_SUCCESS )
{
bReadTrack2 = TRUE;
BcdToAsc0(PosCom.szTrack2, sTemp, iLength*2);
RemoveTailChars(PosCom.szTrack2, 'F'); // erase padded 'F' chars
for(i=0; PosCom.szTrack2[i]!='\0'; i++) // convert 'D' to '='
{
if( PosCom.szTrack2[i]=='D' )
{
PosCom.szTrack2[i] = '=';
break;
}
}
}
void RemoveTailChars(uint8_t *pszString, uint8_t ucRemove)
{
int i, iLen;
iLen = strlen((char *)pszString);
for(i=iLen-1; i>0; i--)
{
if( pszString[i]!=ucRemove )
{
break;
}
pszString[i] = 0;
}
}
JAVA实现
byte[] track2 = Arrays.copyOfRange(data, 2, 2 + data[1] & 0xFF);
int track2Length = data[1] & 0xFF;
String trackContent = tools.hexString(track2);
// 删除结尾的 'F',替换 'D' 为 '='
String formattedTrack2 = trackContent.replaceAll("F*$", "").replace('D', '=');
String finalTrack2 = track2Length + formattedTrack2;
card.setTrack2(finalTrack2);