I have a binary file written in C containing a signed char data type equal to the number 1. I read in the single byte into a byte data type using MappedByteBuffer. However, when I print it out I get 49. What am I doing wrong?
C:
char * buffer = malloc(100);
signed char c;
int temp;
printf("Coordinate System?\n");
scanf("%s",&buffer[0]);
sscanf(&buffer[0],"%d",&temp);
c = temp+'0';
fwrite(&c,1,1,fd);
Java:
byte b;
b = file.read();
System.out.println(b) ===> prints the number 49.
I know it is some kind of bit order or something but I am not sure.
Thanks
The issue is with:
sscanf(&buffer[0],"%c",&c);
it says to scan the incoming character as an ascii value. so you can type 'a', '-', or '1' and the ascii value will be scanned.
you would need to scan %d
instead to get an actual integer. you would scan into an int variable instead of a char, then do a 0-255 range check to determine if it's viable to store in a single byte.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.