LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-29-2007, 01:51 PM   #1
ilnli
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Slackware 10.0, SUSE 9.1, RH 7, 7.3, 8, 9, FC2
Posts: 413

Rep: Reputation: 32
hex to ascii and ascii to hex


Can anyone tell me how can I convert a string into ascii, and hex?

my plateform is linux gcc.
 
Old 08-29-2007, 02:07 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
hexdump and printf, for starters. Look at the man page for each for more detail.
 
Old 08-29-2007, 08:02 PM   #3
ilnli
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Slackware 10.0, SUSE 9.1, RH 7, 7.3, 8, 9, FC2
Posts: 413

Original Poster
Rep: Reputation: 32
I don't need a software I'm need to do this in a C program. So need some library function.

thanks
 
Old 08-30-2007, 03:26 AM   #4
jeff_g
LQ Newbie
 
Registered: Aug 2007
Posts: 1

Rep: Reputation: 0
Are you saying that you want to take a C datatype such as an "int" and format it as a (printable) ascii string?

Of course, you'll use the C library function sprintf(), for example:

Code:
#include <stdio.h>
#include <stdlib.h>

int i;
char buffer[80]; /* A buffer to hold the ascii string */

i = 44;
sprintf(&buffer[0], "%d", i);
/* buffer[] now contains the string "44" */
sprintf() has quite a few options for formatting various C datatypes as ascii strings, so consult a manual on it.

For going the other way (ie, converting an ascii string back to a C datatype such as long or int), you'll want to look at atoi() atol(), atof(), etc.
 
Old 08-30-2007, 11:14 AM   #5
henk1420
LQ Newbie
 
Registered: Aug 2007
Location: Europe
Distribution: Debian
Posts: 5

Rep: Reputation: 0
Quote:
Originally Posted by ilnli View Post
Can anyone tell me how can I convert a string into ascii, and hex?

my plateform is linux gcc.
Code:
// Note: for reasons of 'safety' decstring and hexstring
// are longer than string itself
// This code is not very efficient

char string[64], decstring[256], hexstring[256], dummy[16];
int i;

// Fill the string somehow. Up to you.

// intialise to empty
decstring[0] ='\0';
hexstring[0] = '\0';

for (i=0; i<strlen(string); i++)
{
   sprintf(dummy, "%02X ", string[i]);  // Convert to hexadecimal notation
   strcat(hexstring, dummy);

   sprintf(dummy, "%03d ", string[i]);  // Convert to decimal notation
   strcat(decstring, dummy);
}
Regards,
H
 
Old 08-30-2007, 01:57 PM   #6
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Turning a string into hex. For instance, "Hello" becomes "48 65 6C 6C 6F".
Code:
inline void hexify_ascii(char c, char * buf)
{
	int high = c >> 4;
	int low = c & 0xF;
	*buf       = high + '0' + (7 * (high / 10));
	*(buf + 1) = low  + '0' + (7 * (low  / 10));
}

int str_to_hex_str(const char * str, char * hex, int hex_len)
{
	int i;
	char * ptr = hex;
	int str_len = strlen(str);

	if (str_len * 3 > hex_len - 1) {
		return -1;		/* str won't fit in hex */
	}

	for (i = 0; i < str_len; i++) {
		hexify_ascii(str[i], ptr);
		*(ptr + 2) = ' ';
		ptr += 3;
	}

	*ptr = '\0';

	return i;
}
Going the other way is similar.
 
Old 08-31-2007, 09:29 AM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
sprintf to convert a value (integer) to a hex string , strtol or strtoul to convert a hex string to a value (integer).
 
Old 08-31-2007, 11:55 AM   #8
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Look at strtol() and related for converting strings to any base you choose. If you are in kernel space and can't use any of the standard libraries then you'll have to roll your own.

This routine converts an ascii string of the form 0x123abc to an integer, and it tests to make sure the string starts with 0x or 0X. I use this function in kernel space when I need to do this:
Code:
static int str2hex(char *str) {
  int strlen,result,intermed,intermedtop;
  char *s;
  s=str;
  while(*s != 0x0) {s++;}
  strlen=(int)(s-str);
  s=str;
  if(*s != 0x30) {
	return -1;
  } else {
	s++;
	if(*s != 0x78 && *s != 0x58){
		return -1;
	}
	s++;
  }
  strlen = strlen-3;
  result = 0;
  while(*s != 0x0) {
	intermed = *s & 0x0f;
	intermedtop = *s & 0xf0;
	if(intermedtop == 0x60 || intermedtop == 0x40) {
		intermed += 0x09;
	}
	intermed = intermed << (strlen << 2);
	result = result | intermed;
	strlen -= 1;
	s++;
  }
  return result;
}
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[bash] ASCII to HEX and hex to ascii ////// Programming 17 05-08-2018 09:55 PM
display in hex + perl + non ASCII characters kshkid Programming 4 02-06-2007 04:48 PM
How do i write to a serial port (modem) in ascii or hex directly? Taliesin.duchaos Linux - Networking 6 04-21-2006 08:31 AM
Hex socks Linux - General 4 02-17-2005 12:05 PM
Binary to Hex Ascii converter carboncopy Slackware 1 05-28-2004 09:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration