LinuxQuestions.org
Review your favorite Linux distribution.
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 02-25-2006, 07:44 PM   #1
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
Ascii to hexadecimal values


Hello, I have to read in a Ascii file and manipulate the data in the file as numeric (hexadecimal) values instead of as characters. I was wondering if there was some sort of call in the C library to do this, or do I need to write something myself. So as an example, say I have the following text file:

Code:
This is a file.
I need to open this and retrieve the data and manipulate it as it's numeric ASCII values. The reason for this is because I've implemented an encryption algorithm, and I need to read in the values as bytes instead of as plain text. There may be a really easy way to do this, so before I attempted to create my own function, I thought I'd post this.

Thanks for any suggestions.
 
Old 02-25-2006, 08:34 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
It's already in hexadecimal values in memory. That's how the computer stores it.

So, if you want to read in the string as a sequence of 32-bit numbers (usually equivalent to four characters in the string), do this:
Code:
char     *sFileText;
unsigned  uNumericData;

...

for( chunk = 0; chunk < DATA_SIZE; chunk++ )
{
  // Typecast the string pointer to pretend it's pointing at an array of unsigneds
  // Add "chunk" to the pointer to get the offset for the next group of 4 bytes
  // Dereference the adjusted unsigned pointer to retrieve the data
  uNumericData = *((unsigned *)sFileText + chunk);

  // Process uNumericData
}

Last edited by Dark_Helmet; 02-25-2006 at 08:35 PM.
 
Old 02-25-2006, 09:08 PM   #3
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Original Poster
Rep: Reputation: 30
Thanks for the suggestion. I'll give it a try.
 
  


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
Hexadecimal Error moeFEAR Mandriva 1 10-21-2005 11:14 AM
Hexadecimal Error moeFEAR Linux - Laptop and Netbook 1 10-20-2005 09:26 AM
function in shell to convert hexadecimal into ascii suchi_s Linux - Software 1 04-01-2005 02:07 PM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
converting integer value to hexadecimal string in C - any suggestions?? woodywellhung Programming 3 04-24-2004 05:27 PM

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

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