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 21 25404
Michael wrote:
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
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 wrote:
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.
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
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 wrote:
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.
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
Michael top-posted [corrected]:
John Machin wrote:
Michael wrote:
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.
>
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
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?
My responses are correct. Your "clarificat ion" indicates to me that you
are going by what you are told, not by inspection of (several instances
of) the packet contents, using repr(). It's up to you whether you want
to be skeptical about the packet contents or not. I certainly wouldn't
be throwing the last byte away without checking that it was in fact a
NUL.
Cheers,
John
John,
Thanks for your reply. Just wondering... how are Python strings
formatted? Evidently they're not 0 terminated.
Thanks again,
MDM
John Machin wrote:
Michael top-posted [corrected]:
John Machin wrote:
Michael wrote:
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.
>
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
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?
My responses are correct. Your "clarificat ion" indicates to me that you
are going by what you are told, not by inspection of (several instances
of) the packet contents, using repr(). It's up to you whether you want
to be skeptical about the packet contents or not. I certainly wouldn't
be throwing the last byte away without checking that it was in fact a
NUL.
Cheers,
John
Michael top-posted [again]:
>
John Machin wrote:
Michael top-posted [corrected]:
John Machin wrote:
Michael wrote:
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.
>
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
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?
My responses are correct. Your "clarificat ion" indicates to me that you
are going by what you are told, not by inspection of (several instances
of) the packet contents, using repr(). It's up to you whether you want
to be skeptical about the packet contents or not. I certainly wouldn't
be throwing the last byte away without checking that it was in fact a
NUL.
Cheers,
John
John,
Thanks for your reply. Just wondering... how are Python strings
formatted? Evidently they're not 0 terminated.
A Python string is an object. The details of the internal storage may
vary between implementations . CPython has 8-bit str objects and 16-bit
or 32-bit Unicode objects. In IronPython, (str is Unicode) is true, and
they are 16 bits. In any case the object knows its own length without
having to scan for a terminator. Thus, a string can contain NULs.
Having said all that, the CPython str implementation does have an
additional byte at the end; this is set to zero and is not counted in
the length. However you never see that and don't really need to know
unless you are writing an extension module in C -- it's handy to know
that you don't have to append a NUL if you want to call a C library
function.
Cheers,
John
Michael wrote:
Thanks for your reply. Just wondering... how are Python strings
formatted? Evidently they're not 0 terminated.
have you tried *printing* the thing you got via UDP?
to get a programmer-friendly representation of an arbitrary object, use
print repr(obj)
(where obj is your string, in this case).
</F>
Fredrik Lundh wrote:
Michael wrote:
Thanks for your reply. Just wondering... how are Python strings
formatted? Evidently they're not 0 terminated.
have you tried *printing* the thing you got via UDP?
to get a programmer-friendly representation of an arbitrary object, use
print repr(obj)
(where obj is your string, in this case).
Probably not; there was no indication after the two messages where I
mentioned repr :-)
John,
Since I'm new to Python, I'm having trouble understanding what this
means (see below). Would appreciate any help.
if strg[-1] == "\0":
strg = strg[:-1]
Thanks,
MDM
John Machin wrote:
Fredrik Lundh wrote:
Michael wrote:
Thanks for your reply. Just wondering... how are Python strings
formatted? Evidently they're not 0 terminated.
have you tried *printing* the thing you got via UDP?
to get a programmer-friendly representation of an arbitrary object, use
print repr(obj)
(where obj is your string, in this case).
Probably not; there was no indication after the two messages where I
mentioned repr :-)
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David Mathog |
last post by:
Every so often one of my fgets() based programs encounters
an input file containing embedded nulls. fgets is happy to
read these but the embedded nulls subsequently cause problems
elsewhere in the program. Since fgets() doesn't return
the number of characters read it is pretty tough to handle
the embedded nulls once they are in the buffer....
|
by: ehabaziz2001 |
last post by:
Hi,
Till now I do not understand how the null character automatically added
to the end of the string and it is not an element of the string array .
All books said the null character (\0) added automatically to the end
of the string.
Let say
char name="123456789"
If entered the name in a loop;
|
by: Matt Helm |
last post by:
I am using win32com to access a third party COM interface but am
having trouble using the string that is returned.
The vendor's docs show the following method:
HRESULT CookString(BSTR param_a, short buf_size, BSTR* result_b);
param_a is a string to be processed.
buf_size is the size of the returned string + 1.
|
by: shearichard |
last post by:
Hi - I have written some python to insert a row into a table using
MySQLDB. I have never before written SQL/Python using embedded
parameters in the SQL and I'm having some difficulties. Could someone
point me in the right direction please ?
The python looks like this :
import MySQLdb
import MySQLdb.cursors
conn =...
|
by: Steve Barnett |
last post by:
I need to call a Win32 function that returns a set of string values. Each
value is terminated by a null and the last value is terminated by two nulls.
How do I code for this in C#?
The call is:
internal static extern uint QueryDosDevice(string lpDeviceName,
string lpTargetPath,uint ucchMax);
According to the help, if lpDeviceName is...
| |
by: semut |
last post by:
Given that the string is of null terminated type. What could be the
possible causes (by experience) the string to have no null terminated
and cause buffer overflow later. I know it is quite broad, just like to
find out the causes as much as possible so that I could impose stricter
checking toward my codes.
note: I could not use std::string...
|
by: semut |
last post by:
Given that the string is of null-terminated type. What could be the
possible causes (by experience) the string to have no null character
(\0)
and cause buffer overflow later. I know it is quite broad, just like to
find out the causes as much as possible so that I could impose stricter
checking toward my codes.
note: I could not use...
|
by: kpoman |
last post by:
Hi to all,
I am trying to use some dll which needs some data types that I can't
find in python. From the dll documentation, I am trying to use this:
HRESULT IMKWsq::Compress ( VARIANT rawImage,
short sizeX,
short sizeY,
VARIANT * wsqImage,
short * result
)
|
by: ssylee |
last post by:
If I'm being supplied with a char* that is not null-terminated, is it
impossible to transform it into a null terminated char* with only
abstract information about the char* information?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |