LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 03-13-2011, 05:03 AM   #1
yoavf
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Rep: Reputation: 0
Convert hex string to int in kernel


Hello,
How to convert an hex string to integer inside a device driver, w/o wrinting my own utility.
I see that sscanf, strtol are undef.

Thanks
 
Old 03-13-2011, 09:00 AM   #2
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Rep: Reputation: 22
Hello.
It sounds like you are a beginner, so tell us what you want to do, what the problem is.
 
Old 03-13-2011, 10:42 AM   #3
yoavf
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Original Poster
Rep: Reputation: 0
I am a beginner with device drivers

I am trying to convert hex string to integer using some lib utility, instead of writing it myself.
sscanf, strtol failed to pass compilation.
Then I tried atoi, which returns 0.

Here is the relevant input, code, and log:
Input (dispatch is a process that calls device drive):
/usr/htdr/bin/dispatch 0x35 0x0b 0x130 1 U1400.1B1.019DF02-P1-C3

Code:
int mapRemoteCommand (char *arg, char *rasSimulatedArg, int *op) {

int subCommand;
char *op_str;
char *subCommand_str;

WRITE_LOG("110313 L901 cpssdd mapRemoteCommand start: arg = %s\n", arg, NULL);
/* Remove Remote Command OP from input string */
if (NULL == strtok (arg, space_delimiter)) {
WRITE_LOG("110309 L1704 cpssdd: op= %d; arg= %s\n", op, arg);
WRITE_LOG("OPeration=%d failed on remote Lpar \n", op, NULL);
return 0;
}

op_str = strtok (NULL, space_delimiter);
/* 110313 Does not work: sscanf (op_str, "%x", op); */
/* 110313 Does not work: *op = strtol( op_str, NULL, 0); */
*op = atoi(op_str);
WRITE_LOG("110313 L912 cpssdd mapRemoteCommand: RAS op=%d; op_str = %s\n", *op, op_str);
/* *op is the 2nd field in Remote command - after Remote Command OP */

if (*op <= OS_IOCTL_MAX_CMDS) {
/* RAS command */
subCommand_str = strtok (NULL, space_delimiter);
/* 110313 Does not work: sscanf (subCommand_str,"%x",&subCommand); */
/* 110313 Does not work: *op = strtol( subCommand_str, NULL, 0); */
subCommand = atoi(subCommand_str);
WRITE_LOG("110313 L921 cpssdd mapRemoteCommand: op= %d; op_str = %s\n", *op, subCommand_str);

((OsIoctlRas *) (arg))->header.subCommand = subCommand; /* Subcommand is in the same position for all RAS commands types */
}
return 1; /* DEBUG */

Log:
HTDR_CPSS: 17:25:34:392 110309 L1811 cpssdd Remote Command: op= 53; arg= 0x35 0x0b 0x130 1 U1400.1B1.019DF02-P1-C3
HTDR_CPSS: 17:25:34:392 110313 L901 cpssdd mapRemoteCommand start: arg = 0x35 0x0b 0x130 1 U1400.1B1.019DF02-P1-C3
HTDR_CPSS: 17:25:34:392 110313 L912 cpssdd mapRemoteCommand: RAS op=0; op_str = 0x0b <-- op is expected to be 11
HTDR_CPSS: 17:25:34:392 110313 L921 cpssdd mapRemoteCommand: op= 0; op_str = 0x130 <-- op is expected to be 304


Any idea?
 
Old 03-13-2011, 11:27 AM   #4
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Rep: Reputation: 22
Quote:
Originally Posted by yoavf View Post
I am trying to convert hex string to integer using some lib utility, instead of writing it myself.
sscanf, strtol failed to pass compilation.
If think you've read c++ references like http://www.cplusplus.com/reference/c...cstdio/sscanf/

I can not figure out what you want to do, e.g what you mean with "hex string" - is it a data stream, a simple string of bytes with values from 32 to 255 terminated by 0, ... ?
And explain what you want to convert:
-data coming from X having this
-data structur
-have to be converted to structure Y because ...
-to can be used for what (to do)
 
Old 03-13-2011, 04:29 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Have you #included the correct C headers, such as <stdio.h>, <stdlib.h>, etc. ?

--- rod.
 
Old 03-14-2011, 09:55 AM   #6
yoavf
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks Guys,
Guess I will have to write a small routine.
Thanks anyway
 
Old 03-16-2011, 02:14 AM   #7
ira
LQ Newbie
 
Registered: Sep 2010
Posts: 5

Rep: Reputation: 0
Hello Yoavf,

I have the same requirement, but i cannot figure out how to do it.
Could you please share what you have done?

Thanx in advance.
 
Old 03-16-2011, 02:17 AM   #8
ira
LQ Newbie
 
Registered: Sep 2010
Posts: 5

Rep: Reputation: 0
What i exactly wanted is to retrieve an integer I get inside a char buffer.
(I.e., convert a char* to a unsigned long value)

"simple_strtoul" function does what i exaclty wanted.

Last edited by ira; 03-16-2011 at 04:35 AM.
 
Old 03-16-2011, 08:35 AM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by yoavf View Post
I am a beginner with device drivers

I am trying to convert hex string to integer using some lib utility, instead of writing it myself.
sscanf, strtol failed to pass compilation.
Then I tried atoi, which returns 0.
Why did these fail to compile? The compiler doesn't know that it is compiling a device driver, so it should be just standard C.
Code:
/* sscanfTest.c  */
#include	<stdio.h>
#include	<stdlib.h>

int	main( int argc, char * argv[] ){

int	hexArg1, hexArg2, hexArg3, decArg1;
char	stringArg[1024];	/* Arbitarily large  */
	
	sscanf( argv[1], "%x %x %x %d %s", &hexArg1, &hexArg2, &hexArg3, &decArg1, stringArg );
	
	printf( "Integers: %d %d %d %d\n", hexArg1, hexArg2, hexArg3, decArg1 );
	printf( "String: %s\n", stringArg );
	
	exit(0);
}
Build and run...
Code:
make sscanftest
cc     sscanfTest.c   -o sscanfTest
./sscanfTest '0x35 0x0b 0x130 1 U1400.1B1.019DF02-P1-C3'
Integers: 53 11 304 1
String: U1400.1B1.019DF02-P1-C3
--- rod.
 
Old 03-22-2011, 05:10 AM   #10
yoavf
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Hello ira,
Sorry, but I was not permitted to send the routine.
You will have to think how to build the number by translating each digit from ascii to integer, then consider the position in the number.

Good luck
 
  


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
convert string to int testac Programming 8 07-06-2010 10:12 PM
Hex output of a hex/ascii input string mlewis Programming 35 04-10-2008 12:05 PM
convert int to string mohtasham1983 Programming 15 03-02-2007 06:18 PM
Convert string (C++) to int FreeDoughnut Programming 10 10-30-2006 03:26 PM
convert string to long/int alaios Programming 10 09-15-2005 09:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

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