How to ignore the signed bit?

C

chanma

I have an num -569360386, and turn it into hex format.
I use toString(16),I get -21efc002.

But how can get 0xDE103FFE,which is to ignore the highest bit as the
signed bit?
 
C

chanma

Then is there any way to Get 0xDE103FFE but not -21efc002???

Must I use my own code to get translate it?
 
E

Evertjan.

wrote on 22 jan 2006 in comp.lang.javascript:
Then is there any way to Get 0xDE103FFE but not -21efc002???

Please quote what you are replying to.

This is not email.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at the
top of the article, then click on the "Reply" at the bottom of the article
headers.
Must I use my own code to get translate it?

0xDE103FFE is not an output in javascript

The solution seems simple as each figure is the 16-complement:


n = '-21efc002';

n = n.toUpperCase();
if (n.charAt(0)=='-'){
r = 'FEDBCA9876543210';
m = '';
for (i=1;i<n.length;i++){
g = n.charCodeAt(i)-48;
if (g>9)g=g-7;
m += r.charAt(g);
};
n = m;
};
n = '0x' + n

document.write(n); // 0xDE103FFD
 
E

Evertjan.

Evertjan. wrote on 22 jan 2006 in comp.lang.javascript:
0xDE103FFE is not an output in javascript

The solution seems simple as each figure is the 16-complement:


n = '-21efc002';

n = n.toUpperCase();
if (n.charAt(0)=='-'){
r = 'FEDBCA9876543210';
m = '';
for (i=1;i<n.length;i++){
g = n.charCodeAt(i)-48;
if (g>9)g=g-7;
m += r.charAt(g);
};
n = m;
};
n = '0x' + n

document.write(n); // 0xDE103FFD

I leave the obvious difference of 1 to your coding ability.
 
C

chanma

Evertjan. said:
Evertjan. wrote on 22 jan 2006 in comp.lang.javascript:

But in such code below:

var x=0xf0000000;
alert(x);

it can output :4026531840 !

So how the JS engine interprets it as unsigend numbers??
 
L

Lasse Reichstein Nielsen

I have an num -569360386, and turn it into hex format.
I use toString(16),I get -21efc002.

But how can get 0xDE103FFE,which is to ignore the highest bit as the
signed bit?

Since numbers in Javascript are 64-bit IEEE floating point numbers,
the "highest bit" probably isn't what you think it is.
You can convert -12312151251245125125 to hex as well.

What you might want it to treat the number as a 32 bit integer.
Luckily, that is an internal datatype in Javascript, and some
operations (in particular bitwise ones) does convert/truncate the
number to a 32 bit integer before operating on it. However, that
number can still be signed.

try:
function myToHex(num) {
num <<= 0; // truncate to 32 bits;
if (num < 0) { num = num ^ (1<<31); }
return "0x" + num.toString(16);
}

/L
 
J

Jonas Raoni

(e-mail address removed) escreveu:
Then is there any way to Get 0xDE103FFE but not -21efc002???
Must I use my own code to get translate it?

Since
0xDE103FFE = 3725606910 = 11011110000100000011111111111110
And
~0xDE103FFE + 1 = 569360386 = 100001111011111100000000000010

A xor operation (569360386 ^ 0xffffffff) + 1 would solve your problem:
11111111111111111111111111111111 [XOR]
00100001111011111100000000000010
--------------------------------------------------------
11011110000100000011111111111101 [+ 1] =
11011110000100000011111111111110

But due to the range of numbers available to binary operations, it
won't work :)

So you must make a work around like this one:

x = 569360386;
alert(((0x7fffffff ^ x) + 0x80000001).toString(16));

I've tested it just with your example number (569360386), so test a
little for me haha =)
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Sun, 22 Jan 2006 02:47:08 remote, seen in
I have an num -569360386, and turn it into hex format.
I use toString(16),I get -21efc002.

But how can get 0xDE103FFE,which is to ignore the highest bit as the
signed bit?

"0x" + (Math.pow(2, 32) - 569360386).toString(16).toUpperCase()

To accommodate the widest possible range, you may need different code
for positive and negative, or to add leading zeroes, or to form

"0x" + (Math.pow(2, 52) - 569360386).toString(16).toUpperCase()

and select the required number of digits.

If done repeatedly, the power of 2 should be written as a constant :

"0x" + (0x10000000000000 - 569360386).toString(16).toUpperCase()
 
A

alexey

I think following code mach easy :)

function toHexString(_intValue)
{
var intHigh = (_intValue >> 16) & 0xFFFF;
return "0x" + intHigh.toString(16) + (_intValue &
0xFFFF).toString(16);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top