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 8744715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:53:09+00:00 2026-06-13T11:53:09+00:00

Working my way through some some Python code, I’m noticing there are a number

  • 0

Working my way through some some Python code, I’m noticing there are a number of different representations for hexadecimal values. For example, if I choose a number like so:

xx = '\x03\xff'

Then the following command (a version of which I’m using to convert little endian to big endian)

yy = hex(struct.unpack('>H', xx)[0])

will return:

'0x3ff'

However, this command

zz = xx.encode('hex')

will return:

'03ff'

Finally, printing just the value out will return this

'\x03\xff'

From the looks of it there are three different types of hex then.

  1. '\xFF'
  2. '0xFF'
  3. 'FF'

What’s the difference?

Bonus points if someone could suggest a better way of converting a little endian to a big endian number. The above method for yy won’t work for numbers larger than two bytes obstinately enough and I’m working with some hex strings that are 16 bytes long (including values that don’t correspond to an ascii/integer value)

  • 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-13T11:53:10+00:00Added an answer on June 13, 2026 at 11:53 am

    Anything using \x is a string escape code, which happens to use hex notation; other escape codes include \n for newlines, \' for a literal quote, etc. A python string is a sequence of bytes and you can specify literal values outside the ASCII printable range using such characters. When Python echoes a string value back at you in the interpreter, or you print the result of a repr() call on a string, Python will use such escapes to represent any byte that cannot be printed as a ASCII character instead:

    >>> chr(65)
    'A'
    >>> chr(11)
    '\x0b'
    

    The hex() function returns a very specific string representation, as does .encode('hex') with the difference being that the former includes the 0x prefix. There are two more methods to produce such string representations; using the '%x' and '%X' string formatters, which use lowercase or uppercase letters for the representation.

    >>> hex(11)
    '0xb'
    >>> '\x0b'.encode('hex')
    '0b'
    >>> '%x' % (11,)
    'b'
    >>> '%X' % (11,)
    'B'
    

    These are all string representations though (a series of ASCII characters), and have the same relation to the original data as str(number) is to integer data; you have changed the type and are further away from your goal of changing the byte ordering.

    Changing a piece of binary information from little-ending to big-endian requires that you know the size of that piece of information. If all you have are short integers, then you need to flip every two bytes around, but if you have normal (long) integers, then you have 4 bytes per value and you need to reverse each 4 bytes.

    Using the struct module is, I think, an excellent approach because you have to specify the value type. The following would interpret xx as a big-endian unsigned short int, then pack it back to a binary representation as a little-endian unsigned short int:

    >>> import struct
    >>> xx = '\x03\xff'
    >>> struct.pack('<H', *struct.unpack('>H', xx))
    '\xff\x03'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working my way through some Python tutorials one of the things that
Im working my way through some C++ code and came across the following void
I'm new to Google Apps Script. I'm working my way through some simple example
I'm working my way through some ASP.NET MVC reading and I have a web
I'm still working my way through some pretty basic Actionscript programming (in Flex), and
I'm working my way through some unit testing with AndroidTestCase and was wondering, if
While working my way through the Android tutorials, I came across something I don't
I'm working my way through "Learning jQuery" (Third Edition). In Chapter 4: "Manipulating the
I'm working my way through displaying various data elements from parent & child data,
I'm working my way through Agile Web Development with Rails and am running into

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.