- Related Questions on StackOverflow
- Trying to pack binary bits represented by bytes into a byte in python
I have a string that contains a string of hex values that represent binary bits, so 0x00 = binary 0 and 0x01 = binary 1. How can I pack eight of these hex values into a byte?

For example, the string = '\x01\x01\x00\x01\x00\x01\x00\x01' and from that I want 0b11010101.

The end goal is to extract several bytes worth of binary from a long string.

How can this be done? I've not seen any instance so far of using struct.pack to do this. Any help would be appreciated!

- MIPS: how to convert a hex word in a String to decimal and then Print String
I have been working on this MIPS program and cant seem to find a way how to answer it.

I have a C Prototype of this and the C code works just fine.

I am trying to convert a hex word in a string to its decimal value and the print the String with the hex word showing as decimal.

for example: input isYOUR DAD WILL BE GOOD. output should be YOUR 3501 WILL 190 GOOD.

in MIPS you check for a new line code 10 and it will terminate.

This program will only take UPPERCASE and # and . or spaces.

here is what i have so far.

`.datastr: .space 200newline: .asciiz "\n" # For new Line.textmain:# Load A Stringli $v0, 8la $a0, strli $a1, 200syscall la $t0, str # Load here the string lb $t1, 0($t0) # We do as always, get the first byte pointed by the addresscheckEnd: li $t7, 0 # Set Sum = 0 of Hex wordmove $t6, $t0 # Set K = IwhileLoop2:bge $t1, 65, checkF # If c[i] >= 65 A go to checkFaddi $t0, $t0, 1 # Else I++ ADDRESS OF STRINGlb $t1, 0($t0) # We do as always, get the first byte pointed by the addressbeq $t1, 10, closeProgram # If t1 is new Line go to Close j whileLoop2checkF:bgt $t1, 70, invalidHex # If c[i] > 70 Go to InvalidHexli $s0, 55 # S = 55 sll $t7, $t7, 4 # Shift by 4 or * 16add $t7, $t7, $t2 # Add x = x + c[i]sub $t7, $t7, $s0 # Substract S j whileLoop2 invalidHex: addi $t0, $t0, 1 # add 1 to addresslb $t1, 0($t0) # We do as always, get the first byte pointed by the addressbeq $t1, 10, closeProgram # If t1 is new Line go to Close j whileLoop2closeProgram:li $v0, 10 # We have done, exit the program syscall # DO the Syscall`

So from this How would i replaced the word DAD for example for 3501 and display 3501 instead of DAD in the output.

- Convert hex string to integer
HI I want to get exact integer value from hex string.

My problem is I have an integer value as

`-25`

and its hex String is "E7"but when I convert -25 using

`public static String toHexString(int i) { String hexString = Integer.toHexString(i); if (hexString.length() % 2 != 0) { hexString = "0" + hexString; } return hexString.toUpperCase();}`

the above function It returns "FFFFE7"

and when I convert it to get decimal value which is

`231`

using`int len =Integer.parseInt(Hex,16);`

I get Error "Invalid Int"

but when I convert it to integer value manually as

`int len =Integer.parseInt("E7",16);`

I get result as 231.

so can anyone tell me how can I get exact integer by passing whole hexadecimal string ??

as my hex string is dynamic so i can't fix it .

- Converting A String To Hexadecimal In Java
I am trying to convert a string like "testing123" into hexadecimal form in java. I am currently using BlueJ.

And to convert it back, is it the same thing except backward?

- Python hex string operation: need to preserve the leading zeros
I have two numbers:

a = "00000108"

b = "FFFFF9FF"

I want to compute the bitwise AND of the hex values in the two strings. The result I want is a string:"00000108"

Here is what I have come up with:

`def fixedAND(a, b): ''' The a and b are both strings ''' return ''.join([hex(ord(i) & ord(j))[2:] for i,j in zip(a.decode('hex'), b.decode('hex'))])`

This function returns "0018". It suppresses the leading zeros for each of the two hex digit pairs.

Please note that by viewing our site you agree to our use of cookies (see Privacy for details). You will only see this message once.