python将字符转化成ascll码的格式
程序员文章站
2022-03-31 10:00:33
...
背景
帮同学写了一个将给定指令转换成ascl码的格式的方案,可以对给定的指令集合,将其解析成ascll 码然后放到vivado中使用
代码
#指令名称
text_to_translate = ['PRESET FAST','NPLC 1E-2','DELAY 0','TRIG EXT']
# 数据名称
array_name = 'conf_3458_xxx'
length = 0
for item in text_to_translate:
length += len(item)
result_text = ''
cnt = 0
fivecnt=0
for item in text_to_translate:
result_text += '//'+ item+'\n' +'len:'+str(len(item))
result_text += array_name+'['+str(cnt)+']='+'8\'d'+ str(len(item))+';\n'
cnt+=1
fivecnt = 0
for letter in item:
result_text += array_name+'['+str(cnt)+']='+'8\'h' + hex(ord(letter))[-2:]+';'
fivecnt += 1
if fivecnt == 5:
result_text += '\n'
cnt +=1
result_text +='\n\n'
print(result_text)
上一篇: 登录验证新思路,演示登录代码.