Home > database >  how to decode long int to its string value in python
how to decode long int to its string value in python

Time:10-19

Any data, string/long/dobule or in saved in memory as hex or decimal values.

Based on my point, I want to get the double value of the string and sent it over transmission and decode the received double value to string.

In C, I can get following working.

TX

char name[6] = "me123"//underline decimal value is 2127182692
send((double)(long)name);

RX

double rx_val = receive();
char *new_string = (char*)(long)rx_val ;
printf("the string %s \n", new_string );

I could not succeed with RX with python3.

tried with following

enter image description here

got error

enter image description here

how to get double value to string decoding get working?

CodePudding user response:

Ordinary strings in Python represent Unicode objects - and they have to be converted first to a concrete bytes representation by an specific codec using the .encode(...) method. By default, utf-8 is used, but any encoding can be used, and it is necessary if the text you get goes outside the ASCII correspondence to Unicode codepoints- (32-127) .

That done, the int class in Python has a convenient "from_bytes" method that can combine how many bytes you want into a single numeric value.

In short:

mytext = "me123"
mybytes = mytext.encode("utf-8")
mynumber = int.from_bytes(mybytes, "little")
print(mynumber)

Now, if you want the bytes to be seem as a C double (float64), then you can, instead, just decode each 8 bytes as double using the struct module. Note however, that you are responsible to padding the bytes-string to 8 bytes (you are also responsible for that in C, you did not do it, and may have encoded garbage and did risk a segmentation fault, anyway)

mytext = "me123"
mybytes = mytext.encode("utf-8")
mybytes  = b"\x00" * (8 - len(mybytes) % 8)   mybytes
from struct import unpack
mydouble = unpack("<d", mybytes[:8])[0]
print(mydouble)

However, I could not match the numeric value you posted in the question body, neither as integers nor as float64s- probably due to the extra 2 garbage bytes involved in your snippets.

Rather, actually if your C code is at it shows up in the question, the value you are displaing as "double" is actually the transient memory address where your data actually is - as a char[] variable in C is a pointer, not holder of the actual contents, even if declaring a variable as char[6] allocates statically the needed memory. If you do use (double)(*my_text) in your C code (and do the proper 0 padding) you will find the same values as in Python.

to sum up It is likely that your actual problem only needs you to deal with byte-strings (https://docs.python.org/3/library/stdtypes.html#bytes) in Python, and no fancy numeric conversions at all - unless you are getting actual structured binary data from the other endpoint, in which case you should just use the struct module (https://docs.python.org/3/library/struct.html) to get your actual values.

CodePudding user response:

codecs.decode converts a binary string to a normal string not the other way round. Why not just print the string or even the int?

  • Related