Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to convert string to byte without encoding python

>>> message = 'test 112 hello: what?!'
>>> message = message.encode('iso-8859-15')
>>> message 
b'test 112 hello: what?!'
Comment

how to convert string to byte without encoding python

import struct

def rawbytes(s):
    """Convert a string to raw bytes without encoding"""
    outlist = []
    for cp in s:
        num = ord(cp)
        if num < 255:
            outlist.append(struct.pack('B', num))
        elif num < 65535:
            outlist.append(struct.pack('>H', num))
        else:
            b = (num & 0xFF0000) >> 16
            H = num & 0xFFFF
            outlist.append(struct.pack('>bH', b, H))
    return b''.join(outlist)
Comment

PREVIOUS NEXT
Code Example
Python :: psyche asteroid 
Python :: count gabarit django 
Python :: dictionary function fromkeys in python 
Python :: print var python 
Python :: pytohn epsilon 
Python :: how to use prettytable with python 
Python :: no migrations to apply django 
Python :: pynput left click command 
Python :: python candlestick chart 
Python :: strip unicode characters from strings python 
Python :: how to cancel a input in python 
Python :: flask_mail 
Python :: pandas read_csv nan as empty string 
Python :: gnome-shell turn off 
Python :: convert list into integer python 
Python :: string to datetime python 
Python :: list to dict python 
Python :: icon tkiner 
Python :: sort list in python by substring 
Python :: json.dumps no spaces 
Python :: python make a list of odd numbers 
Python :: pd dataframe get column names 
Python :: if django 
Python :: dice rolling simulator python 
Python :: how to send emails in python 
Python :: python string math 
Python :: numpy generate random 2d array 
Python :: python get number of days 
Python :: create empty pandas dataframe 
Python :: string startswith python 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =