在javascript中从字节中获取两个半字节的最佳方法? [英] Best way to get two nibbles out of a byte in javascript?
本文介绍了在javascript中从字节中获取两个半字节的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用javascript解析一个二进制文件,该文件每个字节存储两个信息,每个半字节存储一个信息.值当然是0-16和0-16.
I'm parsing a binary file in javascript that is storing two pieces of information per byte, one per nibble. The values are, of course, 0-16 and 0-16.
在文件格式的所有其他部分,每个字节代表一条信息,因此我一直在使用以下方法成功获取所需的数字值:
In all other parts of the file format, each byte represents one piece of information, so I have been using the following to successfully get the number values I need:
var num = str.charCodeAt(0) & 0xFF;
但是我一直试图找出如何从单字节字符"str"中获取第一个半字节的0-16值,以及第二个半字节的相同值.
But I'm stuck at trying to figure out how to get the 0-16 value of the first nibble, and the same for the 2nd nibble from my single byte character "str".
对此表示感谢.
推荐答案
var num = str.charCodeAt(0) & 0xFF;
var nibble1 = num & 0xF;
var nibble2 = num >> 4;
这篇关于在javascript中从字节中获取两个半字节的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文