LinuxQuestions.org
Help answer threads with 0 replies.
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 03-16-2016, 05:29 AM   #1
srinietrx
Member
 
Registered: May 2013
Posts: 101

Rep: Reputation: Disabled
Hex to ascii conversion in c


I send below string which is in hex format to server.In between there is one abstract module before sending to server
unsigned char pTAG[] = "9F34031E0300";

In server I am getting in output as
"39463334303331453033303039".

My question is What conversion need to done with pTAG I can get "9F34031E0300". I tried converting into ascii but of no use.

I am using C as programming language.
 
Old 03-16-2016, 05:48 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
You can use sscanf to convert it to some type, for example long long. And then printf to print it decimal. Something like this:

Code:
long long i;
sscanf(s,"%llx",&i);
printf("%lld",i);
 
Old 03-17-2016, 08:07 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
My handy programmer's calculator (app) tells me that the hex-string you provided translates to decimal 175045739414272.

So, I'm not sure where that number you're seeing on the server side came from, and I'm not going to try to dumpster-dive right now to find out ... ahh, wait a minute. I see it.

In the string you show, each two-character pair appears to correspond to one of the characters in the hex string, with "39" tacked on to the end for good measure. So, the code simply converted an ASCII(?) character string from one representation to another. It did not properly interpret it as hexadecimal.

The scanf()-based solution described in a previous post should take you right where you need to go. The correct answer that you're looking for is the number given in this post.
 
  


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
[bash] ASCII to HEX and hex to ascii ////// Programming 17 05-08-2018 09:55 PM
Not Ascii, Hex, Oct or unicode ceantuco Linux - Newbie 6 11-10-2010 07:49 PM
Hex output of a hex/ascii input string mlewis Programming 35 04-10-2008 12:05 PM
hex to ascii and ascii to hex ilnli Programming 7 08-31-2007 11:55 AM
Binary to Hex Ascii converter carboncopy Slackware 1 05-28-2004 09:09 AM

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

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