Skip to content

Commit f51770c

Browse files
Trying this simple change...
1 parent 63116bb commit f51770c

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/g2cio.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int
4141
g2c_file_io(FILE *f, int write, int g2ctype, void *var)
4242
{
4343
void *void_be;
44-
char *bvar = NULL;
44+
signed char *bvar = NULL;
4545
short *svar = NULL;
4646
int *ivar = NULL;
4747
long long int *i64var = NULL;
@@ -64,7 +64,15 @@ g2c_file_io(FILE *f, int write, int g2ctype, void *var)
6464
void_be = &byte_be;
6565
if (write)
6666
{
67-
byte_tmp = *bvar;
67+
/* Are we writing a negative number? */
68+
if (g2ctype == G2C_BYTE && *bvar < 0)
69+
{
70+
byte_tmp = -1 * *bvar; /* Store as positive. */
71+
byte_tmp |= 1UL << BITSHIFT_7; /* Set sign bit. */
72+
}
73+
else
74+
byte_tmp = *bvar;
75+
6876
/* Convert result to big-endian. */
6977
byte_be = byte_tmp;
7078
}
@@ -150,6 +158,13 @@ g2c_file_io(FILE *f, int write, int g2ctype, void *var)
150158
case G2C_UBYTE:
151159
/* No conversion needed for one-byte values. */
152160
*bvar = byte_be;
161+
162+
/* Did we read a negative number? Check the sign bit... */
163+
if (g2ctype == G2C_BYTE && *bvar & 1 << BITSHIFT_7)
164+
{
165+
*bvar &= ~(1UL << BITSHIFT_7); /* Clear sign bit. */
166+
*bvar *= -1; /* Make it negative. */
167+
}
153168
break;
154169
case G2C_SHORT:
155170
case G2C_USHORT:

0 commit comments

Comments
 (0)