Skip to content

Commit

Permalink
Trying this simple change...
Browse files Browse the repository at this point in the history
  • Loading branch information
EricEngle-NOAA committed Jan 21, 2025
1 parent 63116bb commit f51770c
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/g2cio.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int
g2c_file_io(FILE *f, int write, int g2ctype, void *var)
{
void *void_be;
char *bvar = NULL;
signed char *bvar = NULL;
short *svar = NULL;
int *ivar = NULL;
long long int *i64var = NULL;
Expand All @@ -64,7 +64,15 @@ g2c_file_io(FILE *f, int write, int g2ctype, void *var)
void_be = &byte_be;
if (write)
{
byte_tmp = *bvar;
/* Are we writing a negative number? */
if (g2ctype == G2C_BYTE && *bvar < 0)
{
byte_tmp = -1 * *bvar; /* Store as positive. */
byte_tmp |= 1UL << BITSHIFT_7; /* Set sign bit. */
}
else
byte_tmp = *bvar;

/* Convert result to big-endian. */
byte_be = byte_tmp;
}
Expand Down Expand Up @@ -150,6 +158,13 @@ g2c_file_io(FILE *f, int write, int g2ctype, void *var)
case G2C_UBYTE:
/* No conversion needed for one-byte values. */
*bvar = byte_be;

/* Did we read a negative number? Check the sign bit... */
if (g2ctype == G2C_BYTE && *bvar & 1 << BITSHIFT_7)
{
*bvar &= ~(1UL << BITSHIFT_7); /* Clear sign bit. */
*bvar *= -1; /* Make it negative. */
}
break;
case G2C_SHORT:
case G2C_USHORT:
Expand Down

0 comments on commit f51770c

Please sign in to comment.