public static string Bcd2Ascii(byte[] packedBytes, ref int index, ISOFieldPadding padding, int valueLength) { var value = valueLength <= MaxStackAllocationSize ? stackalloc char[valueLength] : new char[valueLength]; if (valueLength % 2 == 0) { // no padding needed just convert to bcd for (int i = 0; i < valueLength; i += 2) { value[i] = (char)((packedBytes[index] >> 4) + 0x30); value[i + 1] = (char)((packedBytes[index] & 0x0F) + 0x30); index++; } } else { if (padding == ISOFieldPadding.LEFT) { // LEFT padding so ignore the first half byte/ just read the second half byte value[0] = (char)((packedBytes[index] & 0x0F) + 0x30); index++; for (int i = 1; i < valueLength; i += 2) { value[i] = (char)((packedBytes[index] >> 4) + 0x30); value[i + 1] = (char)((packedBytes[index] & 0x0F) + 0x30); index++; } } if (padding == ISOFieldPadding.RIGHT) { // RIGHT padding so ignore the last half byte/ just read the first half of last byte int i; for (i = 0; i < valueLength - 1; i += 2) { value[i] = (char)((packedBytes[index] >> 4) + 0x30); value[i + 1] = (char)((packedBytes[index] & 0x0F) + 0x30); index++; } value[i] = (char)((packedBytes[index] >> 4) + 0x30); } } return(value.ToString()); }
public static void Ascii2Bcd(string value, byte[] packedBytes, ref int index, ISOFieldPadding padding, byte paddingCharacter) { int valueLength = value.Length; int startIndex = 0; bool needPadding = valueLength % 2 != 0; if (needPadding && padding == ISOFieldPadding.LEFT) { //Read data and take first nibble if (paddingCharacter != 0) { //left nibble padding set to padding character and add the single nibble at the end packedBytes[index] = (byte)((paddingCharacter << 4) + (value[0] - 0x30)); } else { //left nibble padding leave empty and make a byte from the first nibble of the data packedBytes[index] = (byte)(value[0] - 0x30); } index++; //ignore the first char of the value, since we have already read it. startIndex++; } else if (needPadding && padding == ISOFieldPadding.RIGHT) { //Stop before the last half nibble, in the loop valueLength--; } // no padding needed just convert to bcd for (int i = startIndex; i < valueLength; i += 2) { packedBytes[index] = (byte)(((value[i] - 0x30) << 4) + value[i + 1] - 0x30); index++; } if (needPadding && padding == ISOFieldPadding.RIGHT) { //read the last remaining nibble in the value and pad the last byte if (paddingCharacter != 0) { //right nibble padding, take the last nibble and set it as the high part of the byte, leave the low/right side as the padding character packedBytes[index] = (byte)(((value[valueLength] - 0x30) << 4) + paddingCharacter); } else { //right nibble padding, take the last nibble and set it as the high part of the byte, leave the low/right side as 0 packedBytes[index] = (byte)((value[valueLength] - 0x30) << 4); } index++; } }
/// <summary> /// Do not use /// </summary> /// <param name="value"></param> /// <param name="packedBytes"></param> /// <param name="index"></param> /// <param name="padding"></param> public static void Ascii2BcdOld(string value, byte[] packedBytes, ref int index, ISOFieldPadding padding) { int valueLength = value.Length; if (valueLength % 2 == 0) { // no padding needed just convert to bcd for (int i = 0; i < valueLength; i += 2) { packedBytes[index] = (byte)((value[i] - 0x30) * 0x10 + value[i + 1] - 0x30); index++; } } else { // one for end of string and one for padding char string bcdString = string.Empty; if (padding == ISOFieldPadding.LEFT) { bcdString = "0" + value; } else if (padding == ISOFieldPadding.RIGHT) { bcdString = value + "0"; } valueLength = bcdString.Length; for (int i = 0; i < valueLength; i += 2) { packedBytes[index] = (byte)((bcdString[i] - 0x30) * 0x10 + bcdString[i + 1] - 0x30); index++; } } }
public static void Ascii2Bcd(string value, byte[] packedBytes, ref int index, ISOFieldPadding padding) { int valueLength = value.Length; int startIndex = 0; bool needPadding = valueLength % 2 != 0; if (needPadding && padding == ISOFieldPadding.LEFT) { //left nibble padding leave empty and make a byte from the first nibble of the data packedBytes[index] = (byte)(value[0] - 0x30); index++; //ignore the first char of the value startIndex++; } else if (needPadding && padding == ISOFieldPadding.RIGHT) { //Stop before the last half nibble valueLength--; } // no padding needed just convert to bcd for (int i = startIndex; i < valueLength; i += 2) { packedBytes[index] = (byte)((value[i] - 0x30) * 0x10 + value[i + 1] - 0x30); index++; } if (needPadding && padding == ISOFieldPadding.RIGHT) { //right nibble padding, take the last nibble and set it as the high part of the byte, leave the low/right side as 0 packedBytes[index] = (byte)((value[valueLength] - 0x30) * 0x10); index++; } }