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
Tworzenie dysku naprawy systemu - Pomoc techniczna firmy Microsoft
Will I notice a difference going from 16 GB of RAM to 32?
Warren Ohio Craigslist
Alan Miller Jewelers Oregon Ohio
From Algeria to Uzbekistan-These Are the Top Baby Names Around the World
Activities and Experiments to Explore Photosynthesis in the Classroom - Project Learning Tree
Bucks County Job Requisitions
Delectable Birthday Dyes
Craigslist Phoenix Cars By Owner Only
Does Publix Have Sephora Gift Cards
Cvb Location Code Lookup
Immortal Ink Waxahachie
Simplify: r^4+r^3-7r^2-r+6=0 Tiger Algebra Solver
Comics Valley In Hindi
Dumb Money, la recensione: Paul Dano e quel film biografico sul caso GameStop
Zalog Forum
Airrack hiring Associate Producer in Los Angeles, CA | LinkedIn
Teacup Yorkie For Sale Up To $400 In South Carolina
Juicy Deal D-Art
O'Reilly Auto Parts - Mathis, TX - Nextdoor
The EyeDoctors Optometrists, 1835 NW Topeka Blvd, Topeka, KS 66608, US - MapQuest
Craigslist Northfield Vt
Buying Cars from Craigslist: Tips for a Safe and Smart Purchase
Riversweeps Admin Login
Violent Night Showtimes Near Amc Dine-In Menlo Park 12
Manuela Qm Only
Tomb Of The Mask Unblocked Games World
Vivification Harry Potter
Earthy Fuel Crossword
What Is The Lineup For Nascar Race Today
Scat Ladyboy
Unm Hsc Zoom
Autopsy, Grave Rating, and Corpse Guide in Graveyard Keeper
Orange Pill 44 291
Iban's staff
Manatee County Recorder Of Deeds
When His Eyes Opened Chapter 2048
How are you feeling? Vocabulary & expressions to answer this common question!
Busch Gardens Wait Times
Letter of Credit: What It Is, Examples, and How One Is Used
Florida Lottery Claim Appointment
boston furniture "patio" - craigslist
Juiced Banned Ad
Coffee County Tag Office Douglas Ga
Reilly Auto Parts Store Hours
Chr Pop Pulse
Rite Aid | Employee Benefits | Login / Register | Benefits Account Manager
Shiftselect Carolinas
Inside the Bestselling Medical Mystery 'Hidden Valley Road'
Law Students
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.