Welcome, guest | Sign In | My Account | Store | Cart

A function to convert decimal integer numbers, (from 0 to 255), into byte(s) format. Another function calling the above function to convert ASCII strings into byte(s) format.

Python 3.1.3 (r313:86834, Nov 28 2010, 10:01:07) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information.

>>> exec(open('/home/G0LCU/Desktop/Code/d2b.py').read())
>>> a=78
>>> type(a)
<class 'int'>
>>> b=d2b(a)
>>> print(b)
b'N'
>>> type(b)
<class 'bytes'>
>>> text="\x00(C)2012, B.Walker, G0LCU.\xFF"
>>> len(text)
27
>>> type(text)
<class 'str'>
>>> newtext=t2b(text)
>>> len(newtext)
27
>>> print(newtext)
b'\x00(C)2012, B.Walker, G0LCU.\xff'
>>> type(newtext)
<class 'bytes'>

It requires NOTHING special at all to work and can be run like above or imported from the correct "Lib" drawer/folder/directorfy as:-

>>> import d2b

And when imported called as:-

>>> d2b.d2b(some_number, optional_some_other_mumber)<RETURN/ENTER>

OR

>>> d2b.t2b(some_ASCII_string)<RETURN/ENTER>

Read the code for much more information...

Issued under the GPL2 licence.

Enjoy finding simple solutions to often very difficult problems.

Bazza, G0LCU.

Python, 354 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# d2b.py
#
# Decimal to byte(s) and character/string to byte(s) converter.
# Any decimal number from 0 to 255 converted to b"\x??" format.
# Any standard ASCII string, something like "\x00Some String.\xFF" converted
# to b"\x00Some String.\xff". NOTE:- "FF" is displayed as "ff" on conversion
# but the end result is the same.
#
# NOTE:- The reverse, b"\x00Some String.\xff" converted to
# "\x00Some String.\xff", is easy and therefore not included in this function.
#
# What I needed......
# The function, sometext=chr(a+b), (where "a" could be for example 127 and
# "b" in this case is -127<=b<=128), capability is not easily possible with the
# b"string" byte(s) format so this function was devised.
#
# See here for an example of chr(a+b):-
# http://code.activestate.com/recipes/578013-amplitude-modulation-tremolo-was-an-audiosound-sni/?in=lang-python
#
# This new function uses NO import(s), nor any special programming style,
# but is written so that anyone can understand, (including kids), how it works.
# It does NOT need any special encoding or any other "Pythonic" requirements at all.
# It is not "elegant", as professionals may call it, but, it is functional.
#
# I have no idea at all as to the upper limit of ASCII string length it can
# handle, however it CAN handle a(n) "", (empty), string.
#
# ==========================================================================
#
# Python 3.1.3 (r313:86834, Nov 28 2010, 10:01:07) 
# [GCC 4.4.5] on linux2
# Type "help", "copyright", "credits" or "license" for more information.
# >>> exec(open('/home/G0LCU/Desktop/Code/d2b.py').read())
# >>> a=78
# >>> type(a)
# <class 'int'>
# >>> b=d2b(a)
# >>> print(b)
# b'N'
# >>> type(b)
# <class 'bytes'>
# >>> text="\x00(C)2012, B.Walker, G0LCU.\xFF"
# >>> len(text)
# 27
# >>> type(text)
# <class 'str'>
# >>> newtext=t2b(text)
# >>> len(newtext)
# 27
# >>> print(newtext)
# b'\x00(C)2012, B.Walker, G0LCU.\xff'
# >>> type(newtext)
# <class 'bytes'>
#
# ============================================================================
#
# Copyright, (C)2012, B.Walker, G0LCU.
# Issued under GPL2 licence...
# Enjoy finding simple solutions to often very difficult problems.
#
# Requires Python Version 3.x.x only.
# Tested on Debian 6.0.0 using Python 3.1.3, and PCLinuxOS 2009 using Python 3.2.1.
# Also Tested on MS Windows Vista, 32 bit, using Python 3.0.1 and Python 3.2.2.

