Hi,
I need to know the byte which a number is made of. For example, let's suppose I have
i = 3000
I would like to know if there is a way to get the single byte of the number (0x0B and 0xB8).
Thanks in advance,
Daniele
15 5607 bartonc 6,596
Recognized Expert Expert
Hi,
I need to know the byte which a number is made of. For example, let's suppose I have
i = 3000
I would like to know if there is a way to get the single byte of the number (0x0B and 0xB8).
Thanks in advance,
Daniele
-
>>> aHex = hex(3000) # call built-in function to convert to hex
-
>>> print aHex
-
0xbb8
-
>>> bytes = aHex[2:].zfill(4) # slice off the '0x'
-
>>> print bytes
-
0bb8
-
>>> highByte = '0x%s' % bytes[0:2] #reformat with '0x' added to just the high byte
-
>>> print highByte
-
0x0b
-
>>>
Let's see if you can get the low byte. We're here to help.
dshimer 136
Recognized Expert New Member
Just a curiosity but is there any built-in that would convert to binary? For example:
3000='101110111 000'
First of all thanks,
the lowbyte comes from
lowByte = '0x%s' % bytes[2:4]
I've found also another solution: -
# mod edit # added for completeness #
-
import struct
-
################################
-
def getByteLength(self, length):
-
temp = struct.pack('!h', length)
-
byteList = [firstLength, secondLength] = struct.unpack('BB', temp)
-
return list(byteList)
-
to obtain a list in which the elements are the needed bytes
bartonc 6,596
Recognized Expert Expert
Just a curiosity but is there any built-in that would convert to binary? For example:
3000='101110111 000'
While it's true that
>>> int('101', 2)
5
>>>
There is no bit or byte type to call for conversion to base 2.
bartonc 6,596
Recognized Expert Expert
While it's true that
>>> int('101', 2)
5
>>>
There is no bit or byte type to call for conversion to base 2.
How 'bout this little goodie: - def Byte2Bits(value):
-
# Create a list of bits to convert big endian wise
-
data = []
-
outStr = ""
-
for i in range(8):
-
value, rem = divmod(value, 2)
-
data.insert(0, rem)
-
-
for pos, i in enumerate(data):
-
if not (pos % 4):
-
outStr += " "
-
outStr += str(i)
-
return outStr
-
-
-
def Base2Repr(value):
-
# Create a list of bytes to convert big endian wise
-
data = []
-
outStr = ""
-
while value:
-
value, rem = divmod(value, 256)
-
data.insert(0, rem)
-
for i in data:
-
outStr += Byte2Bits(i)
-
return outStr[1:]
-
-
-
class bits(object):
-
def __init__(self, value):
-
self.value = value
-
-
def __repr__(self):
-
return Base2Repr(self.value)
-
-
-
-
if __name__ == "__main__":
-
b = bits(0xf5a5)
-
print b
-
1111 0101 1010 0101
bvdet 2,851
Recognized Expert Moderator Specialist
Just a curiosity but is there any built-in that would convert to binary? For example:
3000='101110111 000'
I don't think so, but I made this up: - def ConvDecToBaseVar(num, base):
-
if base > 10 or base < 2:
-
raise ValueError, 'The base number must be between 2 and 10.'
-
if num == 0: return 0
-
ans = ''
-
while num != 0:
-
num, rem = divmod(num, base)
-
ans = str(rem)+ans
-
return int(ans)
-
-
'''
-
>>> ConvDecToBaseVar(3000,2)
-
101110111000L
-
>>>
-
'''
dshimer 136
Recognized Expert New Member
I was just curious about a built-in but that is absolutely Beautiful!
bartonc 6,596
Recognized Expert Expert
I was just curious about a built-in but that is absolutely Beautiful!
It's beautiful, all right (ain't BV good). But what about nibble formatting?
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Kristian Nybo |
last post by:
Hi,
I'm writing a simple image file exporter as part of a school project. To
implement my image format of choice I need to work with big-endian
bytes, where 'byte' of course means '8 bits', not 'sizeof(char)'. It
seems that I could use bitset<8> to represent a byte in my code --- if
you have a better suggestion, I welcome it --- but that still leaves me
with the question of how to write those bitsets to an image file as
big-endian bytes...
|
by: becte |
last post by:
I need to use three bytes to store four 6-bit integers (4 * 6 = 3 * 8)
like this
11111122|22223333|33444444
Suppose the input is, int c1, c2, c3, c4, range 0 .. 2^6 -1
and the output is int o1,o2,o3, range 0 .. 2^8-1
How to do this in a clever way?
(The 6 bits integers represent characters in range A-Z and 0-9)
|
by: Lorenzo J. Lucchini |
last post by:
My code contains this declaration:
: typedef union {
: word Word;
: struct {
: byte Low;
: byte High;
: } Bytes;
: } reg;
|
by: KraftDiner |
last post by:
I was under the assumption that everything in python was a refrence...
so if I code this:
lst =
for i in lst:
if i==2:
i = 4
print lst
I though the contents of lst would be modified.. (After reading that
|
by: Andy Sy |
last post by:
Hi Dan,
I find that when doing bit-twiddling in pure Python,
fixed-width integer support is an extremely handy
capability to have in Python regardless of what
the apologists (for its absence) say.
I added some stuff to fixedint.py to make
| |
by: Alex Buell |
last post by:
I just wrote the following and have a question: Why does my code gets
it wrong with the class Simple? See last show_size<Simple> function call
in main () as below:
#include <iostream>
#include <string>
#include <limits>
class Simple
{
|
by: Michael Yanowitz |
last post by:
Is it possible to have a static variable in Python -
a local variable in a function that retains its value.
For example, suppose I have:
def set_bit (bit_index, bit_value):
static bits =
bits = bit_value
print "\tBit Array:"
|
by: Cindy |
last post by:
I am struggling over a simple way to shift multi bytes for certain
bits. Hope someone can help. For example, I open a memory space for 10
bytes:
unsigned char *pData = new unsigned char;
then store some value into pData which occupies pData and pData.
Now I want to shift these two bytes to the left for 9 bits and still
store them in pData. The data now should span from the bit0 in
pData to bit1 in pData.
|
by: Hahnemann |
last post by:
Does anybody know the answer to the following? An unsigned short is 2
bytes long. Why is the following file created as 4 bytes instead of
2?
file = fopen("data.bin", "wb");
if (file != NULL)
{
unsigned short s = 65535;
printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
short)); // 2 bytes - OK
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |