我想生成一个Eisa 3个字符的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);
答案 0 :(得分:0)
好吧,经过一番睡眠并回头看一看,“它变得很清楚”;)
对于一些背景知识,将3个字母分成2个字节。 我们有一个索引字母的“字典”作为整数数组。 从存储索引的数组的长度来看,我们知道每个字母可用的位最大为5位(如果要在上述2个字节中存储这3个字母,则在第一个字节上给我们一个未设置的位),因为用作“字典”中最后一个字符的索引的整数在一个字节中占5位(因此被认为是最大值)
试图使字母“ Z”显示3次(因此“ ZZZ”)将导致以下过程:
/*
Z -> 26 (dec/base10)
0x1A (hex/base16)
11010 ( bin/base2)
Z: 11010
padded Z: 00011010
bytes: 00000000 | 00000000
split: 0 00000 00 | 000 00000
result: 0 11010 11 | 010 11010
formatted: 01101011 | 01011010
hex: 0x6b | 0x5a
-> SUCCESS! ==> gives us 'ZZZ'
*/
任何东西(至少对我来说)都是带有一点点颜色的清除器,以下内容确实很有帮助(但不能突出显示“两位操作之间的0丢失”。.?? ..)
R:检查实际的浏览器控制台;)
var logBits = function(num, showHex){
var numBits = num.toString(2);
//var prefixLen = 8 - numBits;
var prefixStr = '';
//console.log(8 - numBits.length);
for(var i=0; i< 8 - numBits.length; i++){ prefixStr +='0'; }
if(showHex === true) console.log('0b%c' + prefixStr + '%c' + numBits + ' 0x' + num.toString(16), 'color: blue;', 'color: black;');
else console.log('0b%c' + prefixStr + '%c' + numBits, 'color: blue;', 'color: black;');
}
// to do: alternate version that colors in other color ( or doesn't color at all )
// "leading zeros that were lost we manipulating bits" to better perceive the changes in logs
// usage:
logBits(26);
一旦编写了上面的内容,其余的都紧随其后,所以这里是:
// helper
function asciiToInt(asciiChar){
return "0ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(asciiChar);
}
var genEisaId = function(threeCharsId, arr){
var charsArr = threeCharsId.split('').splice(0, 3); // delete anything after 3rd char
// get mapping idx's
var firstLetterBin = asciiToInt(charsArr[0]);
var secondLetterBin = asciiToInt(charsArr[1]);
var thirdLetterBin = asciiToInt(charsArr[2]);
// process
var secondLetter1stChunk = secondLetterBin >> 3; // discard the three last bits
var secondLetter2ndChunk = secondLetterBin & 0x07; //0b00111; // gets only the three last bits
// build
var firstByte = (firstLetterBin << 2 ) | secondLetter1stChunk;
var secondByte = (secondLetter2ndChunk << 5) | thirdLetterBin;
// post-proc
//var firstByteHex = firstByte.toString(16);
//var secondByteHex = secondByte.toString(16);
var firstByteHex = (firstByte.toString(16).length == 2 )? firstByte.toString(16) : '0' + firstByte.toString(16);
var secondByteHex = (secondByte.toString(16).length == 2) ? secondByte.toString(16) : '0' + secondByte.toString(16);
// return hex's
if(arr === true) return['0x' + firstByteHex, '0x' + secondByteHex];
else return '0x' + firstByteHex + ' 0x' + secondByteHex;
}
// usage:
console.log ( genEisaId('TEF') )
上面的函数在给定一个字符字符串时会很高兴地生成任何3个字母的EISA ID十六进制(如果太长,则可能会丢弃一些;))
再次感谢@dgallegos在http://www.edidreader.com/上所做的工作并在github上共享它(通过深入研究“ getEisaId()”和“ intToAscii”以获取映射和解码的基础,这很有帮助)。
我希望发布'会帮助别人也生成一个ID或发现他的有用工具:)