Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8861793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:34:16+00:00 2026-06-14T15:34:16+00:00

I have a signed value given as a hex number, by example 0xffeb and

  • 0

I have a signed value given as a hex number, by example 0xffeb and want convert it into -21 as a “normal” Javascript integer.

I have written some code so far:

function toBinary(a) { //: String
    var r = '';
    var binCounter = 0;
    while (a > 0) {
        r = a%2 + r;
        a = Math.floor(a/2);
    }
    return r;
}

function twoscompl(a) { //: int
    var l = toBinaryFill(a).length;
    var msb = a >>> (l-1);

    if (msb == 0) {
        return a;
    }

    a = a-1;
    var str = toBinary(a);
    var nstr = '';
    for (var i = 0; i < str.length; i++) {
        nstr += str.charAt(i) == '1' ? '0' : '1';
    }
    return (-1)*parseInt(nstr);
}

The problem is, that my function returns 1 as MSB for both numbers because only at the MSB of the binary representation “string” is looked. And for this case both numbers are 1:

-21 => 0xffeb => 1111 1111 1110 1011
 21 => 0x15   =>              1 0101

Have you any idea to implement this more efficient and nicer?

Greetings,
mythbu

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:34:17+00:00Added an answer on June 14, 2026 at 3:34 pm

    Use parseInt() to convert (which just accepts your hex string):

    parseInt(a);
    

    Then use a mask to figure out if the MSB is set:

    a & 0x8000
    

    If that returns a nonzero value, you know it is negative.

    To wrap it all up:

    a = "0xffeb";
    a = parseInt(a, 16);
    if ((a & 0x8000) > 0) {
       a = a - 0x10000;
    }
    

    Note that this only works for 16-bit integers (short in C). If you have a 32-bit integer, you’ll need a different mask and subtraction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a signed class library that I want to create assemblies for via
So this sensor I have returns a signed value between -500-500 by returning two
How to convert the signed DWORD to Unsigned DWORD ? I have one method
I currently have a signed integer set in a MyISAM table, The query is
I have a signed value e.g. -7368817 when I cast it to byte it
OK, so say I have a signed char with value -103: char s_char =
I have signed my applet and it is in jar. I have added the
I have signed up(paid) for Google site search. They have me a url of
I have signed every jar required by the application but when trying to launch,
I have signed my android application with keytool but don't know what to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.