Go Back   The UNIX and Linux Forums > Top Forums > Programming
google site



Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-07-2009
Registered User
 

Join Date: Apr 2009
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Hexadecimal to ascii

Let's suppose i have a hexadecimal array with 16 cells.for example
b3e2d5f636111780

i want to convert it to an array of ascii characters(in C) so that
i can reduce total size of the file i want to put it in.
But i am afraid i have not fully understand the difference between ascii
and hex(i thought that an ascii array is simply a char* array but
this seems wrong).
Thank you in advance.
Sponsored Links
  #2  
Old 12-07-2009
jgt's Avatar
jgt jgt is offline Forum Advisor  
Registered User
 

Join Date: Apr 2007
Location: 44.21.48N 80.50.15W
Posts: 852
Thanks: 0
Thanked 17 Times in 17 Posts
Ascii characters are made up from one 8 bit byte. So a "|" (pipe symbol) is represented by a value of 179 decimal, or b3 hexadecimal.
What you have in your original string is 16 8-bit bytes. If you take these in pairs and multiply the first by 16 and add the second, you will end up with a value between 0 and 255, (b *16 +3 = 11 * 16 +3 = 179), which you can then store in a single 8 bit byte, resulting in a 50% reduction in storage.
  #3  
Old 12-08-2009
Registered User
 

Join Date: Apr 2009
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by jgt View Post
Ascii characters are made up from one 8 bit byte. So a "|" (pipe symbol) is represented by a value of 179 decimal, or b3 hexadecimal.
What you have in your original string is 16 8-bit bytes. If you take these in pairs and multiply the first by 16 and add the second, you will end up with a value between 0 and 255, (b *16 +3 = 11 * 16 +3 = 179), which you can then store in a single 8 bit byte, resulting in a 50% reduction in storage.
thank you for your answer! so you suggest that i do that in a for(){} condition and that i write every byte that in each repetition occurs into the file?
  #4  
Old 12-08-2009
Registered User
 

Join Date: Oct 2003
Posts: 115
Thanks: 0
Thanked 1 Time in 1 Post
There are a few more things to consider. First, strings with an odd number of characters must be flagged somehow so that the resulting representation can be converted back into an odd number of characters. Second, you're turning a string into the native representation of an integer. So, you need to decide which endianness the storage will have if you want the resulting file to be compatible across architectures.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Test Hexadecimal number francis_tom Shell Programming and Scripting 2 09-23-2009 06:17 PM
convert ascii values into ascii characters sandeeppvk Shell Programming and Scripting 10 04-23-2009 11:23 PM
Hexadecimal to Decimal Arunprasad UNIX for Dummies Questions & Answers 2 02-25-2009 03:30 AM
looping in hexadecimal with bash soba UNIX for Dummies Questions & Answers 2 07-01-2008 12:36 PM
Get Hexadecimal Value lesstjm Shell Programming and Scripting 2 09-07-2005 09:29 AM



All times are GMT -4. The time now is 07:34 AM.