如何将null(0)终止的字符串转换为Python字符串? [英] How do I converted a null (0) terminated string to a Python string?
问题描述
大家好,
我收到(通过UDP)一个空终止的字符串,需要将它转换成一个Python字符串。谁能告诉我这是怎么做到的?如果它有帮助,
我知道字符串中的字符数。
谢谢,
M. McDonnell >
Hi All,
I''ve received (via UDP) a null terminated string and need to convert it
into a Python string. Can anyone tell me how this is done? If it helps,
I know the number of characters in the string.
Thanks,
M. McDonnell
推荐答案
Michael写道:
Michael wrote:
大家好,
我收到(通过UDP)一个空终止的字符串,需要将它转换成一个Python字符串。谁能告诉我这是怎么做到的?如果它有帮助,
我知道字符串中的字符数。
谢谢,
M. McDonnell >
您是否在Python或C中收到此字符串?如果是前者,那么
只丢掉你收到的字符串的最后一个字符,并且你已经完成了
!
s = s [: - 1]
问候
Steve
-
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC / Ltd http://www.holdenweb.com
Skype:holdenweb http://holdenweb.blogspot.com
最近的Ramblings http://del.icio.us/steve.holden
Have you received this string in Python or in C? If the former, then
just throw away the last character of the string you''ve received and
you''re done!
s = s[:-1]
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
Michael写道:
Michael wrote:
大家好,
我收到(通过UDP)一个空终止的字符串,需要转换它
成Python字符串。谁能告诉我这是怎么做到的?如果它有帮助,
我知道字符串中的字符数。
Hi All,
I''ve received (via UDP) a null terminated string and need to convert it
into a Python string. Can anyone tell me how this is done? If it helps,
I know the number of characters in the string.
我认为你的意思是NUL,而不是null。
如果它不是Python字符串,你收到了什么?
你可能需要/想要这个:
如果strg [-1] ==" \0":
strg = strg [: - 1]
或者:
strg = strg.rstrip(" \0")#需要Python 2.2.2或更高版本
你可能在谈论一个固定长度字符串
包含useful_stuff +" \0" +填充 - 在这种情况下你需要
strg = strg.split(" \0")[0]#抓住(但不包括)第一个
NUL(如果有的话)
如果你不确定你有什么,请打印repr(the_input_string)
HTH,
John
I think you mean NUL, not null.
What have you received it into, if it''s not a Python string?
You probably need/want this:
if strg[-1] == "\0":
strg = strg[:-1]
alternatively:
strg = strg.rstrip("\0") # requires Python 2.2.2 or later
It''s possible you may be talking about a fixed length string which
contains useful_stuff + "\0" + padding -- in that case you need
strg = strg.split("\0")[0] # grab upto (but not including) the first
NUL (if any)
If you''re not sure what you''ve got, print repr(the_input_string)
HTH,
John
非常感谢您的回复。回答一些
的问题...是的,我在Python中接收一个C语言0终止
字符串以UDP数据包发送到我的Python程序(这是多少
我知道数量)。鉴于此
澄清,您的回复是否仍然正确?
非常感谢,
MDM
John Machin写道:
Thank you very much for your responses. To answer some of the
questions... Yes, I am in Python receiving a C language 0 terminated
string that was sent to my Python program in a UDP packet (which is how
I know the count). Are your responses still correct given this
clarification?
Thanks much,
MDM
John Machin wrote:
Michael写道:
Michael wrote:
大家好,
我收到(通过UDP)一个空终止的字符串,需要将它转换成一个Python字符串。谁能告诉我这是怎么做到的?如果它有帮助,
我知道字符串中的字符数。
Hi All,
I''ve received (via UDP) a null terminated string and need to convert it
into a Python string. Can anyone tell me how this is done? If it helps,
I know the number of characters in the string.
我认为你的意思是NUL,而不是null。
你收到了什么,如果它不是一个Python字符串?
您可能需要/想要这个:
如果strg [-1] ==" \0":
strg = strg [: - 1]
或者:
strg = strg.rstrip(" \0")#需要Python 2.2.2或更高版本
您可能正在谈论一个固定长度的字符串
包含useful_stuff +" \0" +填充 - 在这种情况下你需要
strg = strg.split(" \0")[0]#抓住(但不包括)第一个
NUL(如果有的话)
如果你不确定你有什么,请打印repr(the_input_string)
HTH,
John
I think you mean NUL, not null.
What have you received it into, if it''s not a Python string?
You probably need/want this:
if strg[-1] == "\0":
strg = strg[:-1]
alternatively:
strg = strg.rstrip("\0") # requires Python 2.2.2 or later
It''s possible you may be talking about a fixed length string which
contains useful_stuff + "\0" + padding -- in that case you need
strg = strg.split("\0")[0] # grab upto (but not including) the first
NUL (if any)
If you''re not sure what you''ve got, print repr(the_input_string)
HTH,
John
这篇关于如何将null(0)终止的字符串转换为Python字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!