Help - Search - Members - Calendar
Full Version: WOT: ARRGGHHH, i hate fixing someone elses code!
914World.com > The 914 Forums > 914World Garage
SirAndy
this was supposed to be a simple stripped down BASE64 decoder done in java.

i don't (can't) use a lot of standard libraries because this App will be run on PDAs and java enabled cellphones, so SIZE does matter.

no problem, i get one of my contractors the BASE64 specs and some sample implementations ...

he's been fighting with this for 3 days now so i get his sourcefile and have a look at it this morning.
looks OK at first glance, but when you run it, it sporadically spits out wrong bytevalues while some decode just fine.
wtf?
so i spend all day going through his code (which is not that much) and i just can't figure out what's wrong.

then finally, about 15 minutes ago it dawns on me, have a look at this:

bReturn2[nn+2] = (byte)(iRes % 0xFF);
bReturn2[nn+1] = (byte)((iRes >> 8) % 0xFF);
bReturn2[nn+0] = (byte)((iRes >> 16) % 0xFF);


bReturn2 is a byte array, iRes a integer, the purpose is to roll the lower 3 bytes of the integer into 3 sequential fields in the byte array.

i had been staring at this for 6 hours and couldn't figure out what was wrong.
all the while it was right in front of me, plain sight, right there ... headbang.gif

can you find the error?
idea.gif Andy

PS: did i mention that i HATE going through someone else code?
SpecialK
I have NFI, but I'd guess you're missing a parenthesis in the first line........but then again....I have NFI. confused24.gif

Get your broken off allen wrench out?
Midtowner
Sounds all Japanese to me! smile.gif
Joe Bob
Or ya got extra ones....parenthesis that is....
Foxman
bReturn2[nn+2] = (byte)(iRes % 0xFF);

What are you calculating the remainder of? Are you dividing iRes and 0XFF somewhere else? It is hard to say without seeing the rest of the function.
johnmhudson111
QUOTE (Buzzard1 @ Aug 15 2005, 09:02 PM)
I'd guess you're missing a parenthesis in the first line.......

agree.gif Just a WAG since I have no coding ability, just an analyst by nature and that seems to be the missing pattern.
SirAndy
QUOTE (johnmhudson111 @ Aug 15 2005, 07:29 PM)
Just a WAG since I have no coding ability, just an analyst by nature and that seems to be the missing pattern.

nope, nothing missing, syntax is just fine, compiles and runs without an error ...

wink.gif Andy
SirAndy
QUOTE (Foxman @ Aug 15 2005, 07:29 PM)
What are you calculating the remainder of? Are you dividing iRes and 0XFF somewhere else? It is hard to say without seeing the rest of the function.

this takes a 4 byte integer and zeros out all bits except the lowest 8 (0xff) and then stores those 8 bit in an byte-array.

1st line has no shift to get 1st byte
2nd line has 8 bit shift to get 2nd byte
3rd line has 16 bit shift to get 3rd byte

that all is correct, but he still managed to f&%$ it up ...
dry.gif Andy
johnmhudson111
QUOTE (SirAndy @ Aug 15 2005, 09:32 PM)
nope, nothing missing, syntax is just fine, compiles and runs without an error ...

wink.gif Andy

bReturn2[nn+2] = (byte)((iRes >> 4) % 0xFF);
bReturn2[nn+1] = (byte)((iRes >> 8) % 0xFF);
bReturn2[nn+0] = (byte)((iRes >> 16) % 0xFF);

Nother WAG, I give up after this one happy11.gif
jonwatts
I would have used the '&' operator instead of the '%' operator, but that's just me.

r_towle
QUOTE (SirAndy @ Aug 15 2005, 08:33 PM)
this was supposed to be a simple stripped down BASE64 decoder done in java.

i don't (can't) use a lot of standard libraries because this App will be run on PDAs and java enabled cellphones, so SIZE does matter.

no problem, i get one of my contractors the BASE64 specs and some sample implementations ...

he's been fighting with this for 3 days now so i get his sourcefile and have a look at it this morning.
looks OK at first glance, but when you run it, it sporadically spits out wrong bytevalues while some decode just fine.
wtf?
so i spend all day going through his code (which is not that much) and i just can't figure out what's wrong.

then finally, about 15 minutes ago it dawns on me, have a look at this:

bReturn2[nn+2] = (byte)(iRes % 0xFF);
bReturn2[nn+1] = (byte)((iRes >> 8) % 0xFF);
bReturn2[nn+0] = (byte)((iRes >> 16) % 0xFF);


bReturn2 is a byte array, iRes a integer, the purpose is to roll the lower 3 bytes of the integer into 3 sequential fields in the byte array.

i had been staring at this for 6 hours and couldn't figure out what was wrong.
all the while it was right in front of me, plain sight, right there ... headbang.gif

can you find the error?
idea.gif Andy

PS: did i mention that i HATE going through someone else code?

bReturn2[nn+0] = (byte)(iRes % 0xFF);
bReturn2[nn+1] = (byte)((iRes >> 8) % 0xFF);
bReturn2[nn+2] = (byte)((iRes >> 16) % 0xFF);
SirAndy
QUOTE (jonwatts @ Aug 15 2005, 07:37 PM)
I would have used the '&' operator instead of the '%' operator, but that's just me.

we've got a winner! smilie_pokal.gif

he used the "%" operator which is MODULO instead of "&" which is bitwise AND ...

took me 6 friggin hours to find that one!
headbang.gif Andy

here's the correct code snippet:

bReturn2[nn+2] = (byte)(iRes & 0xFF);
bReturn2[nn+1] = (byte)((iRes >> 8) & 0xFF);
bReturn2[nn+0] = (byte)((iRes >> 16) & 0xFF);
jonwatts
Dumb question. Is your input base10 or base16? It doesn't matter to the compiler but it makes a difference to us humans.

ematulac
edit: nevermind ... modulus looked strange to me there, but it didn't register.
jonwatts
WOW, and I am such a suck-ass programmer too, but I do have to live in byte-stream land on a daily basis so maybe that helps.

I'd like to thank the Academy, my agent, and Andy for the beautiful 944 CV's, axles, and powder coated trailing arms (or do I just win an attaboy?).

SirAndy
QUOTE (ematulac @ Aug 15 2005, 08:08 PM)
He's storing the bytes in the wrong order in the array.

nope, java integer is reverse byte order, so that part was correct ...

jon nailed it, see post above!
type.gif Andy
Foxman
damn, I never thought about the input.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2024 Invision Power Services, Inc.