Subject: | |
From: | |
Reply To: | |
Date: | Tue, 14 Oct 1997 14:44:30 -0400 |
Content-Type: | text/plain |
Parts/Attachments: |
|
|
>Cynthia Redick ([log in to unmask]) wrote:
>: We are looking for a "C" language routine that would format a c data ty=
>: pe in
>: such a way that it could be used to update a cobol comp-3 field, or an =
>: image
>: P(x) field. Thanks in advance!
>
Here's a sample routine that I used on a project for both the 3000 and 9000.
Hope it helps.....
Barry Lemrow
Hewlett-Packard
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define BTOD(d,i) ((i&1) ? ((d[i/2]) & 0xf) : ((d[i/2] >> 4) & 0xf))
/*
*
*
*
*************************************************************************/
int str_to_packed
( char *buffer,
unsigned char *packed_field,
int *packed_size )
{
int decimal_found = 0;
int i;
int n_offset;
int num;
int num_nibbles = 1; /* There is always a nibble for the sign
*/
int sign_type;
unsigned char *pptr;
pptr = packed_field;
*packed_size = 0;
/*
* First let's check the begining or end of the string for a sign.
*********************************************************************/
if ( buffer[ strlen ( buffer ) - 1 ] == '-' || buffer[ 0 ] == '-' )
sign_type = 0xd;
else if ( buffer[ strlen ( buffer ) - 1 ] == '+' || buffer[ 0 ] == '+' )
sign_type = 0xc;
else
sign_type = 0xf;
/*
* Walk down the character string to count the actual number of
* digits and to check that we have an acceptable list of tokens.
*******************************************************************/
for ( i = 0; i < strlen ( buffer ); i++ )
if ( buffer[ i ] == '+' || buffer[ i ] == '-' )
/*
* The sign tokens only valid at the begining or end
* of the number.
******************************************************/
if ( i == 0 || i == strlen ( buffer ) )
continue;
else
return -1;
else if ( buffer[ i ] == ',' )
/*
* Ignore commas
*******************/
continue;
else if ( buffer[ i ] == '.' )
if ( decimal_found == 0 )
++decimal_found;
else
return -1; /* Error, there already was a decimal point */
else
if ( isdigit ( buffer[ i ] ) )
++num_nibbles;
else
/*
* Invalid character found!!
*******************************/
return -1;
/*
* Packed numbers are always on a byte boundry, so if there is
* an odd count of nibbles we will add a leading "slack" or filler
* nibble of zeros to the begining of the number.
********************************************************************/
if ( num_nibbles % 2 )
{
packed_field = 0 << 4; /* add the zeroed slack nibble */
n_offset = 1;
++num_nibbles;
}
else
n_offset = 0;
/*
* The buffer is syntatically correct, so walk through it
* again to perform the conversion.
***********************************************************/
for ( i = 0; i < strlen ( buffer ); i++ )
if ( isdigit ( buffer[ i ] ) )
{
num = buffer[ i ] - '0';
if ( n_offset & 1 )
/* we are processing the right side of the byte here so OR the bits */
packed_field[ n_offset / 2 ] = packed_field[ n_offset / 2 ] | num;
else
/* we are processing the left side of the byte so insert the number
* and perform a LSL (logical shift left) of four bits.
*********************************************************************/
packed_field[ n_offset / 2 ] = num << 4;
++n_offset;
}
/*
* Now add the sign information to the last nibble.
*****************************************************/
if ( n_offset & 1 )
packed_field[ n_offset / 2 ] = packed_field[ n_offset / 2 ] | sign_type;
else
packed_field[ n_offset / 2 ] = sign_type << 4;
*packed_size = num_nibbles;
return 0;
}
/*
*
*
*
*************************************************************************/
int main
( int argc,
char *argv[ ] )
{
char buffer[ 256 ];
int i;
int packed_size;
unsigned char packed_field[ 20 ];
unsigned char *packed_ptr;
if ( argc == 1 )
{
printf ( "Enter a number:" );
if ( fgets ( buffer, sizeof ( buffer ), stdin ) == NULL )
exit ( 0 );
else
buffer[ strlen ( buffer ) - 1 ] = '\0'; /* Remove newline */
}
else
strcpy ( buffer, *++argv );
if ( strlen ( buffer ) == 0 )
exit ( 0 );
printf ( "buffer = %s\n", buffer );
if ( str_to_packed ( buffer, packed_field, &packed_size ) == 0 )
{
packed_ptr = packed_field;
printf ( "packed field result\n" );
printf ( "-------------------\n" );
for ( i = 0; i < packed_size; i++ )
printf ( "%02x ", ( BTOD ( packed_field, i ) ) );
printf ( "\n" );
}
else
printf ( "invalid numeric format\n" );
}
|
|
|