Convert an int value to unicode

前端 未结 4 869
臣服心动
臣服心动 2020-12-30 23:18

I am using pyserial and need to send some values less than 255. If I send the int itself the the ascii value of the int gets sent. So now I am converting the int into a unic

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-30 23:53

    In Python 2 - Turn it into a string first, then into unicode.

    str(integer).decode("utf-8")
    

    Best way I think. Works with any integer, plus still works if you put a string in as the input.

    Updated edit due to a comment: For Python 2 and 3 - This works on both but a bit messy:

    str(integer).encode("utf-8").decode("utf-8") 
    

提交回复
热议问题