Problem parsing 16-bit Hex to ASCII

This is a discussion on Problem parsing 16-bit Hex to ASCII within the C Programming forums, part of the General Programming Boards category; I am writing a program for a class and I have an array of 16 bit ints and I need ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    40

    Unhappy Problem parsing 16-bit Hex to ASCII

    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?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    As always, post your attempt(s) and use code tags to encourage responses, and you will likely get the results you desire.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    40
    Believe me, my code isn't worth posting. It didn't even begin to work. It would compile and not do what I anticipated.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Believe me, it's not worth my time to help someone who won't follow the forum rules.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    40
    Okay, here's what I tried:

    insert
    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(".");
    		}
    	}
    }
    Okay, 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.

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Code:
    		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 just
    Code:
    firstdigit = &yourshort;
    seconddigit = &yourshort + 1;

    EDIT: What is the exact thing that you are attempting to do?

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    40
    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.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by black_spot1984
    Code:
    void disassembler(int s_adr, int e_adr)
    {
    	int index;
    	int memLocation=1; /* Arrays start at 0 usually, unless you have some reason to do otherwise... */
    	char* firstDigit, secondDigit; /* 'secondDigit' isn't a pointer. Only 'firstDigit' is. */
    There are many ways to split your two bits.

    1 - Bitshift.
    Code:
    unsigned short int sixteen = ...whatever...;
    unsigned char byte1, byte2;
    byte1 = sixteen & 0xFF;
    byte2 = (sixteen >> 8) & 0xFF;
    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.)

    2 - Use a union.
    Code:
    union foo
    {
        unsigned short int sixteen;
        unsigned char bytes[2];
    } bar;
    ...
    bar.bytes[0] = 'A';
    bar.bytes[1] = 'B';
    printf( "%u", bar.sixteen );
    3 - Fun with pointers.
    Code:
    unsigned short int sixteen;
    unsigned char *byte;
    
    byte = (unsigned char *)&sixteen;
    *byte = 'A';
    byte++; /* move to the next byte */
    *byte = 'B';
    There are a few more, but those should provide you something to chew on.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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'
    */
    Last edited by Dave_Sinkula; 12-05-2006 at 10:04 PM. Reason: Whoa! Pokey fingers need some sleep.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    /* 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)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by vart
    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)
    Oh, I know. Sometimes I just try to provoke a little thought is all.

    [edit]The assignment appears predisposed to a given conclusion.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    40
    Thank you guys!!! That worked perfectly. Sorry, I had just been working on that problem for so long that I couldn't think anymore!

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Dave_Sinkula
    Oh, I know.
    Have no doubt in it.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 16 bit compilar or 32 bit compilar
    By rajkumarmadhani in forum C Programming
    Replies: 16
    Last Post: 12-04-2007, 08:48 AM
  2. Hex to Ascii conversion
    By Ryno in forum C Programming
    Replies: 2
    Last Post: 03-24-2005, 08:16 AM
  3. ASCII v's hex
    By sononix in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2005, 04:18 PM
  4. 16 bit or 32 bit
    By Juganoo in forum C Programming
    Replies: 9
    Last Post: 12-19-2002, 06:24 AM
  5. Parsing problem
    By converge in forum C++ Programming
    Replies: 1
    Last Post: 01-24-2002, 10:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21