473,387 Members | 1,891 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.

Hexadecimal list conversion

Hi All.

I have a list which is a line from a file:
['\x003\x008\x001\x004\x007\x005\x00.\x005\x000\x00 2\x005\x009\x009\x00',
'\x002\x001\x003\x006\x002\x002\x00.\x001\x007\x00 4\x002\x008\x002\x00']

This should be in the format:
['381475.502599', '213622.174282']

I've tried a few options using replace (replacing "\x00" with "") and
trying to convert from hexademical to decimal.

But nothing has worked. Can anybody give any tips to help?

Thanks.

Dec 20 '07 #1
7 4730
On Dec 20, 12:30 pm, Neil Webster <nswebs...@gmail.comwrote:
Hi All.

I have a list which is a line from a file:
['\x003\x008\x001\x004\x007\x005\x00.\x005\x000\x00 2\x005\x009\x009\x00',
'\x002\x001\x003\x006\x002\x002\x00.\x001\x007\x00 4\x002\x008\x002\x00']

This should be in the format:
['381475.502599', '213622.174282']

I've tried a few options using replace (replacing "\x00" with "") and
trying to convert from hexademical to decimal.

But nothing has worked. Can anybody give any tips to help?
Is your file utf-16 (that would explain why your file has \x00 in
between every character)? If so, use codecs.open to read it, and you
won't get the \x00's (you'll get a unicode string).

Or you can remove them using replace:

a = a.replace('\x00', '')

HTH
--
Paul Hankin
Dec 20 '07 #2
Hi All.

I have a list which is a line from a file:
['\x003\x008\x001\x004\x007\x005\x00.\x005\x000\x00 2\x005\x009
\x009\x00',
'\x002\x001\x003\x006\x002\x002\x00.\x001\x007\x00 4\x002\x008\
x002\x00']

This should be in the format:
['381475.502599', '213622.174282']

I've tried a few options using replace (replacing "\x00" with "") and
trying to convert from hexademical to decimal.

But nothing has worked. Can anybody give any tips to help?

Thanks.
Somthing like:

line =
['\x003\x008\x001\x004\x007\x005\x00.\x005\x000\x00 2\x005\x009\x009\x00'
,
'\x002\x001\x003\x006\x002\x002\x00.\x001\x007\x00 4\x002\x008\x002\x00']

result = [''.join(x.split('\x00')) for x in line]

Cheers,

Drea
Dec 20 '07 #3
En Thu, 20 Dec 2007 09:30:14 -0300, Neil Webster <ns*******@gmail.com>
escribi�:
I have a list which is a line from a file:
['\x003\x008\x001\x004\x007\x005\x00.\x005\x000\x00 2\x005\x009\x009\x00',
'\x002\x001\x003\x006\x002\x002\x00.\x001\x007\x00 4\x002\x008\x002\x00']

This should be in the format:
['381475.502599', '213622.174282']

I've tried a few options using replace (replacing "\x00" with "") and
trying to convert from hexademical to decimal.
The replace works:

pyfor item in L:
.... print item.replace('\x00','')
....
381475.502599
213622.174282

If you got that from a file, I bet you read it using the wrong encoding.
Try opening the file using codecs.open("filename", "rb",
encoding="utf-16-be") instead of plain open. When your read it, you'll get
unicode objects instead of strings, but with the right contents. If you
wish you can convert to strings using
line_read.encode(your_system_encoding); if all your data is numeric the
encoding used is irrelevant and can be omited.

--
Gabriel Genellina

Dec 20 '07 #4

"Gabriel Genellina" <ga*******@yahoo.com.arwrote in message
news:ma***************************************@pyt hon.org...
En Thu, 20 Dec 2007 09:30:14 -0300, Neil Webster <ns*******@gmail.com>
escribi�:
>I have a list which is a line from a file:
['\x003\x008\x001\x004\x007\x005\x00.\x005\x000\x00 2\x005\x009\x009\x00',
'\x002\x001\x003\x006\x002\x002\x00.\x001\x007\x0 04\x002\x008\x002\x00']

This should be in the format:
['381475.502599', '213622.174282']

I've tried a few options using replace (replacing "\x00" with "") and
trying to convert from hexademical to decimal.

The replace works:

pyfor item in L:
... print item.replace('\x00','')
...
381475.502599
213622.174282

If you got that from a file, I bet you read it using the wrong encoding.
Try opening the file using codecs.open("filename", "rb",
encoding="utf-16-be") instead of plain open. When your read it, you'll get
unicode objects instead of strings, but with the right contents. If you
wish you can convert to strings using
line_read.encode(your_system_encoding); if all your data is numeric the
encoding used is irrelevant and can be omited.

--
Gabriel Genellina
There is an odd number of bytes in each string. Each begins and ends with
\x00, so it doesn't look like utf-16-be. But replace works:
>>L=['\x003\x008\x001\x004\x007\x005\x00.\x005\x000\x00 2\x005\x009\x009\x00','\x002\x001\x003\x006\x002\x 002\x00.\x001\x007\x004\x002\x008\x002\x00']
[s.replace('\x00','') for s in L]
['381475.502599', '213622.174282']

-Mark Tolonen

Dec 20 '07 #5
Mark T wrote:
"Gabriel Genellina" <ga*******@yahoo.com.arwrote in message
>If you got that from a file, I bet you read it using the wrong
encoding. Try opening the file using codecs.open("filename", "rb",
encoding="utf-16-be") instead of plain open.
There is an odd number of bytes in each string. Each begins and ends
with \x00, so it doesn't look like utf-16-be.
I think Gabriel is right. The OP probably butchered the original structure
with

open(filename).read().split("\n")

Peter
Dec 20 '07 #6
On Dec 21, 2:51 am, Peter Otten <__pete...@web.dewrote:
Mark T wrote:
"Gabriel Genellina" <gagsl-...@yahoo.com.arwrote in message
If you got that from a file, I bet you read it using the wrong
encoding. Try opening the file using codecs.open("filename", "rb",
encoding="utf-16-be") instead of plain open.
There is an odd number of bytes in each string. Each begins and ends
with \x00, so it doesn't look like utf-16-be.

I think Gabriel is right. The OP probably butchered the original structure
with

open(filename).read().split("\n")
Or he's read the file "normally" and then done
line = lineZAP
where ZAP is one of [:-1], .rstrip(), .rstrip("\n"), etc

However that accounts only for the rightmost trailing \x00. Looks like
each line has been chainsawed with .split(",") or whatever the
original field separator was.

If Gabriel's instructions don't "work" for the OP, the OP should show
us an unambiguous representation of the first few bytes of the
original file, instead of leaving it to guesswork:

print repr(open("the_file", "rb").read()[:200])
Dec 20 '07 #7
En Thu, 20 Dec 2007 12:51:33 -0300, Peter Otten <__*******@web.de>
escribió:
Mark T wrote:
>"Gabriel Genellina" <ga*******@yahoo.com.arwrote in message
>>If you got that from a file, I bet you read it using the wrong
encoding. Try opening the file using codecs.open("filename", "rb",
encoding="utf-16-be") instead of plain open.
>There is an odd number of bytes in each string. Each begins and ends
with \x00, so it doesn't look like utf-16-be.

I think Gabriel is right. The OP probably butchered the original
structure
with

open(filename).read().split("\n")
Sure! I take bets on this too.

--
Gabriel Genellina

Dec 20 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike Jeffers | last post by:
Hi everyone, I need to convert data from a structure into hexadecimal ascii format. The structure is like this: struct ROOM_DATA { short room_number; short floor_number; long total_area;
10
by: pavithra.eswaran | last post by:
Hi, I would like to convert a single precision hexadecimal number to floating point. The following program seems to work fine.. But I do not want to use scanf. I already have a 32 bit hexadecimal...
5
by: Damon | last post by:
I'm getting '', hexadecimal value 0x02, is an invalid character when I'm deseralizing XML from a 3rd party XML gateway. How do I get rid of these hexadecimal values before I deserialize? Cheers...
2
by: akash deep batra | last post by:
hi i want to convert a 96 bit binary number into a hexadecimal number. e.g binary number= 001100010001010000100101011110111111010101110100010110000101011000101010000000000000000000000000 how...
8
by: Vijay | last post by:
Hi , I am doing a small project in c. I have a Hexadecimal file and want to convert into ascii value. (i.e., Hexadecimal to Ascii conversion from a file). Could anyone help me? Thanks in...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
6
by: Andrea | last post by:
Hi, suppose that I have a string that is an hexadecimal number, in order to print this string I have to do: void print_hex(unsigned char *bs, unsigned int n){ int i; for (i=0;i<n;i++){...
6
by: sweeet_addiction16 | last post by:
hello Im writin a code in c... can sum1 pls help me out in writing a c code to convert decimalnumber to hexadecimal number.The hexadecimal number generated has to be an unsigned long.
11
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've a question regarding Hexadecimal to binary conversion. Suppose I have a long hexadecimal string, and I would like to convert it to a binary string. How can I...
14
by: Ellipsis | last post by:
Ok so I am converting to hexadecimal from decimal and I can only cout the reverse order of the hexadecimal?! How could I reverse this so its the right order? Heres my code: #include <iostream>...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
jinu1996
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...

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.