I am writing a program for a class and I have an array of 16 bit ints and I need to make each entry in the array print two ASCII characters. I have tried a few different ways on my own but couldn't come up with a solution. And suggestions?
Printable View
I am writing a program for a class and I have an array of 16 bit ints and I need to make each entry in the array print two ASCII characters. I have tried a few different ways on my own but couldn't come up with a solution. And suggestions?
Believe me, my code isn't worth posting. It didn't even begin to work. It would compile and not do what I anticipated.
Believe me, it's not worth my time to help someone who won't follow the forum rules.
Quzah.
Okay, here's what I tried:
insertOkay, the LC3 is like a hypothetical computer for which I am supposed to create a simulator. This function is supposed to be a visual disassembly of the code. So, it prints the memory address then what is at each one of the memory locations and finally it is supposed to print a "." if the 8-bit ASCII value for each memory location is not within x21 and x7e. If the 8-bit value is within x21 and x7e it is supposed to print the character. This was not my first attempt, it was my latest. Obviously, it doesn't work.Code:void disassembler(int s_adr, int e_adr)
{
int index;
int memLocation=1;
char* firstDigit, secondDigit;
int examine;
printf("\n\nMemory Dump:\n");
for(index=s_adr;index<=e_adr;(index+=8))
{
int counter;
printf("\n%#06x: ",index);
for(counter=8;counter>0;counter--)
{
printf(" %04x",LC3Memory[memLocation]);
memLocation++;
}
memLocation-=8;
printf(" ");
for(counter=8;counter>0;counter--)
{
firstDigit=strtok(("%#04x",LC3Memory[memLocation]), "");
secondDigit=strtok(("%#04x",LC3Memory[memLocation]), "");
strcat(firstDigit,secondDigit);
examine= (int) firstDigit;
if(examine>32 && examine<126)
printf("%c",examine);
else
printf(".");
}
}
}
Not fully understanding the question (I guess), I'd say that you don't need to strtok, but justCode:for(counter=8;counter>0;counter--)
{
firstDigit=strtok(("%#04x",LC3Memory[memLocation]), "");
secondDigit=strtok(("%#04x",LC3Memory[memLocation]), "");
strcat(firstDigit,secondDigit);
examine= (int) firstDigit;
if(examine>32 && examine<126)
printf("%c",examine);
else
printf(".");
}
Code:firstdigit = &yourshort;
seconddigit = &yourshort + 1;
EDIT: What is the exact thing that you are attempting to do?
Okay, there is a 16-bit hex value at each one of the array locations LC3Memory[]. I need to make each one of these 16 bit values 2 8-bit ASCII values that can be printed to the screen.
There are many ways to split your two bits.Quote:
Originally Posted by black_spot1984
1 - Bitshift.Here you just mask off the lower byte, stick that in one variable, then shift over eight places, and mask again. Here we're assuming a few things, like 8 bit bytes. (CHAR_BIT is more accurate.)Code:unsigned short int sixteen = ...whatever...;
unsigned char byte1, byte2;
byte1 = sixteen & 0xFF;
byte2 = (sixteen >> 8) & 0xFF;
2 - Use a union.3 - Fun with pointers.Code:union foo
{
unsigned short int sixteen;
unsigned char bytes[2];
} bar;
...
bar.bytes[0] = 'A';
bar.bytes[1] = 'B';
printf( "%u", bar.sixteen );
There are a few more, but those should provide you something to chew on.Code:unsigned short int sixteen;
unsigned char *byte;
byte = (unsigned char *)&sixteen;
*byte = 'A';
byte++; /* move to the next byte */
*byte = 'B';
Quzah.
All right, I'll play a little bit of devil's advocate.
Code:#include <stdio.h>
int main(void)
{
unsigned short value = 0x3141;
unsigned char *byte = (unsigned char*)&value;
size_t i;
for ( i = 0; i < sizeof value; ++i )
{
printf("byte[%lu] = %02X = '%c'\n", (long unsigned)i, byte[i], byte[i]);
}
return 0;
}
/* my output
byte[0] = 41 = 'A'
byte[1] = 31 = '1'
*/
That's why I prefer shifting and masking - it is more platform independent (depends only on CHAR_BIT value, and it can be solved with masks depending on it)Quote:
/* my output
byte[0] = 41 = 'A'
byte[1] = 31 = '1'
*/
Oh, I know. Sometimes I just try to provoke a little thought is all.Quote:
Originally Posted by vart
[edit]The assignment appears predisposed to a given conclusion.
Thank you guys!!! That worked perfectly. Sorry, I had just been working on that problem for so long that I couldn't think anymore!
Have no doubt in it. :)Quote:
Originally Posted by Dave_Sinkula