MCPcopy Create free account
hub / github.com/bitpay/bitcore-message / hexWrite

Function hexWrite

bitcore-message.js:1075–1100  ·  view source on GitHub ↗
(buf, string, offset, length)

Source from the content-addressed store, hash-verified

1073}
1074
1075function hexWrite (buf, string, offset, length) {
1076 offset = Number(offset) || 0
1077 var remaining = buf.length - offset
1078 if (!length) {
1079 length = remaining
1080 } else {
1081 length = Number(length)
1082 if (length > remaining) {
1083 length = remaining
1084 }
1085 }
1086
1087 // must be an even number of digits
1088 var strLen = string.length
1089 if (strLen % 2 !== 0) throw new TypeError('Invalid hex string')
1090
1091 if (length > strLen / 2) {
1092 length = strLen / 2
1093 }
1094 for (var i = 0; i < length; ++i) {
1095 var parsed = parseInt(string.substr(i * 2, 2), 16)
1096 if (isNaN(parsed)) return i
1097 buf[offset + i] = parsed
1098 }
1099 return i
1100}
1101
1102function utf8Write (buf, string, offset, length) {
1103 return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)

Callers 1

bitcore-message.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected