我想生成一个 Eisa 3 chars Id,但似乎我太累了,看不出我到底在哪里搞砸了..加上 js 中的位移或其他任何东西还不是我最喜欢的茶;)
有人感兴趣吗?:)
看来我需要添加更多细节:我目前正在使用 vim 和 xxd 玩弄 EDID .bin,现在我在校验和方面得到了很好的东西(还没有在 js 中)和保存那个.bin,我想尝试通过编写基于Web的EDID修改工具来构建dgallegos的工作(至少为制造商、序列号等生成正确的十六进制......)
nb:非常感谢 dgallegos 的基于 Web 的 EDID 阅读器;)
var getEisaId = function()
{
var FIVE_BIT_LETTER_MASK = 0x1F;
var EISA_ID_BYTE1 = 8;
var EISA_ID_BYTE2 = 9;
var EISA_LETTER1_OFF = 2
var EISA_LETTER2_OFF = 5;
var LETTER2_TOP_BYTES = 3;
var LETTER2_TOP_MASK = 0x03;
var LETTER2_BOT_MASK = 0x07;
var firstLetter = (0xA1 >> EISA_LETTER1_OFF) &
FIVE_BIT_LETTER_MASK;
// Get the first two bits [2:0] of the top byte
var secondLetterTop = 0xA1 & LETTER2_TOP_MASK;
// Get the last three bits [7:5] of the bottom byte
var secondLetterBottom = (0x00 >> EISA_LETTER2_OFF) &
LETTER2_BOT_MASK;
// Combine the top and bottom
var secondLetter = (secondLetterTop << LETTER2_TOP_BYTES) | secondLetterBottom;
var thirdLetter = 0x00 & FIVE_BIT_LETTER_MASK;
return intToAscii(firstLetter)+intToAscii(secondLetter)+intToAscii(thirdLetter);
}
function intToAscii(intCode)
{
var abc = "0ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
return abc[intCode];
}
getEisaId();
/* ==== this is fine ;p ====*/
function asciiToInt(asciiChar){
return "0ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(asciiChar);
}
//var byte9 = ( asciiToInt("0").toString(2) & 0x1F ).toString(16)
/* ==== this is not yet aside for "HHH" :/ ====*/
var generateEisaId = function(threeCharsId){
var FIVE_BIT_LETTER_MASK = 0x1F;
var EISA_LETTER1_OFF = 2;
var EISA_LETTER2_OFF = 5;
var LETTER2_TOP_BYTES = 3;
var LETTER2_TOP_MASK = 0x03;
var LETTER2_BOT_MASK = 0x07; // 111 in base 2
var charsArr = threeCharsId.split('').splice(0, 3); // delete anything after 3rd char
// format 1st char
var firstLetterBin = asciiToInt(charsArr[0]); //.toString(2); // get index from letter, and get its binary representation
console.log('1st letter: ' + charsArr[0] + ' > ' + asciiToInt(charsArr[0]) + ' (int/base10) > ' + firstLetterBin.toString(2) + ' (bin/base2)');
// format 2nd char
var secondLetterBin = asciiToInt(charsArr[1]); //.toString(2);
console.log('2nd letter: ' + charsArr[1] + ' > ' + asciiToInt(charsArr[1]) + ' (int/base10) > ' + secondLetterBin.toString(2) + ' (bin/base2)');
// get the second letter binary chunk for the 1st two bits of the top byte
//var secondLetterTopBin = secondLetterBin >> LETTER2_TOP_MASK;
//var secondLetterTopBin = secondLetterBin & LETTER2_TOP_MASK;
//var secondLetterTopBin = secondLetterBin & LETTER2_TOP_MASK;
var secondLetterTopBin = secondLetterBin >> 3; // shift 3 positions right ( drop stuff )
console.log('2nd letter top bin: ' + secondLetterTopBin.toString(2));
// get the second letter binary chunk for the last three bits of the bottom byte
//var secondLetterBottomBin = secondLetterBin & LETTER2_BOT_MASK;
var secondLetterBottomBin = ( secondLetterBin << 2 ) & 0x07;
console.log('2nd letter bottom bin: ' + secondLetterBottomBin.toString(2));
// format Last char
var thirdLetterBin = asciiToInt(charsArr[2]); //.toString(2);
console.log('3rd letter: ' + charsArr[2] + ' > ' + asciiToInt(charsArr[2]) + ' (int/base10) > ' + thirdLetterBin.toString(2) + ' (bin/base2)');
// 1st byte - add 1st char binary to top byte & shift it 2 positions left to make room for 2nd char 1st binary chunk of 2 bits
var firstLetterOnceOffset = ( firstLetterBin << EISA_LETTER1_OFF ) & FIVE_BIT_LETTER_MASK;
//var firstLetterOnceOffset = ( firstLetterBin & 0xA0 );
console.log('1st letter once offset: ' + firstLetterOnceOffset.toString(2));
//var topByte = ( ( firstLetterBin << EISA_LETTER1_OFF ) | secondLetterTopBin ) >> 2;
var topByte = ( firstLetterBin << EISA_LETTER1_OFF ) | secondLetterTopBin;
console.log('top byte: ' + topByte.toString(2) );
// 2nd byte - add 3rd char binary to bottom byte & shift it 3 positions right to make room for 2nd char 2nd binary chunk of 3 bits
//var bottomByte = ( thirdLetterBin & FIVE_BIT_LETTER_MASK ) | ( secondLetterBottomBin << EISA_LETTER2_OFF )
//var bottomByte = ( thirdLetterBin & FIVE_BIT_LETTER_MASK ) | ( secondLetterBottomBin << EISA_LETTER2_OFF );
//var bottomByte = ( thirdLetterBin & FIVE_BIT_LETTER_MASK );
var bottomByte = thirdLetterBin | ( secondLetterBottomBin << EISA_LETTER2_OFF ); // & 0xA0;
//var bottomByte = ( thirdLetterBin & FIVE_BIT_LETTER_MASK ) | ( secondLetterBottomBin << 8 );
//var bottomByte = ( thirdLetterBin & FIVE_BIT_LETTER_MASK ) | ( secondLetterBottomBin << 8 ); // &0x1F;
console.log('bottom byte: ' + bottomByte.toString(2));
// padding ?
var n1 = topByte.toString(2);
n1 = "00000000".substr(n1.length) + n1;
console.log('padded top byte: ' + n1);
var n2 = bottomByte.toString(2);
n2 = "00000000".substr(n2.length) + n2;
console.log('padded bottom byte: ' + n2);
// get hex's of the padded versions ?
var eisaIdP = '0x' + parseInt(n1, 2).toString(16) + ' 0x' + parseInt(n2, 2).toString(16);
console.log('padded: ' + eisaIdP);
// get hex's for both of the aboves
var eisaId = '0x' + topByte.toString(16) + bottomByte.toString(16);
return eisaId;
}
var myId = generateEisaId('HHH');
var myId = generateEisaId('AAA');
console.log('generated Eisa Id hex: ' + myId);