# Decimal number to byte(s) string converter.
def d2b(a,b=0):
	# "a" any decimal number between 0 to 255.
	# "b" is reserved, but for my usage is very useful, see above... ;o)
	# Don't allow any floating point numbers.
	a=int(a)
	b=int(b)
	decimal=a+b
	# Out of range checks forcing function to exit!
	if decimal<=-1 or decimal>=256:
		print("\nError in d2b(a,b=0) function, decimal integer out of range, (0 to 255)!\n")
		# FORCE a Python error to stop the function from proceeding; "FORCED_HALT" IS NOT DEFINED!
		print(FORCED_HALT)
	# Convert new decimal value 0 to 255 to b"\x??" value.
	if decimal==0: newbyte=b"\x00"
	if decimal==1: newbyte=b"\x01"
	if decimal==2: newbyte=b"\x02"
	if decimal==3: newbyte=b"\x03"
	if decimal==4: newbyte=b"\x04"
	if decimal==5: newbyte=b"\x05"
	if decimal==6: newbyte=b"\x06"
	if decimal==7: newbyte=b"\x07"
	if decimal==8: newbyte=b"\x08"
	if decimal==9: newbyte=b"\x09"
	if decimal==10: newbyte=b"\x0A"
	if decimal==11: newbyte=b"\x0B"
	if decimal==12: newbyte=b"\x0C"
	if decimal==13: newbyte=b"\x0D"
	if decimal==14: newbyte=b"\x0E"
	if decimal==15: newbyte=b"\x0F"
	if decimal==16: newbyte=b"\x10"
	if decimal==17: newbyte=b"\x11"
	if decimal==18: newbyte=b"\x12"
	if decimal==19: newbyte=b"\x13"
	if decimal==20: newbyte=b"\x14"
	if decimal==21: newbyte=b"\x15"
	if decimal==22: newbyte=b"\x16"
	if decimal==23: newbyte=b"\x17"
	if decimal==24: newbyte=b"\x18"
	if decimal==25: newbyte=b"\x19"
	if decimal==26: newbyte=b"\x1A"
	if decimal==27: newbyte=b"\x1B"
	if decimal==28: newbyte=b"\x1C"
	if decimal==29: newbyte=b"\x1D"
	if decimal==30: newbyte=b"\x1E"
	if decimal==31: newbyte=b"\x1F"
	if decimal==32: newbyte=b"\x20"
	if decimal==33: newbyte=b"\x21"
	if decimal==34: newbyte=b"\x22"
	if decimal==35: newbyte=b"\x23"
	if decimal==36: newbyte=b"\x24"
	if decimal==37: newbyte=b"\x25"
	if decimal==38: newbyte=b"\x26"
	if decimal==39: newbyte=b"\x27"
	if decimal==40: newbyte=b"\x28"
	if decimal==41: newbyte=b"\x29"
	if decimal==42: newbyte=b"\x2A"
	if decimal==43: newbyte=b"\x2B"
	if decimal==44: newbyte=b"\x2C"
	if decimal==45: newbyte=b"\x2D"
	if decimal==46: newbyte=b"\x2E"
	if decimal==47: newbyte=b"\x2F"
	if decimal==48: newbyte=b"\x30"
	if decimal==49: newbyte=b"\x31"
	if decimal==50: newbyte=b"\x32"
	if decimal==51: newbyte=b"\x33"
	if decimal==52: newbyte=b"\x34"
	if decimal==53: newbyte=b"\x35"
	if decimal==54: newbyte=b"\x36"
	if decimal==55: newbyte=b"\x37"
	if decimal==56: newbyte=b"\x38"
	if decimal==57: newbyte=b"\x39"
	if decimal==58: newbyte=b"\x3A"
	if decimal==59: newbyte=b"\x3B"
	if decimal==60: newbyte=b"\x3C"
	if decimal==61: newbyte=b"\x3D"
	if decimal==62: newbyte=b"\x3E"
	if decimal==63: newbyte=b"\x3F"
	if decimal==64: newbyte=b"\x40"
	if decimal==65: newbyte=b"\x41"
	if decimal==66: newbyte=b"\x42"
	if decimal==67: newbyte=b"\x43"
	if decimal==68: newbyte=b"\x44"
	if decimal==69: newbyte=b"\x45"
	if decimal==70: newbyte=b"\x46"
	if decimal==71: newbyte=b"\x47"
	if decimal==72: newbyte=b"\x48"
	if decimal==73: newbyte=b"\x49"
	if decimal==74: newbyte=b"\x4A"
	if decimal==75: newbyte=b"\x4B"
	if decimal==76: newbyte=b"\x4C"
	if decimal==77: newbyte=b"\x4D"
	if decimal==78: newbyte=b"\x4E"
	if decimal==79: newbyte=b"\x4F"
	if decimal==80: newbyte=b"\x50"
	if decimal==81: newbyte=b"\x51"
	if decimal==82: newbyte=b"\x52"
	if decimal==83: newbyte=b"\x53"
	if decimal==84: newbyte=b"\x54"
	if decimal==85: newbyte=b"\x55"
	if decimal==86: newbyte=b"\x56"
	if decimal==87: newbyte=b"\x57"
	if decimal==88: newbyte=b"\x58"
	if decimal==89: newbyte=b"\x59"
	if decimal==90: newbyte=b"\x5A"
	if decimal==91: newbyte=b"\x5B"
	if decimal==92: newbyte=b"\x5C"
	if decimal==93: newbyte=b"\x5D"
	if decimal==94: newbyte=b"\x5E"
	if decimal==95: newbyte=b"\x5F"
	if decimal==96: newbyte=b"\x60"
	if decimal==97: newbyte=b"\x61"
	if decimal==98: newbyte=b"\x62"
	if decimal==99: newbyte=b"\x63"
	if decimal==100: newbyte=b"\x64"
	if decimal==101: newbyte=b"\x65"
	if decimal==102: newbyte=b"\x66"
	if decimal==103: newbyte=b"\x67"
	if decimal==104: newbyte=b"\x68"
	if decimal==105: newbyte=b"\x69"
	if decimal==106: newbyte=b"\x6A"
	if decimal==107: newbyte=b"\x6B"
	if decimal==108: newbyte=b"\x6C"
	if decimal==109: newbyte=b"\x6D"
	if decimal==110: newbyte=b"\x6E"
	if decimal==111: newbyte=b"\x6F"
	if decimal==112: newbyte=b"\x70"
	if decimal==113: newbyte=b"\x71"
	if decimal==114: newbyte=b"\x72"
	if decimal==115: newbyte=b"\x73"
	if decimal==116: newbyte=b"\x74"
	if decimal==117: newbyte=b"\x75"
	if decimal==118: newbyte=b"\x76"
	if decimal==119: newbyte=b"\x77"
	if decimal==120: newbyte=b"\x78"
	if decimal==121: newbyte=b"\x79"
	if decimal==122: newbyte=b"\x7A"
	if decimal==123: newbyte=b"\x7B"
	if decimal==124: newbyte=b"\x7C"
	if decimal==125: newbyte=b"\x7D"
	if decimal==126: newbyte=b"\x7E"
	if decimal==127: newbyte=b"\x7F"
	if decimal==128: newbyte=b"\x80"
	if decimal==129: newbyte=b"\x81"
	if decimal==130: newbyte=b"\x82"
	if decimal==131: newbyte=b"\x83"
	if decimal==132: newbyte=b"\x84"
	if decimal==133: newbyte=b"\x85"
	if decimal==134: newbyte=b"\x86"
	if decimal==135: newbyte=b"\x87"
	if decimal==136: newbyte=b"\x88"
	if decimal==137: newbyte=b"\x89"
	if decimal==138: newbyte=b"\x8A"
	if decimal==139: newbyte=b"\x8B"
	if decimal==140: newbyte=b"\x8C"
	if decimal==141: newbyte=b"\x8D"
	if decimal==142: newbyte=b"\x8E"
	if decimal==143: newbyte=b"\x8F"
	if decimal==144: newbyte=b"\x90"
	if decimal==145: newbyte=b"\x91"
	if decimal==146: newbyte=b"\x92"
	if decimal==147: newbyte=b"\x93"
	if decimal==148: newbyte=b"\x94"
	if decimal==149: newbyte=b"\x95"
	if decimal==150: newbyte=b"\x96"
	if decimal==151: newbyte=b"\x97"
	if decimal==152: newbyte=b"\x98"
	if decimal==153: newbyte=b"\x99"
	if decimal==154: newbyte=b"\x9A"
	if decimal==155: newbyte=b"\x9B"
	if decimal==156: newbyte=b"\x9C"
	if decimal==157: newbyte=b"\x9D"
	if decimal==158: newbyte=b"\x9E"
	if decimal==159: newbyte=b"\x9F"
	if decimal==160: newbyte=b"\xA0"
	if decimal==161: newbyte=b"\xA1"
	if decimal==162: newbyte=b"\xA2"
	if decimal==163: newbyte=b"\xA3"
	if decimal==164: newbyte=b"\xA4"
	if decimal==165: newbyte=b"\xA5"
	if decimal==166: newbyte=b"\xA6"
	if decimal==167: newbyte=b"\xA7"
	if decimal==168: newbyte=b"\xA8"
	if decimal==169: newbyte=b"\xA9"
	if decimal==170: newbyte=b"\xAA"
	if decimal==171: newbyte=b"\xAB"
	if decimal==172: newbyte=b"\xAC"
	if decimal==173: newbyte=b"\xAD"
	if decimal==174: newbyte=b"\xAE"
	if decimal==175: newbyte=b"\xAF"
	if decimal==176: newbyte=b"\xB0"
	if decimal==177: newbyte=b"\xB1"
	if decimal==178: newbyte=b"\xB2"
	if decimal==179: newbyte=b"\xB3"
	if decimal==180: newbyte=b"\xB4"
	if decimal==181: newbyte=b"\xB5"
	if decimal==182: newbyte=b"\xB6"
	if decimal==183: newbyte=b"\xB7"
	if decimal==184: newbyte=b"\xB8"
	if decimal==185: newbyte=b"\xB9"
	if decimal==186: newbyte=b"\xBA"
	if decimal==187: newbyte=b"\xBB"
	if decimal==188: newbyte=b"\xBC"
	if decimal==189: newbyte=b"\xBD"
	if decimal==190: newbyte=b"\xBE"
	if decimal==191: newbyte=b"\xBF"
	if decimal==192: newbyte=b"\xC0"
	if decimal==193: newbyte=b"\xC1"
	if decimal==194: newbyte=b"\xC2"
	if decimal==195: newbyte=b"\xC3"
	if decimal==196: newbyte=b"\xC4"
	if decimal==197: newbyte=b"\xC5"
	if decimal==198: newbyte=b"\xC6"
	if decimal==199: newbyte=b"\xC7"
	if decimal==200: newbyte=b"\xC8"
	if decimal==201: newbyte=b"\xC9"
	if decimal==202: newbyte=b"\xCA"
	if decimal==203: newbyte=b"\xCB"
	if decimal==204: newbyte=b"\xCC"
	if decimal==205: newbyte=b"\xCD"
	if decimal==206: newbyte=b"\xCE"
	if decimal==207: newbyte=b"\xCF"
	if decimal==208: newbyte=b"\xD0"
	if decimal==209: newbyte=b"\xD1"
	if decimal==210: newbyte=b"\xD2"
	if decimal==211: newbyte=b"\xD3"
	if decimal==212: newbyte=b"\xD4"
	if decimal==213: newbyte=b"\xD5"
	if decimal==214: newbyte=b"\xD6"
	if decimal==215: newbyte=b"\xD7"
	if decimal==216: newbyte=b"\xD8"
	if decimal==217: newbyte=b"\xD9"
	if decimal==218: newbyte=b"\xDA"
	if decimal==219: newbyte=b"\xDB"
	if decimal==220: newbyte=b"\xDC"
	if decimal==221: newbyte=b"\xDD"
	if decimal==222: newbyte=b"\xDE"
	if decimal==223: newbyte=b"\xDF"
	if decimal==224: newbyte=b"\xE0"
	if decimal==225: newbyte=b"\xE1"
	if decimal==226: newbyte=b"\xE2"
	if decimal==227: newbyte=b"\xE3"
	if decimal==228: newbyte=b"\xE4"
	if decimal==229: newbyte=b"\xE5"
	if decimal==230: newbyte=b"\xE6"
	if decimal==231: newbyte=b"\xE7"
	if decimal==232: newbyte=b"\xE8"
	if decimal==233: newbyte=b"\xE9"
	if decimal==234: newbyte=b"\xEA"
	if decimal==235: newbyte=b"\xEB"
	if decimal==236: newbyte=b"\xEC"
	if decimal==237: newbyte=b"\xED"
	if decimal==238: newbyte=b"\xEE"
	if decimal==239: newbyte=b"\xEF"
	if decimal==240: newbyte=b"\xF0"
	if decimal==241: newbyte=b"\xF1"
	if decimal==242: newbyte=b"\xF2"
	if decimal==243: newbyte=b"\xF3"
	if decimal==244: newbyte=b"\xF4"
	if decimal==245: newbyte=b"\xF5"
	if decimal==246: newbyte=b"\xF6"
	if decimal==247: newbyte=b"\xF7"
	if decimal==248: newbyte=b"\xF8"
	if decimal==249: newbyte=b"\xF9"
	if decimal==250: newbyte=b"\xFA"
	if decimal==251: newbyte=b"\xFB"
	if decimal==252: newbyte=b"\xFC"
	if decimal==253: newbyte=b"\xFD"
	if decimal==254: newbyte=b"\xFE"
	if decimal==255: newbyte=b"\xFF"
	return(newbyte)

# Text/Character string to byte(s) string converter.
# "some_string" is any ASCII string including "\x??" characters as required.
def t2b(some_string):
	# Allocate an empty byte(s) string.
	new_byte_string=b""
	# Use the loop to build the byte(s) string from a standard string.
	for n in range(0,len(some_string),1):
		# Convert each _character_ in the string to a decimal number.
		decimal_number=ord(some_string[n])
		# Call the "d2b()" above function.
		d2b_character=d2b(decimal_number)
		# Build the byte(s) string one character at a time.
		new_byte_string=new_byte_string+d2b_character
	# The complete byte(s) string has now been converted.
	return(new_byte_string)

# End of d2b() function...
# Enjoy finding simple solutions to often very difficult problems... ;o)

I really don't care how anyone votes nor what any professionals think of this code, as someone will like it. There may well be a much more "Pythonic" way of doing it, but to a 10 year old that way will just look like visual noise.

I needed a VERY simple way of "real_time" audio access in Linux using Python 3.x.x, just like inside the pointer above.

Enjoy finding simple solutions to often VERY difficult problems... ;o)

Bazza, G0LCU.

5 comments

Daniel Lepage 12 years, 2 months ago  # | flag

For what it's worth, the pythonic way is to use the builtin constructor for the "bytes" class:

def d2b(a, b=0):
    decimal = int(a)+int(b)
    return bytes([decimal])

def t2b(some_string):
    decimals = [ord(c) for c in some_string]
    return bytes(decimals)
Barry Walker (author) 12 years, 2 months ago  # | flag

Daniel, many thanks.

I don't mind being voted down with something much smaller and obviously better. (Even kids would be able to understand that code as it does not look like visual noise.)

I shall try your snippet out inside the pointer code above tonight. (Mine, although very BASIC like, does work.)

Again many thanks... ;o)

Bazza...

Barry Walker (author) 12 years ago  # | flag

Oh dear, voted down yet again...

Who cares?

Bazza...

rylangrayston 11 years, 8 months ago  # | flag

Well Im voting you in!!! your post really helped me understand exactly what is going on.

I didnt realize that the \x dosent change and every where else I looked people were converting big in into 32 bit and all the bit shifting made it really hard to grasp without knowing the very basics first !!!

I kept typing bytes(5)

now i know i have to type bytes([5]) because bytes() only converts int into bytes if it receives the int from an array

Thanks Barry Walker and Daniel Lepage

Thanks to you i am now controlling stepper motors with blender 3d and arduino!

Barry Walker (author) 11 years, 8 months ago  # | flag

Hi rylangrayston...

Many thanks for your comments and re-vote upwards from -1 to 0... Much appreciated...

(The most credit must go to Daniel Lepage, many thanks Daniel...)

"Thanks to you i am now controlling stepper motors with blender 3d and arduino!"

Good one keep it up, you are much like me... ;o)

Just view my uploads here:-

http://code.activestate.com/recipes/users/4177147/

And you will find some relevant Arduino stuff too...

Once again many thanks for your up-vote...

Bazza, G0LCU...