Thread: Hex to String Code

  1. #1
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    Hex to String Code

    Hi All,

    Anyone feel like going though this code line by line with me as im new to this language and trying to get a pic working. Its supposed to output values that were gotten in hex as strings.
    Many Thanks and happy christmas / new year.

    Code:
    //usart.c
    #ifndef USART_H
    #define USART_H
    
    #include <pic18.h>
    #include <stdio.h>
    #include "usart.h"
    #include "Compiler.h"
    #include "portmap.h"
    
    void putch(char c)
    {
        while( !DEBUG_TXIF);
        DEBUG_TXREG = c;
    }
    
    const char hexDigit[] = "0123456789ABCDEF";
    
    void puthexdigit(unsigned char c)
    {
        putch(hexDigit[c]);
    }
    
    void putdec(unsigned char c)
    {
        if(c >= 200) {
            putch('2');
            c -= 200;
        } else if(c >= 100) {
            putch('1');
            c -= 100;
        }
    
        if(c >= 10) putch('0' + (c / 10));
    	else putch('0');
        putch('0' + (c % 10));
    }
    
    void puthex(unsigned char c)
    {
        puthexdigit(c >> 4);
        puthexdigit(c & 0x0F);
    }
    
    void puthex2(unsigned int c)
    {
        puthex(c >> 8);
        puthex(c);
    }
    
    void puthex4(unsigned long int c)
    {
        puthex(c >> 24);
        puthex(c >> 16);
        puthex(c >> 8);
        puthex(c);
    }
    
    void puthexstr(const char* str, int numBytes)
    {
        for(; numBytes--; ++str) puthex(*str);
    }
    
    #endif
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So what's the problem?
    Did you try
    Code:
    int main ( ) {
      unsigned char test[] = { 0x11, 0x55, 0xff };
      puthexstr ( test, 3 );
      putch( '\n );
      return 0;
    }
    > as im new to this language and trying to get a pic working.
    Most people new to C begin with lots of practice on their desktop machines, where there are plenty of tools to help with development. Embedded C development when you don't know a whole bunch of basic information is really hard.

    > while( !DEBUG_TXIF);
    This may or may not just lock up your program in an infinite loop.

    You need to state
    - which PIC in particular
    - which cross-compiler you're using

    > #ifndef USART_H
    > #define USART_H
    You DONT put these inside source code, only in header files.

  3. #3
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Merry X-mas One and all.

    Thank you for replying Salem.
    To begin with, I ( the original user) am not responsible for the post. It's a friend's in dire need.

    My friend stated that all he wants is a simple function to convert hex to actual string. Since he's completely new to C (but not Java), someone wrote that pile of functions for him. I went thru the whole thing and I figure that just about works. only problem is I thoughht surely there'd be an easier way to do it. I think the
    Code:
    void putch(char c)
    {
        while( !DEBUG_TXIF);
        DEBUG_TXREG = c;
     }
    function writes to stdout or at least is the output function. I haven't done C in quite a while nor do I know anything about pic programmming but I'm having problems understanding this function
    Code:
    void putdec(unsigned char c)
    {
        if(c >= 200) {
            putch('2');
            c -= 200;
        } else if(c >= 100) {
            putch('1');
            c -= 100;
        }
    
        if(c >= 10) putch('0' + (c / 10));
    	else putch('0');
        putch('0' + (c % 10));
    }
    which, from what I see, I think it translates the hex to decimal format (function name big giveaway). And I'd also like this line explained please:
    Code:
     puthexdigit(c & 0x0F);
    is he accessing a register there or trying to logically and I think the former but shouldn't it be
    Code:
     puthexdigit(c &0x0F);
    ?

    Sorry about the long winded post but it's been a while.
    Thank you for your time.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I think your 'stdout' is a serial port, judging from the names used in the putch function.

    Before doing anything else, I'd make sure a simple program to putch() a few characters actually works, and you can see them when you connect the PIC module to your PC via a serial cable, and run say hyperterm on the PC to monitor say COM1 (or whatever port you plugged into).

    > puthexdigit(c & 0x0F);
    Just extracts 4 bits from a byte
    Given c = 0x5A say, then c & 0x0F gets the 'A' and c >> 4 gets the '5'

    > from what I see, I think it translates the hex to decimal format
    That would be my guess as well.

  5. #5
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    > Given c = 0x5A say, then c & 0x0F gets the 'A' and c >> 4 gets the '5'
    bitwise AND right? does this mean a character interger is represented internally as a 32bit or 16bit word?? (I'm getting confused here still)

    Which are the High order/most or least significant bits in a byte.
    Assuming the byte is laid out thus: 8 7 6 5 4 3 2 1 0
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's represented as whatever a int is on your machine. Do you have 16bit ints? 32bit ints? 64bit ints? It all depends really. There are only minimum size specifications in the standard as to what size an int is. Also, you are illustrating a 9 bit byte. Which while it is allowed by the standard, is probably just a typo on your end. I assume you meant: 7 6 5 4 3 2 1 0, as there would be no 8.

    Anyway, this will tell you what your integer is:
    Code:
    printf("My int is %d.\n", sizeof( int ) );
    That'll tell you how many bytes it is. The CHAR_BIT macro will tell you if in fact you do have 9 bit bytes.


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

  7. #7
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Thanx for clarifying that. but I still need this line of code explanied to me in laymen's term:
    Code:
     c & 0x0F
    ;
    Just for my own clarification. I know what it does but I'm after the operation process.
    Thanx.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    FAQ > Explanations of... > Bit shifting and bitwise operations
    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.*

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > It's a friend's in dire need.
    Your friend should join the board and ask his own questions in his own way then.
    Third-party translations are just going to slow the whole job down.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Error in Recursive String Palindrome Code
    By clegs in forum C Programming
    Replies: 13
    Last Post: 12-21-2008, 12:36 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM