string 長怎樣,轉過去bytes就怎樣
限制string長度必須為偶數
protected byte[] StringToBytes(string HexString) { int byteLength = HexString.Length / 2; byte[] bytes = new byte[byteLength]; string hex; int j = 0; for (int i = 0; i < bytes.Length; i++) { hex = new String(new Char[] { HexString[j], HexString[j + 1] }); bytes[i] = HexToByte(hex); j = j + 2; } return bytes; } private byte HexToByte(string hex) { if (hex.Length > 2 || hex.Length <= 0) throw new ArgumentException("hex must be 1 or 2 characters in length"); byte newByte = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber); return newByte; }
沒有留言:
張貼留言