How to convert a 4-byte hexadecimal sequence in a little-endian architecture into decimal – Today I Learned… in Code (2024)

Here is my technique for solving the problem of converting a 4-byte hexadecimal sequence in a little-endian architecture into decimal.

This may seem rather niche but it was a surprisingly large part of Week 5 in my CS271 class. The class’s materials and extra help I found around the web seemed to go off on tangents that were interesting but unrelated to solving this kind of problem quickly, and all I really wanted was a simple step-by-step guide I could use on exams.

Therefore, I am sharing my simple 5-step technique for converting hex to decimal in a little-endian architecture here! Hope someone else finds it helpful.

Example problem:

The four-byte sequence 0x86 0x65 0x53 0x82 stored in consecutive memory cells in a little-endian architecture represents ___________ (decimal) when interpreted as a 32-bit signed integer.

From reading this, we know:

  • it’s little-endian, so we are going to reverse the order of the bits
  • our result will be signed

Step 1: Reverse the bytes

Take the bits in blocks of two and work right to left.

0x86 0x65 0x53 0x82 becomes 0x82536586

Step 2: Look at the most significant bit and determine if there will be a negative result

0x82536586 <-- that's this dude in red here

The most significant bit here contains an 8.

We know that in hex a most-significant bit of 7 or more means we are looking at a negative number. (If this were a positive number, ie: the most significant bit is between 0 and 6 inclusive, then skip ahead to Step 4.)

Step 3: Since we are working with a negative number, flip the bits (subtract our hex sequence from FFFFFFFF) and add 1

 FFFFFFFF- 82536586 7DAC9A79

Add one to the result:

7DAC9A79 +17DAC9A7A

The result is the hex sequence we will use for the next step.

7DAC9A7A

Step 4: Multiply each term by 16 raised to a power

To convert a hex value into a decimal value, we multiply each “position” in the hex sequence by 16 raised to a power. Working from right to left, we know that furthest-right position is 16^0 (so, just a 1). The second-from-right position is 16^1 (so, just a 16). The third-from-right position is 16^2, and so on.

Recall that A = 10, B = 11, C = 12, D = 13, E = 14, and F = 15 when working in hex.

(7*16^7)+(D*16^6)+(A*16^5)+(C*16^4)+(9*16^3)+(A*16^2)+(7*16^1)+A

The result of this long sequence is:

2108463738

Remember that negative sign? Now’s a good time to stick it on.

-2108463738

And there you have it: the result is-2108463738.

Some final notes

  • Be sure to observe whether the problem expects a signed decimal result or an unsigned decimal result. If the problem is asking for unsigned, you can skip the FFFFFFFF subtraction step entirely, even if the most significant bit is 7 or higher.
  • Remember that when working with a signed hexadecimal number, you look at the most significant bit to determine if it’s negative or positive.
    • 0-7 = positive
    • 8-F = negative
  • If you had to do the flipping step, don’t forget to put that negative sign onto your final answer!
How to convert a 4-byte hexadecimal sequence in a little-endian architecture into decimal – Today I Learned… in Code (2024)
Top Articles
Referring Friends and Referral Bonuses — Pokémon GO Help Center
Bundespolizei - Homepage - Tips: How to protect yourself from pickpockets
Using GPT for translation: How to get the best outcomes
Sandrail Options and Accessories
Don Wallence Auto Sales Vehicles
Chase Bank Operating Hours
Chelsea player who left on a free is now worth more than Palmer & Caicedo
Find All Subdomains
Practical Magic 123Movies
The Potter Enterprise from Coudersport, Pennsylvania
Obituaries
Aries Auhsd
4156303136
Superhot Unblocked Games
Nioh 2: Divine Gear [Hands-on Experience]
Cooking Fever Wiki
U/Apprenhensive_You8924
Northern Whooping Crane Festival highlights conservation and collaboration in Fort Smith, N.W.T. | CBC News
The best firm mattress 2024, approved by sleep experts
Promiseb Discontinued
Iroquois Amphitheater Louisville Ky Seating Chart
Mc Donald's Bruck - Fast-Food-Restaurant
Village
Suspiciouswetspot
Jesus Revolution Showtimes Near Regal Stonecrest
Saxies Lake Worth
Miles City Montana Craigslist
Vera Bradley Factory Outlet Sunbury Products
Soiza Grass
Panchang 2022 Usa
Teenbeautyfitness
Ket2 Schedule
拿到绿卡后一亩三分地
Mistress Elizabeth Nyc
Greater Keene Men's Softball
Geology - Grand Canyon National Park (U.S. National Park Service)
Letter of Credit: What It Is, Examples, and How One Is Used
Lovely Nails Prices (2024) – Salon Rates
Craigslist Boats Dallas
The best specialist spirits store | Spirituosengalerie Stuttgart
Cl Bellingham
Panolian Batesville Ms Obituaries 2022
Citroen | Skąd pobrać program do lexia diagbox?
Child care centers take steps to avoid COVID-19 shutdowns; some require masks for kids
Jammiah Broomfield Ig
Kjccc Sports
Ratchet And Clank Tools Of Destruction Rpcs3 Freeze
Craigslist Sparta Nj
Billings City Landfill Hours
Noelleleyva Leaks
라이키 유출
King Fields Mortuary
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 5618

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.