Java Integer.ParseInt translation to python
jose isaias cabrera
jicman at cinops.xerox.com
Tue Feb 8 19:42:59 EST 2005
Ok, so this,
>> buffer[0] = (byte)Integer.parseInt(string,16);
in java is, partly, this
buffer[0] = int(string, 16)
in python. But here is my problem. When I call this java subroutine,
byte[] decodeKey(String inString)
{
if (inString == null)
return null;
System.out.println("StringLength = " + inString.length());
byte[] retBuf = new byte[inString.length()/2];
// The string has two hex characters per byte.
for (int index = 0; index < retBuf.length; index++)
{
System.out.print(inString.substring(2*index, 2*index+2));
System.out.println(" " +
Integer.parseInt(inString.substring(2*index, 2*index+2), 16) + " " +
(byte)Integer.parseInt(inString.substring(2*index, 2*index+2), 16));
retBuf[index] =
(byte)Integer.parseInt(inString.substring(2*index, 2*index+2), 16);
}
System.out.println(retBuf);
return retBuf;
}
I get this output:
StringLength = 40
c1 193 -63
7c 124 124
e1 225 -31
86 134 -122
ab 171 -85
94 148 -108
ee 238 -18
b0 176 -80
de 222 -34
8a 138 -118
e3 227 -29
b5 181 -75
b7 183 -73
51 81 81
a7 167 -89
c4 196 -60
d8 216 -40
e9 233 -23
ed 237 -19
eb 235 -21
[B at 1616c7
But, here is what I have for python,
def PrepareHash(HashStr):
while len(HashStr) > 0:
byte = HashStr[0:2]
print byte,int(byte,16),byte(int(byte,16)) # & 0xff
HashStr = HashStr[2:]
return byte
def Main():
HashStr = "c17ce186ab94eeb0de8ae3b5b751a7c4d8e9edeb"
HashStr = PrepareHash(HashStr)
print "Prepared HashStr :",HashStr
Main()
and it results to,
mulo 19:32:06-> python test.py
c1 193 Á
7c 124 |
e1 225 á
86 134
ab 171 «
94 148
ee 238 î
b0 176 °
de 222 Þ
8a 138
e3 227 ã
b5 181 µ
b7 183 ·
51 81 Q
a7 167 §
c4 196 Ä
d8 216 Ø
e9 233 é
ed 237 í
eb 235 ë
which is not even close, and yes, I know that it's not the same code. So,
the question is, how can I make this java (byte) call in python? so that the
result would be the right one, "[B at 1616c7"
Thanks.
josé
More information about the Python-list
mailing list