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-11-2007, 02:36 AM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
convert string to hexadecimal


Hi,
i am using the following code, in order to convert a char array (or string) to its hexadecimal representation:
Code:
	byte hexval=0x00;
	cout << "sscanf: ";
	char car2[2];
	car2[0]='1';
	car2[1]='9';
	sscanf( car2, "0x%02X", &hexval );	
	printf("0x%02X", hexval);
However, i get back the "0x00" value. With other "experiments" i didn't also manage to get the "0x13" back.

Ideas?
Thanks
 
Old 12-11-2007, 03:57 AM   #2
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
I assume the number in the string will be base ten as you want 19=0x13
Code:
	unsigned char hexval=0x00;
	cout << "sscanf: ";
	char car2[3];
	car2[0]='1';
	car2[1]='9';
	car2[2]='\0';
	sscanf( car2, "%2d", &hexval );	
	printf("0x%2X", hexval);
 
Old 12-11-2007, 05:45 AM   #3
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
There are some things in the C++ standard library you could use to easily solve this problem. Here's an example:

Code:
#include <iomanip>
#include <iostream>
#include <sstream>

using namespace std;

void print_in_hex_form(const string& s)
{
   istringstream iss(s);
   int n;

   if (!(iss >> n))
   {
      cout << "Not a number!" << endl;
   }
   else
   {
      cout << hex << "0x" << n << endl;
   }
}

int main()
{
   print_in_hex_form("1337");
   print_in_hex_form("15");
   print_in_hex_form("0");
}
When run it prints:
Code:
$ ./runme.exe
0x539
0xf
0x0
 
Old 12-11-2007, 06:23 AM   #4
lali.p
Member
 
Registered: Jan 2007
Distribution: Slackware 11.0
Posts: 141

Rep: Reputation: 16
wow!!! thats why everbody loves standard library !!!!
cheers + beers = first love



Regards lali.p
 
  


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
Comparison between character and hexadecimal string.. vishalbutte Programming 2 03-05-2006 07:59 PM
function in shell to convert hexadecimal into ascii suchi_s Linux - Software 1 04-01-2005 02:07 PM
Convert C++ string to C char* nyk Programming 3 06-17-2004 08:15 AM
converting integer value to hexadecimal string in C - any suggestions?? woodywellhung Programming 3 04-24-2004 05:27 PM
How do you convert a string to an integer? wbdune Linux - Newbie 11 11-01-2003 02:01 PM

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

All times are GMT -5. The time now is 04:01 AM.

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