Damir
Damir

Reputation: 56199

Get byte from number problem

I have test array of hex(radix 16) numbers

var numbers = ["01","02","a1"]; 

and I convert them to int(radix 10) by using

var num = parseInt(temp.join(''), 16);

where temp is an number in the array of numbers.

I extract byte like

( num >> (8*index_byte) & 0xFF) 

but I dont get value like "03", problem is that I get just 3. How to get like "01" not 1 or "02" not 2 ?

Upvotes: 0

Views: 462

Answers (3)

Jonas Elfström
Jonas Elfström

Reputation: 31428

You could use String.js from jsxt.

var fnumber = '%02d'.sprintf( num>>(8*index_byte) & 0xFF );

Upvotes: 0

rmflow
rmflow

Reputation: 4765

Something like:

num = 1;
if (String(num).length < 2)
{
    num = "0" + num;
}

Upvotes: 1

Shadow Wizard
Shadow Wizard

Reputation: 66389

03 is 3 by all means.

Your only option is padding the number yourself:

var byte = "0" + (num >> (8 * index_byte) & 0xFF);

Upvotes: 1

Related Questions