Last active
September 3, 2019 16:58
-
-
Save castaneai/80e5ab5111ba7ac81c09 to your computer and use it in GitHub Desktop.
converting HexString <-> Uint8Array with javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* "1A 2B 3C" のような16進数表記文字列をバイト配列に変換する | |
*/ | |
$scope.hexs2bytes = function(hexs) { | |
return hexs.split(' ').map(function(h) { return parseInt(h, 16) }); | |
}; | |
/** | |
* バイト配列を"1A 2B 3C"のような16進数表記文字列に変換する | |
*/ | |
$scope.bytes2hexs = function(bytes) { | |
return bytes.map(function(b) { var s = b.toString(16); return b < 0x10 ? '0'+s : s; }).join(' ').toUpperCase(); | |
} | |
/** | |
* 整数(int8, int16, int32, int64)をバイト配列に変換する | |
*/ | |
$scope.int2bytes = function(int) { | |
hex = int.toString(16) | |
if (hex.length % 2 != 0) { | |
hex = '0' + hex; | |
} | |
var result = []; | |
for (var i = 0; i < hex.length; i+= 2) { | |
result.push(parseInt(hex.substr(i, 2), 16)); | |
} | |
result.reverse(); | |
return result; | |
} | |
/** | |
* バイト配列を整数に変換する | |
*/ | |
$scope.bytes2int = function(bytes) { | |
var bytes_r = bytes.concat().reverse(); | |
return parseInt(bytes_r.reduce(function(acc, cur) { | |
if (cur == 0) return acc; | |
var s = cur.toString(16); | |
return acc + (cur < 0x10 ? '0'+s : s); | |
}), 16); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment