LBA与C/H/S相互转化计算--计算扇区数量,含c++计算

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/c5113620/article/details/84112888

计算扇区时需要转换 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;
}

猜你喜欢

转载自blog.csdn.net/c5113620/article/details/84112888