How to Format an Integer to a Two Digit Hex

How can I format an integer to a two digit hex?

You can use string formatting for this purpose:

>>> "0x{:02x}".format(13)
'0x0d'

>>> "0x{:02x}".format(131)
'0x83'

Edit: Your code suggests that you are trying to convert a string to a hexstring representation. There is a much easier way to do this (Python2.x):

>>> "abcd".encode("hex")
'61626364'

An alternative (that also works in Python 3.x) is the function binascii.hexlify().

Formatting hex to be printed with 2 digits?

You can use string formatting for this purpose:

>>> "0x{:02x}".format(13)
'0x0d'

More detailed examples here:

How can I format an integer to a two digit hex?

Integer to two digits hex in Java

String.format("%02X", value);

If you use X instead of x as suggested by aristar, then you don't need to use .toUpperCase().

Integer to two digit Hexadecimal in C/C++

Simple

int val = 10;
int msb = val/256;
int lsb = val%256;
hex[3] = msb;
hex[2] = lsb;

Doesn't work for negative integers or integers with more that 16 significant bits. But I guess you don't care about that.

There's a hint in your question that you have the common newbie misunderstanding that there's some fundamental difference between hexadecimal integers and decimal integers. There's isn't, all integers are of the same kind (which is normally binary). Decimal and hexadecimal are just different ways of printing integers not different ways the C++ language represents integers.

How to separate a two digit hexadecimal number into its digits?

if you mean 'how can I get the upper and lower nibbles of a byte into two variables'

 char x = 0x4e;
int low = x & 0x0f;
int high = (x & 0xf0) >> 4;

Python - How do I format a hexadecimal number in uppercase and two digits?

TL;DR (Python 3.6+)

print(f'Number {x:02x}')

This is explained in the Format specification Mini-Language.

To get uppercase letters:

'x' Hex format. Outputs the number in base 16, using lowercase letters for the digits above 9.

'X' Hex format. Outputs the number in base 16, using uppercase letters for the digits above 9.

To get 2 digits:

width is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content.

When no explicit alignment is given, preceding the width field by a zero ('0') character enables sign-aware zero-padding for numeric types. This is equivalent to a fill character of '0' with an alignment type of '='.

So, you either need to set the width to 2, the fill to 0, and the alignment to =, or, more simply, just use the special 0 prefix before the width.

So:

print('Number '+'{0:02X}'.format(int(x)))

While we're at it, this is pretty silly code.

First, x is already an int, so why call int on it?

print('Number '+'{0:02X}'.format(x))

Meanwhile, if the only thing you're putting in a format string is a single format specifier, you don't need str.format, just format:

print('Number ' + format(x, '02X'))

Or, alternatively, the whole point of str.format is that you can throw multiple things into one format string:

print('Number {:02X}'.format(x))

If you are using >= Python 3.6, you can use elegant f-strings:

print(f'Number {x:02x}')

How to convert HEX into two digits format?

Instead of using the s format specifier for String.format, you can use the x specifier which formats an integer as hexadecimal:

String.format("%02x", lightOnHours)

Or X if you prefer uppercase hexadecimal:

String.format("%02X", lightOnHours)

How to print unsigned char as 2-digit hex value in C?

As far as I know, the Keil C compiler doesn't fully conform to the C standard. If so, it's likely that it doesn't quite follow the standard promotion rules for things like passing char values to variadic functions; on an 8-bit CPU, there are performance advantages in not automatically expanding 8-bit values to 16 bits or more.

As a workaround, you can explicitly truncate the high-order bits before passing the argument to printf. Try this:

#include <stdio.h>

int main(void) {
unsigned char status = 0x00;
status |= 0xC0;

printf("status = 0x%02X\n", (unsigned int)(status & 0xFF));
return 0;
}

Doing a bitwise "and" with 0xFF clears all but the bottom 8 bits; casting to unsigned int shouldn't be necessary, but it guarantees that the argument is actually of the type expected by printf with a "%02X" format.

You should also consult your implementation's documentation regarding any non-standard behavior for type promotions and printf.



Related Topics



Leave a reply



Submit