⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pduutils.java

📁 发送短信 接收短信 多种接口com/net/modem 开发库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		else
		{
			// 
			String decodedUdh = unencodedSeptetsToString(encodedSeptetsToUnencodedSeptets(udhData, false));
			String decoded = unencodedSeptetsToString(encodedSeptetsToUnencodedSeptets(encodedPduData));
			//System.out.println("'"+decoded+"'");
			//System.out.println("'"+decodedUdh+"'");
			return decoded.substring(decodedUdh.length());
		}
	}

	public static String decode8bitEncoding(byte[] udhData, byte[] pduData)
	{
		// standard 8-bit characters
		try
		{
			int udhLength = ((udhData == null) ? 0 : udhData.length);
			return new String(pduData, udhLength, pduData.length - udhLength, "ISO8859_1");
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	}

	public static String decodeUcs2Encoding(byte[] udhData, byte[] pduData)
	{
		try
		{
			int udhLength = ((udhData == null) ? 0 : udhData.length);
			// standard unicode
			return new String(pduData, udhLength, pduData.length - udhLength, "UTF-16");
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	}

	public static byte swapNibbles(int b)
	{
		return (byte) (((b << 4) & 0xF0) | ((b >>> 4) & 0x0F));
	}

	public static String readBCDNumbers(int numDigits, byte[] addressData)
	{
		// reads length BCD numbers from the current position
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < addressData.length; i++)
		{
			int b = addressData[i];
			int num1 = b & 0x0F;
			sb.append(num1);
			int num2 = (b >>> 4) & 0x0F;
			if (num2 != 0x0F)
			{
				// check if fillbits
				sb.append(num2);
			}
		}
		return sb.toString();
	}

	public static int createSwappedBCD(int decimal)
	{
		// creates a swapped BCD representation of a 2-digit decimal
		int tens = (decimal & 0xFF) / 10;
		int ones = (decimal & 0xFF) - (tens * 10);
		return (ones << 4) | tens;
	}

	// CONVERSION UTILITIES
	// from Java String to Pdu
	public static String stringToPdu(String txt)
	{
		byte[] txtBytes = stringToUnencodedSeptets(txt);
		return bytesToPdu(unencodedSeptetsToEncodedSeptets(txtBytes));
	}

	// from Java String to uncompressed septets (GSM characters)
	public static byte[] stringToUnencodedSeptets(String s)
	{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int i, j, k, index;
		char ch;
		k = 0;
		for (i = 0; i < s.length(); i++)
		{
			ch = s.charAt(i);
			index = -1;
			for (j = 0; j < extAlphabet.length; j++)
				if (extAlphabet[j] == ch)
				{
					index = j;
					break;
				}
			if (index != -1) // An extended char...
			{
				baos.write((byte) Integer.parseInt(extBytes[index].substring(0, 2), 16));
				k++;
				baos.write((byte) Integer.parseInt(extBytes[index].substring(2, 4), 16));
				k++;
			}
			else
			// Maybe a standard char...
			{
				index = -1;
				for (j = 0; j < stdAlphabet.length; j++)
					if (stdAlphabet[j] == ch)
					{
						index = j;
						baos.write((byte) j);
						k++;
						break;
					}
				if (index == -1) // Maybe a Greek Char...
				{
					for (j = 0; j < grcAlphabetRemapping.length; j++)
						if (grcAlphabetRemapping[j][0] == ch)
						{
							index = j;
							ch = grcAlphabetRemapping[j][1];
							break;
						}
					if (index != -1)
					{
						for (j = 0; j < stdAlphabet.length; j++)
							if (stdAlphabet[j] == ch)
							{
								index = j;
								baos.write((byte) j);
								k++;
								break;
							}
					}
					else
					// Unknown char replacement...
					{
						baos.write((byte) ' ');
						k++;
					}
				}
			}
		}
		return baos.toByteArray();
	}

	// from compress unencoded septets
	public static byte[] unencodedSeptetsToEncodedSeptets(byte[] septetBytes)
	{
		byte[] txtBytes;
		byte[] txtSeptets;
		int txtBytesLen;
		BitSet bits;
		int i, j;
		txtBytes = septetBytes;
		txtBytesLen = txtBytes.length;
		bits = new BitSet();
		for (i = 0; i < txtBytesLen; i++)
			for (j = 0; j < 7; j++)
				if ((txtBytes[i] & (1 << j)) != 0) bits.set((i * 7) + j);
		// big diff here
		int encodedSeptetByteArrayLength = txtBytesLen * 7 / 8 + ((txtBytesLen * 7 % 8 != 0) ? 1 : 0);
		txtSeptets = new byte[encodedSeptetByteArrayLength];
		for (i = 0; i < encodedSeptetByteArrayLength; i++)
		{
			for (j = 0; j < 8; j++)
			{
				txtSeptets[i] |= (byte) ((bits.get((i * 8) + j) ? 1 : 0) << j);
			}
		}
		return txtSeptets;
	}

	public static int getNumMultiCharsInSeptets(byte[] bytes)
	{
		int count = 0;
		int i;
		for (i = 0; i < bytes.length; i++)
		{
			if (bytes[i] == 0x1b)
			{
				count++;
			}
		}
		return count;
	}

	// from GSM characters to java string
	public static String unencodedSeptetsToString(byte[] bytes)
	{
		StringBuffer text;
		String extChar;
		int i, j;
		text = new StringBuffer();
		for (i = 0; i < bytes.length; i++)
		{
			if (bytes[i] == 0x1b)
			{
				// NOTE: - ++i can be a problem if the '1b'
				//         is right at the end of a PDU
				//       - this will be an issue for displaying 
				//         partial PDUs e.g. via toString()
				if (i < bytes.length - 1)
				{
					extChar = "1b" + Integer.toHexString(bytes[++i]);
					for (j = 0; j < extBytes.length; j++)
						if (extBytes[j].equalsIgnoreCase(extChar)) text.append(extAlphabet[j]);
				}
			}
			else
			{
				text.append(stdAlphabet[bytes[i]]);
			}
		}
		return text.toString();
	}

	public static String encodedSeptetsToString(byte[] encodedSeptets)
	{
		return unencodedSeptetsToString(encodedSeptetsToUnencodedSeptets(encodedSeptets));
	}

	public static int getNumSeptetsForOctets(int numOctets)
	{
		return numOctets * 8 / 7 + ((numOctets * 8 % 7 != 0) ? 1 : 0);
		//return numOctets + (numOctets/7);
	}

	// decompress encoded septets to unencoded form
	public static byte[] encodedSeptetsToUnencodedSeptets(byte[] octetBytes)
	{
		return encodedSeptetsToUnencodedSeptets(octetBytes, true);
	}

	public static byte[] encodedSeptetsToUnencodedSeptets(byte[] octetBytes, boolean discardLast)
	{
		byte newBytes[];
		BitSet bitSet;
		int i, j, value1, value2;
		bitSet = new BitSet(octetBytes.length * 8);
		value1 = 0;
		for (i = 0; i < octetBytes.length; i++)
			for (j = 0; j < 8; j++)
			{
				value1 = (i * 8) + j;
				if ((octetBytes[i] & (1 << j)) != 0) bitSet.set(value1);
			}
		value1++;
		// this is a bit count NOT a byte count
		value2 = value1 / 7 + ((value1 % 7 != 0) ? 1 : 0); // big diff here
		//System.out.println(octetBytes.length);
		//System.out.println(value1+" --> "+value2);
		if (value2 == 0) value2++;
		newBytes = new byte[value2];
		for (i = 0; i < value2; i++)
		{
			for (j = 0; j < 7; j++)
			{
				if ((value1 + 1) > (i * 7 + j))
				{
					if (bitSet.get(i * 7 + j))
					{
						newBytes[i] |= (byte) (1 << j);
					}
				}
			}
		}
		if (discardLast)
		{
			// when decoding a 7bit encoded string 
			// the last septet may become 0, this should be discarded
			// since this is an artifact of the encoding not part of the 
			// original string
			// this is only done for decoding 7bit encoded text NOT for
			// reversing octets to septets (e.g. for the encoding the UDH)
			if (newBytes[newBytes.length - 1] == 0)
			{
				byte[] retVal = new byte[newBytes.length - 1];
				System.arraycopy(newBytes, 0, retVal, 0, retVal.length);
				return retVal;
			}
		}
		return newBytes;
	}

	// converts a PDU style string to a byte array
	public static byte[] pduToBytes(String s)
	{
		byte[] bytes = new byte[s.length() / 2];
		for (int i = 0; i < s.length(); i += 2)
		{
			bytes[i / 2] = (byte) (Integer.parseInt(s.substring(i, i + 2), 16));
		}
		return bytes;
	}

	// converts a PDU style string to a bit string
	public static String pduToBits(String pduString)
	{
		return bytesToBits(pduToBytes(pduString));
	}

	// converts a byte array to PDU style string
	public static String bytesToPdu(byte[] bytes)
	{
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < bytes.length; i++)
		{
			sb.append(byteToPdu(bytes[i] & 0xFF));
		}
		return sb.toString();
	}

	// converts a byte array to a bit string
	public static String bytesToBits(byte[] b)
	{
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < b.length; i++)
		{
			String bits = Integer.toBinaryString(b[i] & 0xFF);
			while (bits.length() < 8)
			{
				bits = "0" + bits;
			}
			if (i > 0)
			{
				sb.append(" ");
			}
			sb.append(bits);
		}
		return sb.toString();
	}

	public static String byteToBits(byte b)
	{
		String bits = Integer.toBinaryString(b & 0xFF);
		while (bits.length() < 8)
		{
			bits = "0" + bits;
		}
		return bits;
	}

	public static String byteToPdu(int b)
	{
		StringBuffer sb = new StringBuffer();
		String s = Integer.toHexString(b & 0xFF);
		if (s.length() == 1)
		{
			sb.append("0");
		}
		sb.append(s);
		return sb.toString().toUpperCase();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -