seoulforest
seoulforest

Reputation: 35

How can I change decimal integer to hexadecimal integer?

I have to change decimal integer into hexadecimal integer, but hex() returns string, even I have to get hexadecimal integer.

For example, if I have integer

a = 65536     (16^4)

then I have to make a1, a2, a3, a4
a1 = 0x00
a2 = 0x01
a3 = 0x00
a4 = 0x00

due to send RS485 serial communication.

What can I do for it?

Upvotes: 0

Views: 88

Answers (2)

Ren
Ren

Reputation: 155

First know that in a computer, data is stored as binary, whatever format you display it in, It's always the same.

so for a computer 0x12 == 0b00010010 == 18

to get the values of thoses digits tho, what you can use is the modulo:

a = 65536
a1 = int(a/65536 % 16)
a2 = int(a/4096 % 16)
a3 = int(a/256 % 16)
a4 = int(a/16 % 16)

Upvotes: 0

Jesper
Jesper

Reputation: 1076

there is no "hexadecimal interger" because it's just a different way to show the same number. if a serial device needs to get a hex number you can send a string or send the interger and parse that interger on the other end.

Upvotes: 1

Related Questions