How to convert from string to 16-bit unsigned integer in python? - c#

I'm currently working on some encoding and decoding of the string in python. I was supposed to convert some code from C# to python, however I encountered some problem as below:
So now I have a string that looks like this: 21-20-89-00-67-00-45-78
The code was supposed to eliminates the - in between the numbers, and packed 2 integers into 1 group, then convert them into bytes. In C#, it was done like this:
var value = "21-20-89-00-67-00-45-78";
var valueNoDash = value.Replace("-", null);
for (var i = 0; i < DataSizeInByte; i++)
{
//convert every 2 digits into 1 byte
Data[i] = Convert.ToByte(valueNoDash.Substring(i * 2, 2), 16);
}
The above code represents Step 1: Remove - from the string, Step 2: using Substring method to divide them into 2 digits in 1 group, Step 3: use Convert.ToByte with base 16 to convert them into 16-bit unsigned integer. The results in Data is
33
32
137
0
103
0
69
120
So far I have no problem with this C# code, however when I try to do the same in python, I could not get to the same result as the C# code. My python code are as below:
from textwrap import wrap
import struct
values = "21-20-89-00-67-00-45-78"
values_no_dash = a.replace('-', '')
values_grouped = wrap(b, 2)
values_list = []
for value in values_grouped:
values_list.append(struct.pack('i', int(value)))
In python, it gives me list of bytes in hex value, which is as below:
b'\x15\x00\x00\x00'
b'\x14\x00\x00\x00'
b'Y\x00\x00\x00'
b'\x00\x00\x00\x00'
b'C\x00\x00\x00'
b'\x00\x00\x00\x00'
b'-\x00\x00\x00'
b'N\x00\x00\x00'
This is in bytes object, however when I converted this object into Decimal, it gives me the exact same value as the original string: 21, 20, 89, 0, 67, 0, 45, 78.
Which means I did not convert successfully into 16-bit unsigned integer right? How can I do this in python? I've tried using str.encode() but the result still different. How can I achieve what C# had done in python?
Thanks and appreciates if anyone can help!

I think this is the solution you're looking for:
values = "21-20-89-00-67-00-45-78"
values_no_dash_grouped = values.split('-') #deletes dashes and groups numbers simultaneously
for value in values_no_dash_grouped:
print(int(value, 16)) #converts number in base 16 to base 10 and prints it
Hope it helps!

Related

Split and store an integer into two byte

I am working on a Embedded systems and I have only 2 Bytes of storage. I need to store a JSON response in those 2 byte. the JSON response is a string containing 2 digits. How can I convert the string to an unsigned integer split and save into those 2 bytes. I am using C#:
var results = "16";
I need to convert this and store it into 2 bytes.
As your value is only 2 digits long you just need 1 byte to store it.
You can just call Byte.Parse("16") and you will get 16 as a byte.
You can then store your byte whereever you want.
What #TheBlueOne said - a two digit number, even when hexadecimal requires just 1 byte - but for larger numbers you can use
BitConverter.GetBytes.
var s2 = "FF01";
var n = Convert.ToUInt16(s2, 16);
var bytes = BitConverter.GetBytes(n);
//bytes[0] = 1
//bytes[1] = 255

Bit shifting with hex in Python

I am trying to understand how to perform bit shift operations in Python. Coming from C#, it doesn't work in the same way.
The C# code is;
var plain=0xabcdef0000000; // plaintext
var key=0xf0f0f0f0f123456; // encryption key
var L = plain;
var R = plain>>32;
The output is;
000abcdef0000000 00000000000abcde
What is the equivilent in Python? I have tried;
plain = 0xabcdef0000000
key = 0xf0f0f0f0f123456
print plain
left = plain
right = plain >> 32
print hex(left)
print hex(right)
However, it doesn't work. The output is different in Python. The 0's padding are missing. Any help would be appreciated!
The hex() function does not pad numbers with leading zeros, because Python integers are unbounded. C# integers have a fixed size (64 bits in this case), so have an upper bound and can therefor be padded out. This doesn't mean those extra padding zeros carry any meaning; the integer value is the same.
You'll have to explicitly add those zeros, using the format() function to produce the output:
print format(left, '#018x')
print format(right, '#018x')
The # tells format() to include the 0x prefix, and the leading 0 before the field width asks format() to pad the output:
>>> print format(left, '#018x')
0x000abcdef0000000
>>> print format(right, '#018x')
0x0000000000abcde
Note that the width includes the 0x prefix; there are 16 hex digits in that output, representing 64 bits of data.
If you wanted to use a dynamic width based on the number of characters used in key, then calculate that from int.bit_length(); every 4 bits produce a hex character:
format(right, '#0{}x'.format((key.bit_length() + 3) // 4 + 2))
Demo:
>>> (key.bit_length() + 3) // 4 + 2
17
>>> print format(right, '#0{}x'.format((key.bit_length() + 3) // 4 + 2))
0x0000000000abcde
But note that even the key is only 60 bits in length and C# would pad that value with an 0 as well.
I have no problem with you you tried
>>> hex(0xabcdef0000000)
'0xabcdef0000000'
>>> hex(0xabcdef0000000 >> 32)
'0xabcde'
In [83]: plain=0xabcdef0000000
In [84]: plain>>32
Out[84]: 703710
In [85]: plain
Out[85]: 3022415462400000
In [87]: hex(plain)
Out[87]: '0xabcdef0000000'
if
In [134]: left = plain
In [135]: right = plain >> 32
Then
In [140]: '{:0x}'.format(left)
Out[140]: 'abcdef0000000'
In [143]: '{:018x}'.format(right)
Out[143]: '0000000000000abcde'

How to store integers efficiently in bytes ?

I'm looking for a good and efficient way to store integers in bytes.
The situation is the following:
I have two integers, Value 1 is "1857" (11bit) and Value 2 is "14" (4bit) and 2 bytes (16bit).
What I'm looking for, is to store the 2 integers in the 2 bytes. This mean cut the first integer, put 8 bits in the first byte and the rest plus the second integer in the second byte. Also I need to get them back together.
Is there a way or .net class to do that?
I've found the BitConverter class, but thats not what I'm looking for because this class only convert one integer to an full byte array.
You could use bit operators for that: bitwise and (&), or (|), and shift left (<<) and right (>>):
int value1 = 1857;
int value2 = 14;
int result = value1 | (value2 << 11);
To get the original values back you have to reverse that:
int result1 = result & 0x7ff; // 1857
int result2 = result >> 11; // 14

bitwise or a number in int form with a number in hex form

I have a number
int number = 509; // integer
string bool_number = Convert.ToString(number, 2); // same integer converted to binary no
I want to bitwise or this number with hex values 0x01, 0x02, 0x04 and 0x08.
(e.g. something like this)
result = number | 0x01
How can I do it? Should I convert number to hex form or whats the right way?
You can use hexadecimal values as numeric literals...
int number = 509;
int hexNumber = 0x02;
int newNumber = number | hexNumber;
// whatever
string newNumberAsBinaryString = Convert.ToString(newNumber, 2);
Console.WriteLine(newNumber);
// etc.
If you need to input a hex string and convert it to a numeric type:
int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
If you need to output a numeric type as hex:
Console.WriteLine(num.ToString("x"));
// or
Console.WriteLine("{0:x}", num);
See also MSDN's page on dealing with hex strings.
An int value isn't in any particular base. You can use bitwise operators on an int at any time - there's no need to convert it first. For example:
int a = 509;
int b = 0x1fd;
The variables a and b have exactly the same value here. I happen to have used a decimal literal to initialize a, and a hex literal to initialize b, but the effect is precisely the same.
So you can bitwise OR your ints at any time. Your example (adding a suitable declaration and semicolon to make it compile):
int result = number | 0x01;
will work just fine - you don't need to do anything to prepare number for this sort of usage. (Incidentally, this will do nothing, because the result of a bitwise OR of the numbers 509 and 1 is 509. If you write 509 in binary you get 111111101 - the bottom bit is already 1, so ORing in 1 won't change anything.)
You should avoid thinking in terms of things like "hex values", because there isn't really any such thing in C#. Numeric bases are only relevant for numbers represented as strings, which typically means either literals in source code, or conversions done at runtime. For example, if your program accepts a number as a command line argument, then that will arrive as a string, so you'll need to know its base to convert it correctly to an int. But once it's an int it's just an int - there's no such thing as a hex value or a decimal value for an int.

How to convert a byte to a char, e.g. 1 -> '1'?

How to convert a byte to a char? I don't mean an ASCII representation.
I have a variable of type byte and want it as a character.
I want just following conversions from byte to char:
0 ->'0'
1 ->'1'
2 ->'2'
3 ->'3'
4 ->'4'
5 ->'5'
6 ->'6'
7 ->'7'
8 ->'8'
9 ->'9'
(char)1 and Convert.ToChar(1) do not work. They result in '' because they think 1 is the ASCII code.
the number .ToString();
one.ToString(); // one.ToString()[0] - first char -'1'
two.ToString(); // two.ToString()[0] - first char -'2'
Note that you can't really convert a byte to a char
char is one char while byte can even three digits value!
If you want to use LINQ and you're sure the byte won't exceed one digit(10+) you can use this:
number.ToString().Single();
Simply using variable.ToString() should be enough. If you want to get fancy, add the ASCII code of 0 to the variable before converting:
Convert.ToChar(variable + Convert.ToByte('0'));
Use this for conversion.
(char)(mybyte + 48);
where mybyte = 0 or 1 and so
OR
Convert.ToChar(1 + 48); // specific case for 1
While others have given solution i'll tell you why your (char)1 and Convert.ToChar(1) is not working.
When your convert byte 1 to char it takes that 1 as an ASCII value.
Now ASCII of 1 != 1.
Add 48 in it because ASCII of 1 == 1 + 48`. Similar cases for 0, 2 and so on.
Assume you have variable byte x;
Just use (char)(x + '0')
Use Convert.ToString() to perform this.

Categories