An example OutputId is 0x0c78e998f5177834ecb3bae1596d5056af76e487386eecb19727465b4be86a790000
:
Hex-prefix |
TransactionId |
OutputIndex |
OutputIndex Decimal |
0x |
0c78e998f5177834ecb3bae1596d5056af76e487386eecb19727465b4be86a79 |
0000 |
0 |
0x |
0c78e998f5177834ecb3bae1596d5056af76e487386eecb19727465b4be86a79 |
0100 |
1 |
0x |
0c78e998f5177834ecb3bae1596d5056af76e487386eecb19727465b4be86a79 |
0200 |
2 |
0x |
0c78e998f5177834ecb3bae1596d5056af76e487386eecb19727465b4be86a79 |
2a00 |
42 |
0x |
0c78e998f5177834ecb3bae1596d5056af76e487386eecb19727465b4be86a79 |
8000 |
128 |
Code examples to hex encoded the OutputIndex:
Rust:
fn main() {
fn output_index_to_hex(index: u16) -> String {
let bytes = u16::to_le_bytes(index);
bytes
.iter()
.map(|b| format!("{b:02x}"))
.collect::<Vec<String>>()
.join("")
}
let output_index_hex = output_index_to_hex(0);
println!("{output_index_hex}");
}
Python:
def output_index_to_hex(index: int) -> str:
return index.to_bytes(2, "little").hex()
print(output_index_to_hex(0))
JS:
// Works in browser and Node.js
function outputIndexToHex(number) {
let buf = new ArrayBuffer(2);
const view = new DataView(buf);
view.setUint16(0, number, true)
return [...new Uint8Array(buf)].map(x => x.toString(16).padStart(2, '0'))
.join('')
}
console.log(outputIndexToHex(0))
// Cleaner version, but Node.js only
function outputIndexToHexNodeJs(number) {
let buf = Buffer.allocUnsafe(2);
buf.writeInt16LE(number)
return buf.toString('hex')
}
console.log(outputIndexToHexNodeJs(0))
The reverse operation in JS:
function hexOutputIndexToInt(number) {
const numberString = number.toString();
let chunks = [];
for (let i = 0, charsLength = numberString.length; i < charsLength; i += 2) {
chunks.push(numberString.substring(i, i + 2));
}
const separated = chunks.map(n => parseInt(n, 16))
const buf = Uint8Array.from(separated).buffer;
const view = new DataView(buf);
return view.getUint16(0, true)
}