0

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 ?

3 Answers 3

1

Something like:

num = 1;
if (String(num).length < 2)
{
    num = "0" + num;
}
Sign up to request clarification or add additional context in comments.

1 Comment

var num = 1; num = num.toString().length < 2 ? "0" + num : num; for simple comparisons you can use ternary (it`s said that it will produce faster code, not very sure, give it a try)
1

03 is 3 by all means.

Your only option is padding the number yourself:

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

Comments

0

You could use String.js from jsxt.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.