LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-10-2011, 10:27 AM   #1
avee137
Member
 
Registered: Apr 2010
Location: bangalore,india
Distribution: ubuntu 9.10,CentOS 5
Posts: 120

Rep: Reputation: 16
string to hex parser


I need to implement string to hex parser .

My requirement is as below :
1.get 8 characters from console input.
say ab12de11
2. my program should store it as
char buf5[5] = {'0xab','0x12','0xde','0x11','\0'};


any relevant suggestion would be appreciated .
 
Old 09-10-2011, 10:49 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Well what have you tried and where are you stuck? Looks like a simple homework type question.
 
Old 09-10-2011, 10:55 AM   #3
Ian John Locke II
Member
 
Registered: Mar 2008
Location: /dev/null
Distribution: Slackware, Android, Slackware64
Posts: 130

Rep: Reputation: 17
Scratch that. Misunderstood the question.

Last edited by Ian John Locke II; 09-10-2011 at 11:18 AM.
 
Old 09-10-2011, 11:09 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,239

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Do you know to handle arrays of arrays (and arrays of strings) in C?

Last edited by dugan; 09-10-2011 at 11:56 AM.
 
Old 09-10-2011, 03:07 PM   #5
avee137
Member
 
Registered: Apr 2010
Location: bangalore,india
Distribution: ubuntu 9.10,CentOS 5
Posts: 120

Original Poster
Rep: Reputation: 16
i figured out a limited parser for my use ;

code goes like this:

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

void DBG(char *str)
{
#ifdef DEBUG
	printf("%s\n",str);
#endif
}

char toHex(char ch1 , char ch2)
{
	char hexByte ;
	ch1 = ch1 << 4 ;
	hexByte = (ch1 | ch2 );
	return hexByte ;
}
char getHexEq(char inChar)
{
	switch (inChar)
	{
	case '0' :
		return 0 ;
		break ;
	case '1' :
			return 1 ;
			break ;
	case '2' :
			return 2 ;
			break ;
	case '3' :
			return 3 ;
			break ;
	case '4' :
			return 4 ;
			break ;
	case '5' :
			return 5 ;
			break ;
	case '6' :
			return 6 ;
			break ;
	case '7' :
			return 7 ;
			break ;
	case '8' :
			return 8 ;
			break ;
	case '9' :
			return 9 ;
			break ;
	case 'a' :
			return 10 ;
			break ;
	case 'b' :
			return 11 ;
			break ;
	case 'c' :
			return 12 ;
			break ;
	case 'd' :
			return 13 ;
			break ;
	case 'e' :
			return 14 ;
			break ;
	case 'f' :
			return 15 ;
			break ;
	default:
			printf("invalid character format\n");
			break ;
	}
}

int main()

{

	int i ,j;
	char ch1 ,ch2 ,chHex;
	char buf9[9] = {'\0'};

	char hexBuf4[4] = {'\0'};

	printf ("enter the 8 character hex id\n");
	scanf("%s",buf9);

	for(i = 0,j=0 ; i < 8,j<4  ; i+=2,++j)
	{
		ch1 = getHexEq(buf9[i]);
		ch2 = getHexEq(buf9[i+1]);

		chHex = toHex(ch1 , ch2);
		hexBuf4[j] = chHex ;

	}

	for(i = 0 ; i < 4  ; ++i)
	{
	printf(" 0x%x\n ", (unsigned)(unsigned char)hexBuf4[i] );
	}
	return 0;
}
 
Old 09-10-2011, 04:41 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by avee137 View Post
i figured out a limited parser for my use ;

code goes like this:
Here are a few tips:
  1. char values are actually numbers, but they're printed as characters and the zero value ('\0', not '0') has a special meaning for text. If you look up "ASCII table" on the web you'll see that the digits and the letters are respectively numbered sequentially. This means if you know a char val is a digit then you can get the integer form with val-'0'. Something similar can be done with letters, but uppercase and lowercase need to be dealt with separately.
  2. When reading strings with scanf, always specify the maximum string length. Your program could crash if someone entered a string longer than 8 characters. In more "important" programs that can potentially be exploited to crack into your system. In this case, you'd use "%8s" as your format.
  3. Some of your variable assignments can be skipped to make your code shorter. For example, toHex could be one line, as could your first for loop.
Kevin Barry
 
  


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
[SOLVED] Convert hex string to int in kernel yoavf Linux - Kernel 9 03-22-2011 05:10 AM
Hex String to ttysX digmeupnow Linux - Newbie 1 11-25-2008 03:33 AM
Hex output of a hex/ascii input string mlewis Programming 35 04-10-2008 12:05 PM
hex string to long in c rubadub Programming 5 07-26-2007 12:48 PM
String Parser pawan_songara Programming 1 08-27-2006 09:58 AM

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

All times are GMT -5. The time now is 04:40 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