Hexadecimal to ascii


 
Thread Tools Search this Thread
Top Forums Programming Hexadecimal to ascii
# 1  
Old 12-07-2009
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.Smilie
# 2  
Old 12-07-2009
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
Quote:
Originally Posted by jgt
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
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generate hexadecimal

Hello I'm working on a script to list all ipv6 from given address so I've run this script which create hex part of ipv6 STR2=159 END2=200 SUM2=`expr $END2 - $STR2` for ((i=STR2;i<=END2;++i)); do x=$( printf "%x" $i ) ; echo $x echo -e "::"$x >> netpart.txt done output is : ::9f... (2 Replies)
Discussion started by: nimafire
2 Replies

2. Shell Programming and Scripting

Convert Hex to Ascii in a Ascii file

Hi All, I have an ascii file in which few columns are having hex values which i need to convert into ascii. Kindly suggest me what command can be used in unix shell scripting? Thanks in Advance (2 Replies)
Discussion started by: HemaV
2 Replies

3. Programming

Hexadecimal to binary operation

Dear all, I am trying to write c-program to read the following file containing hexadecimal values (snippet of big data file). I want to combine two hexadecimal values together like A0A03E01 and then would like to have the binary equivalent to perform further test on it. Unfortunately, it failed... (16 Replies)
Discussion started by: emily
16 Replies

4. Shell Programming and Scripting

Conversion from Hexadecimal to binary

How can I convert hexadecimal values to Binary from the second field to the end Input: WS-2 23 345 235 DT-3 45 4A3 000 pp-2 76 300 E4 Output: WS-2 100011 1101000101 1000110101 DT-3 1000101 10010100011 000 pp-2 1110110 1100000000 11100100 (16 Replies)
Discussion started by: aydj
16 Replies

5. Shell Programming and Scripting

Hexadecimal to Binary conversion

Hi Guys, Is it possible to convert the hexadecimal to Binary by unix command.....I could not figure out.... If I need to convert AF6D to binary...what could be the way to do? Thanks in advance!! ---------- Post updated at 02:57 AM ---------- Previous update was at 02:42 AM ---------- I... (6 Replies)
Discussion started by: Indra2011
6 Replies

6. Shell Programming and Scripting

Convert hexadecimal value in decimal value

hi all, this is my script: #! /bin/sh minutes=$( { i2cget -f -y 0 0x51 3; } 2>&1 ) minutes=${minutes:2} hour=$( { i2cget -f -y 0 0x51 4; } 2>&1 ) hour=${hour:2} day=$( { i2cget -f -y 0 0x51 5; } 2>&1 ) day=${day:2} month=$( { i2cget -f -y 0 0x51 7; } 2>&1 ) month=${month:2} ... (6 Replies)
Discussion started by: enaud
6 Replies

7. Shell Programming and Scripting

printf Hexadecimal output

printf "%X\n" "A" 41 printf "%X\n" "2" 2 Expected 32 (not 2). Is there a "printf" which will output the hexadecimal value of a numeric character? (9 Replies)
Discussion started by: methyl
9 Replies

8. Shell Programming and Scripting

convert ascii values into ascii characters

Hi gurus, I have a file in unix with ascii values. I need to convert all the ascii values in the file to ascii characters. File contains nearly 20000 records with ascii values. (10 Replies)
Discussion started by: sandeeppvk
10 Replies

9. UNIX for Dummies Questions & Answers

Hexadecimal to Decimal

Hi all, I have a small script to convert my HexaDecimal Input to Decimal as output. #!/bin/ksh hd=00208060 dec=`printf %d $hd` echo $dec Output of the above program: printf: 00208060 not completely converted 16 But my expected output is "2130016". How can i acheive this. I... (2 Replies)
Discussion started by: Arunprasad
2 Replies

10. Shell Programming and Scripting

Get Hexadecimal Value

I have a record in a file that ends with the new line character "\n". How dio I determine the hexadecimal value for that? (2 Replies)
Discussion started by: lesstjm
2 Replies
Login or Register to Ask a Question