0
data="";
for x in range(255):
  data = data + chr(x)
  return (data)

This is written in python and using flask framework.

I am trying to send Integer value to my hardware but as flask return does not support it I had to convert them to a string of respective ASCII values.

From 0-127 things are fine but after that, I am getting UTF-8 encoded value. Please see the screenshot of the terminal window below. Is there any way that I can get respective integer value instead of UTF-8 when converted above 127.

For example: chr(128) => when returned should give 128 not UTF-8 enter image description here

What I am looking for, how I can get the same integer value even after 127?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • How are you attempting to return this to the caller? If you're going to return binary data, don't use a string in python3 - use a byte sequence (`b''`) – MatsLindh Jul 12 '19 at 21:06
  • I am returning this from flask microframework for python. It only supports string, dict and tuple as return object. This is why I had to convert int to repective ASCII value and pass it as string in return but once the value is above 127 it starts encoding into UTF-8. – SANTOSH VERMA Jul 13 '19 at 04:42
  • It likely is using UTF-8 for all codepoints. You are just miss-reading the bytes as ASCII because that (by the design of UTF-8) gives you the result that you want. – Tom Blodget Jul 13 '19 at 14:46
  • @SANTOSHVERMA Not really - you should use `make_response()` to return a byte sequence directly. You can manipulate the response returned from `make_response` with the proper content type. See [Flask to return image store in database](https://stackoverflow.com/questions/11017466/flask-to-return-image-stored-in-database) – MatsLindh Jul 13 '19 at 18:39

1 Answers1

0

After 128 its not ascii value its become Unicode from 128 to 255. So go through Unicode documentation. Try another for loop for remaining 128 to 255.

Data = data + x.unicode('utf-128')