473,320 Members | 2,202 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,320 software developers and data experts.

How do I convert a string of length four into a decimal value?

I am trying to convert a 4 letter string (that can be of any character on the ASCII chart) into decimal values. For example:

- bean which has a hex value of 62 65 61 6E should have a value of 6736203616366.

- I know about ord() but that only works for single characters.

Any help is appreciated :].
Feb 11 '10 #1

✓ answered by Glenton

Okay. So you haven't included the code from my preceding post.
Try this:
Expand|Select|Wrap|Line Numbers
  1. offsetraw = file.read(4)
  2. offset = offsetraw[::-1]
  3. hexoffset=''
  4. for letter in offset:
  5.     temp=hex(ord(letter))[2:]
  6.     if len(temp)==1:
  7.         temp="0"+temp
  8.     hexoffset += temp
  9. offsetvalue = int(hexoffset, 16)
  10. print hexoffset
  11. print offsetvalue,
  12.  

18 2651
Glenton
391 Expert 256MB
The ease of breaking up a string into it's component characters is one of the things that makes python awesome.

Expand|Select|Wrap|Line Numbers
  1. In [1]: s="bean"
  2.  
  3. In [2]: for c in s:
  4.    ...:     print ord(c),
  5.    ...:     
  6.    ...:     
  7. 98 101 97 110
  8.  
Feb 11 '10 #2
Glenton
391 Expert 256MB
By the way, I can't figure out where 6736203616366 comes from?!? What is it?
Feb 11 '10 #3
http://www.easycalculation.com/hex-converter.php

The value comes from the hex representation of the string I included with the original post.

Oh whoops, I forgot to remove spaces. I've been exhausted of all my energy today. xD

1650811246 is the value it should be.

Thanks for your help, I really appreciate it :].
Feb 11 '10 #4
bvdet
2,851 Expert Mod 2GB
Can you explain how you get the decimal value "1650811246" from "62 65 61 6E"?
Feb 11 '10 #5
Copy and paste "6265616E" into the website http://www.easycalculation.com/hex-converter.php.

Which converts the hexadecimal representation of "bean" into a decimal value which is 1650811246 according to the website.

I hope I am writing this correctly.
Feb 11 '10 #6
bvdet
2,851 Expert Mod 2GB
Now I get it.
Expand|Select|Wrap|Line Numbers
  1. >>> ''.join([hex(ord(letter))[2:] for letter in 'bean'])
  2. '6265616e'
  3. >>> int('6265616e', 16)
  4. 1650811246
  5. >>> 
Feb 11 '10 #7
Wow, thanks it worked! There is a bit of a glitch with it though.

07079533 should be 117937459 but it is 7836979 which in hex is 779533.

Anyway to also include the zeros? What I am doing is reading four characters and outputting them into decimal numbers.
Feb 11 '10 #8
bvdet
2,851 Expert Mod 2GB
What word gives you hex values of "07 07 95 33"?
Feb 11 '10 #9
Oh its not a word, the program I am making also converts special characters and other things like NUL characters also. I just used the above hex representation as a example of that.

I really appreciate your help :].
Feb 11 '10 #10
bvdet
2,851 Expert Mod 2GB
The code I posted should work for special characters. Function hex() takes an integer and returns a string.
Expand|Select|Wrap|Line Numbers
  1. >>> hex(4)
  2. '0x4'
  3. >>> hex(ord('\t'))
  4. '0x9'
  5. >>> 
Feb 11 '10 #11
Is there a way for this code to read the hex value as 07 and not 7? The example I posted up gives me a different value.

07 07 95 33 = 117937459
7 7 95 33 = 7836979
Feb 12 '10 #12
Glenton
391 Expert 256MB
Hi

@bvdet will probably have a better way, but here's one way.

Expand|Select|Wrap|Line Numbers
  1. In [12]: s="%2s" %"7"
  2.  
  3. In [13]: s
  4. Out[13]: ' 7'
  5.  
  6. In [14]: s.replace(" ","0")
  7. Out[14]: '07'
  8.  
Feb 12 '10 #13
Just tried this way and it outputs the same thing. Maybe I am just doing it wrong, but it seems that python doesn't see the hex value as 07 compared to 7 which changes the values when more hex values are put in place.

I really appreciate everyones help though :].
Feb 12 '10 #14
Glenton
391 Expert 256MB
Please post your code (using the code tags pls ;D). Difficult to debug when we can't see!

Anyway, you need to be clear what type python is using at each point. Using type(whatever) in the command line will give you an idea of whether it's an integer or string or whatever.

You also need to be clear about what, exactly, your conversion is.
Currently, it seems you want to convert:
characters -> 2-digit hex -> multi-digit hex by concatenation -> integer

Your characters start off as a string (I guess).
The 2-digit hex is a string, but the problem is it's sometimes 1-digit hex. So you could even write something explicit like (supposing hs is your hex in a string format):
Expand|Select|Wrap|Line Numbers
  1. if len(hs)==1:
  2.     hs="0"+hs
At the end of this your hs is two digits. You'd need to be careful: is it possible that len(hs)==0 or len(hs)>=3. If so these cases need to be handled.

Good luck, and pls post your code next time if you want more specific help.
Feb 12 '10 #15
Expand|Select|Wrap|Line Numbers
  1.     offsetraw = file.read(4)
  2.     offset = offsetraw[::-1]
  3.     hexoffset = ''.join([hex(ord(letter))[2:] for letter in offset])
  4.     offsetvalue = int(hexoffset, 16)
  5.     print hexoffset
  6.     print offsetvalue,
I just tried your method and it doesn't solve the problem, I think that way only works with single hex strings.

With my code it reads from a defined file by 4 characters and places that as a string in to a variable. Then flips the data (it must do this), then I used bvdet's code to convert this string to its hex representation and offsetvalue is the value the string should be.

With hexoffset printing what I need to know, it should print 07079533 as the hex representation of the string put in, but what I get is 779533 which is missing the preceding zeros. I hope this is clear enough.

Thanks for your efforts! I really appreciate it. :]
Feb 12 '10 #16
Glenton
391 Expert 256MB
Okay. So you haven't included the code from my preceding post.
Try this:
Expand|Select|Wrap|Line Numbers
  1. offsetraw = file.read(4)
  2. offset = offsetraw[::-1]
  3. hexoffset=''
  4. for letter in offset:
  5.     temp=hex(ord(letter))[2:]
  6.     if len(temp)==1:
  7.         temp="0"+temp
  8.     hexoffset += temp
  9. offsetvalue = int(hexoffset, 16)
  10. print hexoffset
  11. print offsetvalue,
  12.  
Feb 12 '10 #17
Thanks! it works as it should now. Thanks for your support!
Feb 12 '10 #18
bvdet
2,851 Expert Mod 2GB
If you know the hex value will never be more than 2 characters long, you can do this:
Expand|Select|Wrap|Line Numbers
  1. >>> word = "\t\tnd"
  2. >>> ''.join(["%2s" % (hex(ord(letter))[2:]) for letter in word]).replace(" ", "0")
  3. '09096e64'
  4. >>> 
Feb 12 '10 #19

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

Similar topics

4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
7
by: patang | last post by:
I want to convert amount to words. Is there any funciton available? Example: $230.30 Two Hundred Thirty Dollars and 30/100
6
by: patang | last post by:
Could someone please tell me where am I supposed to put this code. Actually my project has two forms. I created a new module and have put the following code sent by someone. All the function...
5
by: Ray | last post by:
I have a table with some audit date and time columns. Problem is the developer who stored the data left them as DECIMAL type instead of DATE and TIME. Is there a way I can convert the DECIMAL type...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
10
by: cmdolcet69 | last post by:
Public ArrList As New ArrayList Public bitvalue As Byte() Public Sub addvalues() Dim index As Integer ArrList.Add(100) ArrList.Add(200) ArrList.Add(300) ArrList.Add(400) ArrList.Add(500)
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.