LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-26-2005, 05:39 AM   #1
linux_lover2005
Member
 
Registered: Dec 2004
Posts: 44

Rep: Reputation: 15
convert unsigned char * to unsigned long int


hello,
I am writing program in which i require to have unsigned char * string say (af03d) to be converted to unsigned long int. Which function is avail to me? i am sending that to other program and want that program to recover the unsigned long int to unsigned char * variable.
 
Old 04-26-2005, 05:58 AM   #2
sandipan
Newbie
 
Registered: Mar 2005
Location: India
Distribution: Red Hat Linux 8.0
Posts: 5

Rep: Reputation: 0
The function atoi converts strings to integers. it returns the integer on success, else 0. for ex, if str="1234" it returns 1234, if str="123a0f" it will return 0.

the function itoa does the reverse
 
Old 04-26-2005, 09:13 PM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
strtol is actually the best of the string to long functions (in the bsd/unix stdc world).

Code:
long strtol (char *string, char **endptr, int base);
Pass the string in as the first argument. The 2nd argument is a pointer to the element of the string you want to stop converting at (if you want it to convert until it can't go any further pass in NULL for 2nd parameter). The last parameter is the base of the number. For instance, if the string is "123A" and you pass the base in as 10 you will get 123 as your number (it will stop at A). If your base is 16 it will convert the A as well since it is a valid hex digit.

Also, itoa does not exist in the unix world, snprintf is what you can use as an equivalent.

EDIT: The function is available in stdlib.h....

Last edited by jtshaw; 04-26-2005 at 09:15 PM.
 
Old 04-26-2005, 11:38 PM   #4
sandipan
Newbie
 
Registered: Mar 2005
Location: India
Distribution: Red Hat Linux 8.0
Posts: 5

Rep: Reputation: 0
thanks jtshaw, that function was unknown to me,...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
printf unsigned long long int? blackzone Programming 9 03-04-2008 12:41 PM
How to print unsigned long int alaios Programming 6 07-14-2005 10:27 AM
64 bit CPU unsigned long int GodSendDeath Programming 8 03-29-2005 01:19 PM
int value larger then unsigned long jpc82 Programming 3 03-12-2004 03:54 PM
convert from char* to unsigned char* D J Linux - Software 2 02-20-2004 04:09 AM


All times are GMT -5. The time now is 10:50 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration