简体   繁体   English

十六进制到二进制和二进制操作的麻烦

[英]Hex to Binary & Binary Operation Troubles

Here is the skinny: I am slapping together some code that will convert an EUI64 back into a MAC address.这是瘦身:我正在拼凑一些将EUI64转换回MAC地址的代码。 Logically, this is pretty simple, and I thought the code wouldn't be much of an issue either, but I am having some trouble.从逻辑上讲,这很简单,我认为代码也不是什么大问题,但我遇到了一些麻烦。

Side note: My experience with Javascript is minimal旁注:我对 Javascript 的经验很少

So far, my problem is:到目前为止,我的问题是:

  • Can't tell Javascript to define a number as it being Hex不能告诉 Javascript 将数字定义为十六进制
  • Can't alter a specific binary within the number after it has been converted转换后无法更改数字中的特定二进制

I know within Javascript a Hex is defined by adding 0x before the number, but it doesn't seem to like when I try to do that with a variable, and applying toString(16) doesn't seem to alter it at all.我知道在 Javascript 中,十六进制是通过在数字前添加 0x 来定义的,但是当我尝试使用变量执行此操作时,它似乎并不像,并且应用 toString(16) 似乎根本没有改变它。

Proceeding is my code.继续是我的代码。 I recognize that it is wrong (that is why I am here), and I thought it would be a good to show where I am at (though a lot of what I have tried is not shown).我承认这是错误的(这就是我在这里的原因),并且我认为展示我所处的位置会很好(尽管我尝试过的很多东西都没有展示出来)。

eui64toMac=function (eui64)
{
  console.log("EUI64: " + eui64);
  console.log("EUI64 as Hex: " + eui64.toString(16));

  console.log("Isolating and converting first two numbers of EUI64 to Binary.");
  
  convert=(eui64.slice(0, 2));
  console.log("First two of EUI64: " + convert);
  console.log("First two of EUI64 as Hex: " + convert.toString(16));
  convert=(convert >>> 0).toString(2);
  console.log("First two of EUI64 (Binary): " + convert);
  
  console.log("First binary bit: " + convert[0]);
  console.log("Attempt at changing the 1st binary to a 0.");
  // Attempt at changing the 1st binary to a 0 (with no luck)
  var num = 0;
  convert[0] = num;
  console.log("Third binary in sequence change attempt: " + convert[0]);
  
  console.log("Converted number after conversion: " + convert);
    
    return eui64;
}

eui64toMac("92152BFFFEE49B60");

Output: Output:

☁️ "Running fiddle"
"EUI64: 92152BFFFEE49B60"
"EUI64 as Hex: 92152BFFFEE49B60"
"Isolating and converting first two numbers of EUI64 to Binary."
"First two of EUI64: 92"
"First two of EUI64 as Hex: 92"
"First two of EUI64 (Binary): 1011100"
"First binary bit: 1"
"Attempt at changing the 1st binary to a 0."
"Third binary in sequence change attempt: 1"
"Converted number after conversion: 1011100"

For those who are interested in running the code: https://jsfiddle.net/对于那些对运行代码感兴趣的人: https://jsfiddle.net/

According to eui64 definition, it seems to me that you only have to remove the FFFE part (slice 6,10) to convert back to a mac address.根据 eui64 定义,在我看来,您只需删除FFFE部分(切片 6,10)即可转换回 mac 地址。

eui64 = "92152BFFFEE49B60"
macaddress = eui64.slice(0,6) + eui64.slice(10,14)
=> "92152BE49B"
lastByte = eui64.slice(14,16)
=> "60"
flip7thBit = parseInt("0x"+lastByte) ^ 128
=> "E0"
macaddressCompleted = macaddress + flip7thBit.toString(16).toUpperCase();
=> "92152BE49BE0"

^ 128 means xor 128 , it effectively flip the 7th bit. ^ 128表示xor 128 ,它有效地翻转第 7 位。 (2 power 7 = 128) (2 次方 7 = 128)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM