converting character string to hex string

HI

Hi I have a character string which contains some special characters and I need it to display as a hex string.

For example, the sample i/p string: ץ�A � g���  and
                  the o/p should be  : D7A5EF4100C5010067EFDBFD 

Any pointers or sample code pls.

str="ץ�A � g��� ";
printf("%x",str);

That will not work. It will print the pointer to the string as hex, not the string itself.

How about this:

{
  const char *str="asdf";
  int n;
  for(n=0; str[n] != '\0'; n++)
    printf("%02x",(unsigned char)str[n]);
}

Given the weird characters in the string you want, you may have encoding problems that make the bytes different while the string looks the same. I can't even copy-paste that string. It messes up my shell.

[edit] Nope, that doesn't work. Your string contains a NULL. Either that or it's unicode... working on it...

Thanks corona, It works fine.

I have one more doubt regarding sizeof\(string\).

For ex:- If I have code,

           char func\(char funcstr[12]\)\{
                printf\("%d\\n",sizeof\(funcstr\)\);
           \}
           main\(\) \{
              char mainstr[12];
              func\(mainstr\);
              printf\("%d\\n",sizeof\(mainstr\)\);
           \}

size of mainstr is giving the array size(i.e. 12),
whereas funcstr is giving the pointer size(i.e. 4),
will it not give the array size as we declared it as array ?

Thanks

Your string is 16-bit unicode. How are you reading it??

Once you've inputted a 16-bit unicode string, you can print it like this:

#include <stdio.h>
#include <string.h>

int main()
{
  int n;
  unsigned short int ustr[]=
    { 0xD7A5, 0xEF41, 0x00C5, 0x0100, 0x67EF, 0xDBFD, 0x0000};

  for(n=0; ustr[n] != 0x0000; n++)
    printf("%04x",ustr[n]);

  printf("\n");
  return(0);
}

[edit] I'm glad my first example worked but I have not a clue how it managed that feat. :eek:

CODE TAGS FOR CODE PLEASE.

Anyway.

It is giving the array size for the array because it is an array. It is giving the pointer size for the passed array because it is a pointer -- arrays are passed by reference.