The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Korn: How to loop through a string character by character shew01 Shell Programming and Scripting 10 12-02-2008 07:58 AM
Converting integer to String ROOZ Shell Programming and Scripting 1 06-05-2008 01:38 PM
Converting the case of the string? skyineyes Shell Programming and Scripting 5 07-09-2007 05:27 AM
command for converting string to integer esham Shell Programming and Scripting 1 08-04-2005 07:20 AM
converting string to unicode webtekie Shell Programming and Scripting 1 07-21-2004 11:43 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-20-2006
axes axes is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 49
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.
  #2 (permalink)  
Old 09-20-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
str="×¥ïA Å gïÛý ";
printf("%x",str);

Last edited by anbu23; 09-20-2006 at 12:41 PM..
  #3 (permalink)  
Old 09-20-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,929
That will not work. It will print the pointer to the string as hex, not the string itself.

How about this:

Code:
{
  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...

Last edited by Corona688; 09-20-2006 at 01:09 PM..
  #4 (permalink)  
Old 09-20-2006
axes axes is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 49
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
  #5 (permalink)  
Old 09-20-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,929
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:
Code:
#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.
  #6 (permalink)  
Old 09-20-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,929
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0