我正在寫一個二進制文件轉換器,我需要將1-6字節的數組轉換爲int(short-long)值。目前我正在使用以下三個功能,但我想知道是否有改進性能的方法?如何提高將byte []轉換爲int的性能?
private string byteToShortParse(byte[] recordData, int offset, int length)
{
byte[] workingSet = new byte[2];
Buffer.BlockCopy(recordData, offset, workingSet, 0, length);
return (BitConverter.ToInt16(workingSet, 0).ToString());
}
private string byteToIntParse(byte[] recordData, int offset, int length)
{
byte[] workingSet = new byte[4];
Buffer.BlockCopy(recordData, offset, workingSet, 0, length);
return (BitConverter.ToInt32(workingSet, 0).ToString());
}
private string byteToLongParse(byte[] recordData, int offset, int length)
{
byte[] workingSet = new byte[8];
Buffer.BlockCopy(recordData, offset, workingSet, 0, length);
return (BitConverter.ToInt32(workingSet, 0).ToString());
}
如果你正在處理的字節真的沒有太多的優化,你需要做的。 – BoltClock 2011-01-28 20:46:12
yes recordData是一個bytearray – Asha 2011-01-28 20:47:54
這對codereview.stackexchange.com來說是一個很好的問題 – 2011-01-28 20:48:06