LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-04-2004, 12:19 PM   #1
Guru3
Member
 
Registered: Sep 2003
Location: //home/sweden
Distribution: RedHat 9, Fedora Core 1
Posts: 360

Rep: Reputation: 31
char to int: a twist


I've got two chars, say a and b. For example A is '2' and B is 'F'. How can I get the integer value of the hex those two make up, ie, 0x2F?
 
Old 12-04-2004, 12:51 PM   #2
Pecunia
LQ Newbie
 
Registered: May 2003
Location: The Netherlands
Distribution: LFS
Posts: 28

Rep: Reputation: 15
Something like this? (in C)
Code:
#include <ctype.h>

int hextoint(char c) {
	if (!isxdigit(c)) {
		return -1;
	}
	if (isdigit(c)) {
		return c - '0';
	}
	c = toupper(c);
	return c - 'A' + 0xA;
}

int do_funny_stuff(char a, char b) {
	int aa, bb;
	aa = hextoint(a);
	bb = hextoint(b);
	if (aa == -1 || bb == -1) {
		return -1;
	}
	return aa << 4 | bb;
}
 
Old 12-04-2004, 06:23 PM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Another way could be:
Code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  char a = '2', b = 'f';
  char buf[3];
  int val;

  sprintf(buf, "%c%c", a, b);  // or buf[0] = a, buf[1] = b, buf[2] = 0;
  val = strtol(buf, NULL, 16);

  printf("%X\n", val);

  return 0;
}
Quote:
itsme@dreams:~/C$ ./getnum
2F
 
Old 12-04-2004, 08:35 PM   #4
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Rep: Reputation: 90
Maybe I'm plain wrong, but I THINK there are some ansi functions to handle hex conversions.
 
Old 12-05-2004, 03:05 AM   #5
Guru3
Member
 
Registered: Sep 2003
Location: //home/sweden
Distribution: RedHat 9, Fedora Core 1
Posts: 360

Original Poster
Rep: Reputation: 31
i found sscanf.
Code:
int d;
char[] s = "AF";
sscanf(s, "%x", &d);
works perfectly, and thank you for your replies
 
Old 12-05-2004, 08:49 AM   #6
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Rep: Reputation: 90
This is one of the few times I get proud of my memory!

You're welcome.
 
  


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
C programming int to char conversion paranoid times Programming 15 03-17-2005 11:06 PM
c++ ERROR: int to const char ashirazi Programming 6 08-06-2004 10:01 PM
Converting int value to char liguorir Programming 8 05-23-2004 07:21 PM
int(char(254)) becomes kalleanka Programming 2 02-17-2004 03:35 PM
int, long in char * marek Programming 10 09-01-2003 10:43 AM

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

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