Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hexToByteArray adding length of hex to Array #77

Open
ShamanBlackout opened this issue Jan 3, 2024 · 0 comments
Open

hexToByteArray adding length of hex to Array #77

ShamanBlackout opened this issue Jan 3, 2024 · 0 comments

Comments

@ShamanBlackout
Copy link
Contributor

[src/core/utils/index.ts]

current code

export function hexToByteArray(hexBytes: string) {
  const res = [hexBytes.length / 2]; //The issue
  for (let i = 0; i < hexBytes.length; i += 2) {
    const hexdig = hexBytes.substr(i, 2); //substr is deprecated
    if (hexdig == '') {
      res.push(0);
    } else res.push(parseInt(hexdig, 16));
  }
  return res;
}
  • As you can see the hexBytes.length / 2 will be the first element of the array thus invalidating the valid hex

  • substr is deprecated as well

  • I suggest the following script

export function hexToByteArray(hexBytes: string) {
  let res = [];
  for (let i = 0; i < hexBytes.length; i += 2) {
    const hexdig = hexBytes.substring(i, i+2);
    if (hexdig == '') {
      res.push(0);
    } else res.push(parseInt(hexdig, 16));
  }
  return res;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant