1. 程式人生 > >LBA與C/H/S相互轉化計算--計算扇區數量,含c++計算

LBA與C/H/S相互轉化計算--計算扇區數量,含c++計算

計算扇區時需要轉換 LBA與C/H/S
C/H/S | Cylinder/Head/Sector | 柱面/磁頭/扇區 ,是舊式磁碟尋道方式
LBA | Logical Block Address ,是抽象的扇區,可以理解為線性直鋪的扇區,從0–max

CS表示起始柱面號,HS表示起始磁頭號,SS表示起始扇區號,PS表示每磁軌扇區數,PH表示每柱面磁軌數,CS=0,HS=0,SS=1,PS=63,PH=255

LBA=(C–CS)﹡PH﹡PS+(H–HS)﹡PS+(S–SS)-1

C=LBA / (PH x PS)+CS    , /是整除,求商,%模,求餘數
H=(LBA / PS)% PH+HS
S=LBA % PS+SS

例如
當C/H/S=0/0/1 時,代入公式得LBA=0
當C/H/S=0/0/63時,代入公式得LBA=62
當C/H/S=0/1/1 時,代入公式得LBA=63

以下是c++計算

#include <iostream>

using namespace std;

void printChoice()
{
	cout << "[1] LBA   to  C/H/S" << endl;
	cout << "[2] C/H/S to  LBA" << endl;
	cout << "input choice: ";
	return;
}
//CS表示起始柱面號,HS表示起始磁頭號,SS表示起始扇區號,PS表示每磁軌扇區數,PH表示每柱面磁軌數,CS=0,HS=0,SS=1,PS=63,PH=255

/*
C=LBA / (PH x PS)+CS    , /是整除,求商,%模,求餘數
H=(LBA / PS)% PH+HS
S=LBA % PS+SS
*/
void lBAtoCHS(unsigned int lba)
{
	cout << "get LBA:"<<lba<<endl;
	unsigned int c, h, s;
	c = lba / (255 * 63);
	h = (lba / 63) % 255;
	s = lba % 63 + 1;
	cout << "to C/H/S: " << c << "/" << h << "/" << s << endl;
	return;
}

/*
LBA=(C–CS)﹡PH﹡PS+(H–HS)﹡PS+(S–SS)-1
*/
void cHStoLBA(unsigned int c, unsigned int h, unsigned int s) {
	cout << "get C/H/S:"<<c<<"/"<<h<<"/"<<s <<endl;
	unsigned int lba;
	lba = c * 255 * 63 + h * 63 + s - 1;
	cout << "toLBA: " << lba << endl;
	return;
}

int main()
{
start:
	printChoice();
	unsigned int choice,lba,c,h,s;
	cin >> choice;

	switch (choice)
	{
	case 1:
			cout << "input LBA: ";
			cin >> lba;
			lBAtoCHS(lba);
			break;
	case 2:
			cout << "input C/H/S each with delimiter(space): ";
			cin >> c >> h >> s;
			cHStoLBA(c,h,s);
			break;
	default:
		goto start;
		break;
	}

    return 0;
}