Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How do I convert COBOL Comp-3 packed to ASCII?
Message
De
07/03/2003 10:59:46
 
 
À
06/03/2003 19:51:39
James Hansen
Canyon Country Consulting
Flagstaff, Arizona, États-Unis
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00762419
Message ID:
00762747
Vues:
23
>If all the numbers are 6 bytes long as in your example, then if you read in six bytes into a string variable S, you could decode the value into Y as:
>
>
>Y = 0
>for  I = 2 to 6
>   C = asc(substr(S,I,1))
>   Y = (Y * 10 + bitrshift(C,4)) * 10 + bitand(C,255)
>endfor
>Y = Y / 100
>if  left(S,1) = chr(01)
>   Y = - Y
>endif
>There may be a more elegant solution, but that one should work for brute force.
>
>...Jim
>Long-standing member of BPA (Bit Pickers Anonymous)


Well, after careful thought I realize that I was trying too hard to come up with a generic algorithm, that fit all cases abd all lengths. Your example did not work for me but gave me the idea to simplify.

I have a fixed length, so I all I need is to keep shifting numbers:
* sample string (should translate to $31.05)
S = CHR(02)+CHR(00)+CHR(00)+CHR(00)+CHR(31)+CHR(05)
Y = 0
for I = 2 to 6
   C = asc(substr(S,I,1))
   * just shift the previous accumulated result to the left
   Y = (Y * 100) + C
endfor
Y = Y / 100
if left(S,1) = chr(01)
   Y = - Y
ENDIF
So, your message got me thinking.
I must be getting old, not seeing how simple it was, and trying to do it the hard way. Thanks.


Alex Feldstein, MCP, Microsoft MVP
VFP Tips: English - Spanish
Website - Blog - Photo Gallery


"Once again, we come to the Holiday Season, a deeply religious time that each of us observes, in his own way, by going to the mall of his choice." -- Dave Barry
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform