Hi everyone,

I am new here and I hope all of you are doing well.

I have spent the whole day searching around on the almighty Google for code that can help me with my problem. In the following code there may be quite a few n00b mistakes, and for these I apologize, since I have not done coding in C in ages. So, firstly I will try and explain what I need to do as best I can, before dumping eye-watering code:

My Objective:
I need to convert a String, ie: "Hello World!" (<- why did this ever became the standard? (".) ) into its hex equivalent.

str[11] = "Hello World";
hexstr[0] == 0x48;
hexstr[1] == 0x65;
...

I need this, since I am sending 4bit instructions in the form of certain pins (connected to and powered by a RL78G13 Renesas chip) being driven HI or LOW. These pins are connected to DB7-DB4 of a HD44780 LCD.

Pin60 -> DB4
Pin60 -> DB5
Pin62 -> DB6
Pin63 -> DB7

To do this, I send the following instructions to the RL78G13:

\* Example1: *\

P6 = 0x04;
P6 = 0x08;

This causes the LCD to print "H"...

Now, I need to somehow automate this process for a whole string. Otherwise, the code will become... well... :)

Currently, I have the following code:

\* Example2: *\

char greeting[] = "Hello World";
char hexstr[sizeof(greeting)] = "";
char *phexstr = hexstr;

unint8_t i = 0;

for(i = 0; i < sizeof(greeting); i++)
{       
    sprintf(phexstr, "%x", greeting[i]);
    phexstr +=2;

    P6 = hexstr[i] >> 4;    // 0x04 is sent 
                            // The 4higher bits of foo[i] are set on P6.0-P6.3, 
                            // the 4bottom bits are "don't cares"
    P3 = 0x01;              // LCD_RS set hi
    Stable_Wait(1000);      // Wait for LCD_RS pin to reach 5V
    P3 = 0x00;              // LCD_RS set low. The four pins P6.0-P6.3 are read by LCD
    Stable_Wait(1000);      // Wait for LCD_RS pin to reach 0V

    P6 = hexstr[i];         // 0x48 is sent
                            // The 4lower bits, which were discarded ^^, are set.               
    P3 = 0x01;          
    Stable_Wait(1000);
    P3 = 0x00;
    Stable_Wait(1000);

    // PS: i might be wrong about this >> operation
    // This means to me that with command: 0x48 >> 4 => 0x04
    // I only care about the 4 byte
    // and then I only care about the 8 byte
    // and since P6 only has P6.0 - P6.3 connected, only the last relevant
    // byte will be read... am I talking in circles? :P

    // I know my hardware is working, because if I hardcode as in Example 1, the LCD writes "H" :)
}

Ok, some remarks:

First, syntax errors. I am unsure of the exact version of C I am programming in. I am using the compiler of E2 Studio by the Renesas group... this comes with my chip. So what I wrote down is exactly the code in my program.

Secondly, sprintf gives me an Error: "incompatible implicit declaration of built-in function 'sprintf' [enabled by default]"... this where I go (╯°□°)╯︵ ┻━┻ ... sadly I cannot even try to remember casting and pointers and all of that. If any of you can suggest reading material or tutorial sites, I would highly appreciate it.

I would really appreciate any help regarding this.

Regards,

Edited 2 Years Ago by swaar.metaal.7: O.o

I'm quite the novice but are chars not just unsigned int values anyway?

for example...

char c = 0x41; // A
cout << c <<endl;
getchar();
Comments
right :)

You're right Suzie999, there is no need to convert from char to hex -- char is already hex. Whether it is int or hex all depends on how you want to display it to us humans. It's all the same to computers. Line 14 could just as easily be written like this:

P6 = greeting[i] >> 4;

Wow... it was as easy as that...

Thank you all for your help. Code works perfectly.

Regards,

This article has been dead for over six months. Start a new discussion instead.