in Education by
I need to encode a signed integer as hexadecimal using via the two's complement notation. For example I would like to convert e.g. -24375 to 0xffffa0c9. So far I have been working on the following lines: parseInt(-24375).toString(2) > "-101111100110111" This matches what Wolfram Alpha displays, but I am not sure how to get to the signed 24bit int representation of the number (ffffa0c9). I've worked out how to take the unsigned binary number and represent this as two's complement: ~ parseInt("101111100110111", 2) + 1 > -23475 but I am not sure get the binary representation of this number to convert to hex. Any ideas? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
To create two's-compliment numbers of fixed size I've created factory method: function createToInt(size) { if (size < 2) { throw new Error('Minimum size is 2'); } else if (size > 64) { throw new Error('Maximum size is 64'); } // Determine value range const maxValue = (1 << (size - 1)) - 1; const minValue = -maxValue - 1; return (value) => { if (value > maxValue || value < minValue) { throw new Error(`Int${size} overflow`); } if (value < 0) { return (1 << size) + value; } else { return value; } }; } Now, to solve your question you can create functions toInt8, toInt16, toInt32, etc. And use it to convert JS numbers to two's compliment. Example with int8: const toInt8 = createToInt(8); '0x' + toInt8(-128).toString(16); // -> 0x80 '0x' + toInt8(127).toString(16); // -> 0x7f '0x' + toInt8(-1).toString(16); // -> 0xff // Values less then 16 should be padded '0x' + toInt8(10).toString(16).padStart(2, '0); // -> 0x0a

Related questions

0 votes
    I am trying to convert string into integer using jquery but it throwing NumberFormatException. How to fix it ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I am getting Facebook users' friends birthday back from Facebook in the following format: 10/07/1967 or ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    I am getting Facebook users' friends birthday back from Facebook in the following format: 10/07/1967 or ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    11. Express the decimal format of the signed binary number (10010)2 O a) 2 b) 12 O c)-12 Co O d)-2 Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    How do I convert a hex string to an int in Python? I may have it as "0xffff" or just "ffff". Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I want to encode a 1-D numpy array: x = array([1,0,3]) As a 2-D 1-hot array y = array([ ... some faster technique other than looping. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I receive the latitude and longitude from GPS with this format: Latitude : 78°55'44.29458"N I need ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    each circular outward direction. Suggest her wh F.Soleil 1. Convert (655) in binary number: 2. Convert (487) ... 3. Compute 10001-1111 Select the correct answer from above options...
asked Dec 28, 2021 in Education by JackTerrance
0 votes
    A convert the following Decimal numbers into Binary numbers: a. 68 b. 987 c. 657 I want step by step solutions… Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    Convert the following decimal numbers into binary numbers. a) (58)10 Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    Write a program to convert a decimal number into binary number using function Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    2. Convert the following binary numbers into decimal: (1101) Select the correct answer from above options...
asked Dec 15, 2021 in Education by JackTerrance
...