Converting binary string to binary [duplicate] - rust

This question already has answers here:
Is there anything in Rust to convert a binary string to an integer?
(3 answers)
Closed 12 months ago.
I have binary strings:
let x = "0b101101111";
let y = "0b111111";
How do I convert the string to binary to be able to find the sum?
this works:
let x = 0b101101111;
let y = 0b111111;
println!("x + y = {:b} ", x + y);
// x + y = 110101110
thanks!

If you know that the string always begins with the constant 0b characters, then you can simply strip those and read the actual digits using from_str_radix.
let x = "0b101101111";
let x_num = i64::from_str_radix(x[2..], 2).unwrap();

Related

Escaping parameters in function calls for Python [duplicate]

This question already has answers here:
How do I put a variable’s value inside a string (interpolate it into the string)?
(9 answers)
Closed 2 years ago.
I have a function call as follow:
send_at('AT+CMQPUB=0,"sensor",1,0,0,34',"hex_numbers_here",'OK',10)
I want the "34" and the "hex_numbers_here" to be variables instead of literal inputs, but my attempts to "escape" out of the quotes are failing miserably...
Please help!
I tried:
send_at('AT+CMQPUB=0,"sensor",1,0,0,'+myval+',"'+my_hex+'",'OK',10)
where myval and my_hex are the variables I created earlier in my code.
update:
I tried the following as well now:
mqtt_msg = b'testing mqtt message from sim7020'
output = binascii.b2a_hex(mqtt_msg)
length = len(output)
print(output)
output_text = str(output)
print(output_text)
f1 = 'AT+CMQPUB=0,"sensor",1,0,0,'+str(length)+',"'+output_text+'"'
f2 = 'OK'
f3 = '10'
print(f1)
AT+CMQPUB=0,"sensor",1,0,0,66,"b'74657374696e67206d717474206d6573736167652066726f6d2073696d37303230'"
The only thing noiw is that the output_text variable should not contain the b'' - making me think that the casting to str() of the hex/bit value is now doing what I think it should be doing...
BTW - I HAVE to cast the text to binary/hex - this is what the modem wants...
UPDATE:
OK, I found a way around this that is working now:
mqtt_msg = b'testing mqtt message from sim7020 - and again!'
output = binascii.b2a_hex(mqtt_msg)
length = len(output)
print(output)
output_text = str(output)
print(output_text)
output_text = output_text[2:length+2]
new_len = len(output_text)
f1 = 'AT+CMQPUB=0,"sensor",1,0,0,'+str(new_len)+',"'+output_text+'"'
f2 = 'OK'
f3 = '10'
print(f1)
I'm basically just stripping out the first 2 and last chars from the string.
What about this?
def send_at(p1, p2, p3, p4):
print(p1, p2, p3, p4)
send_at('AT+CMQPUB=0,"sensor",1,0,0,{}'.format(34), '{}'.format('FC0012'), 'ok', 10)
Output:
AT+CMQPUB=0,"sensor",1,0,0,34 FC0012 ok 10

Converting strings to code

This is a programming question for Python 3.5
Say I have a string s which I define as
s = "a + b"
and I have some variables
a = 1
b = 2
How can I make a function from the string that uses the variables a and b?
s is something that you can arbitrarily enter as a string input.
def f1(s):
???Code???
return a + b
or
s = "a*b"
def f2(s):
???Code???
return a*b
Does this involve symbolic programming? Is this even possible?
You can use the eval function.
https://docs.python.org/3/library/functions.html#eval
Example:
a = 2
b = 5
eval('a+b') # 7

Format a number to string with thousand separators and always has 2 digits after decimal point in swift

How to format 5432.1 to 5,432.10.
We can use string formatting to force it always show 2 digtis.
let n = 5432.1
let s = String(format: "%.2f", n) //5432.10
We can use number formatter to add thousand separators.
let formatter = NSNumberFormatter()
formatter.numberStyle = .DecimalStyle
let s2 = formatter.stringFromNumber(n) //5,432.1
But how can we combine both? Are there any way to convert that directly?
Or do I have to manipulate those two string results to get the final result?
See NSNumberFormatter.minimumFractionDigits.
let formatter = NSNumberFormatter()
formatter.numberStyle = .DecimalStyle
formatter.minimumFractionDigits = 2
let s2 = formatter.stringFromNumber(n) //5,432.10

Convert HEX to Decimal value? w/ an example

My professor has assured me this example is correct but I can not back into it. I need to convert the mac of my printer to decimal so I can find the decimal value.
In the example he gave me, I have tried this on several online converters and I can not replicate it. What am I missing here, I searched stack I see some examples but I can not reproduce this so this is no duplicate.
MAC = AA:BB:CC:00:11:22, converted to decimal would be 170.187.204.0.17.34
A mac address has a size of 6 byte. This bytes are seperated by colons.
To convert the mac address to decimal you have to convert these single bytes.
So hex AA would be 140 decimal, BB=187 and CC=204 and so on...
A MAC address has six groups of two hexadecimal digits. In this case you can think of ':' as periods to make it easier. So if MAC = AA:BB:CC:00:11:22 = AA.BB.CC.00.11.22 you'll separately convert each of the six hexadecimal groups to decimal form.
When converting from hex to decimal, I like to use exponential notation so I know I'm getting the right answer. After some practice, you pick it up can can do the conversions on sight.
(2nd digit x 161) + (1st digit × 160)
So starting from the right of the address, going through
AA.BB.CC.00.11.22hex group by group looks like:
a.b.c.d.e.f
Remember:
A = 10, B = 11, C = 12, D = 13, E = 14, F = 15
a.
(10 x 161) + (10 × 160) = 170dec
b.
(11 x 161) + (11 × 160) = 187dec
c.
(12 x 161) + (12 × 160) = 204dec
d.
(0 x 161) + (0 × 160) = 0dec
e.
(1 x 161) + (1 × 160) = 17dec
f.
(2 x 161) + (2 × 160) = 34dec
So AA:BB:CC:00:11:22 = 170.187.204.0.17.34

Splitting strings in half, python [duplicate]

This question already has answers here:
Split a string into 2 in Python
(6 answers)
Closed 3 months ago.
I have this string my_string = '717460881855742062' how can I split it in half? The string is auto-generated so just splitting by the 1 won't work
You can try do this way:
firsthalf, secondhalf = my_string[:len(my_string)//2], my_string[len(my_string)//2:]
Something like this should do the job:
my_string = '717460881855742062'
length = int(len(my_string) / 2)
first_part = my_string[:length]
second_part = my_string[length:]
print(first_part)
print(second_part)
output:
717460881
855742062
You can modify it and make sure you take also handle the situation
where the length%2 is not 0.

Resources