473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

COnverting integer hex format into strings

Hi everybody,

I am programming an application to send data using UDP sockets with Python 3.1.

The command socket.send requires data in bytes format.

The problem I am having is that the package I have to send has three different fields, the first one contains a 16 bits integer variable (c_ushort) and so does the second field whereas the third one is an string whose length can go up to 900 characters.

I decided then to create an struct that contains these three fields:
Expand|Select|Wrap|Line Numbers
  1. class PHAL_msg(Structure):
  2.     _fields_ = [("Port", c_ushort),
  3.                 ("Size", c_ushort),
  4.                 ("Text", c_wchar_p)]
I would expect I could send this object by just converting it to a bytes object:

Expand|Select|Wrap|Line Numbers
  1. Msg_TX = PHAL_msg(Port=PHAL_ADDRESS, Size=PAYLOAD_SIZE, Text='HELLO woRLD!')
  2.  
  3. socket.send(bytes(Msg_TX))
, but it does not work.

Any idea how this could be done?

Regards
Aug 31 '10 #1
2 2154
Banfa
9,065 Expert Mod 8TB
Define "doesn't work" ...

Are you sure your code produces an array of bytes to be sent?
Are you sure your code has correctly set up the socket?
How do you receive the data at the destination computer?
Do you receive nothing or do you receive something but it appears to be wrong?
Are there and endian issues to be taken into account?
Are you actually transmitting across a network, as opposed to sending an receiving on the same machine? If you are have you used a utility like WireShark to monitor network traffic while you are trying to send?
Sep 2 '10 #2
dwblas
626 Expert 512MB
You also want to print bytes(Msg_TX) and see if it is what you expect.
Sep 3 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: marduk | last post by:
I have a weird request. I want to be able to say def myvalues(): while True: # stuff that determines a new somevalue yield somevalue x = "Hello, %s, this is a %s with %s and %s on top of...
4
by: grv575 | last post by:
For a string like print '%.*f' % (prec_length, float_var) How does python convert this to a C printf sytle format string? Is there a way to specify the precision length for floating points in C...
0
by: Mike | last post by:
can anyone explain to me why, given: decimal dec = 84.50M; Convert.ToInt32(dec).ToString() and Math.Round(dec,0).ToString() both correctly output "84"
5
by: dank | last post by:
http://msdn2.microsoft.com/en-us/library/tcxf1dw6.aspx describes the integer format modifiers accepted by Microsoft's printf. ...
1
by: Dennis Benzinger | last post by:
Is there a library with a strftime replacement which supports Unicode format strings? Bye, Dennis
9
by: Eric_Dexter | last post by:
http://www.ddj.com/184405774;jsessionid=BDDEMUGJOPXUMQSNDLQCKHSCJUNN2JVN I saw a warning from homeland security about this. I only comment on the because I am trying to use os.system('command1...
7
by: Carroll, Barry | last post by:
Greetings: Personally, I don't think top-posting is the most annoying newsgroup habit. I think it's making a big fuss about minor inconveniences. One of the nicest things about being human...
7
by: Michael Howes | last post by:
MSDN documentation for format strings seems ambiguous. It's says things like "significant digits or number of decimal places" all over the place. Well those two things are different. How do I...
3
by: nvx | last post by:
Hi, I'm looking for a simple way to convert a Double to a String exactly the .ToString() does, except these two differences: - the number of decimals is given (rounding is applied if necessary),...
2
by: joyjignesh | last post by:
hi i have make a report with mshflexgrid. the report between two date. when i have written query .open"select * from tablename where t_date>=convert(datetime,' " & text1.text &"') and ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.