Convert HEX to EBCDIC IN C


 
Thread Tools Search this Thread
Top Forums Programming Convert HEX to EBCDIC IN C
# 1  
Old 11-24-2008
Convert HEX to EBCDIC IN C

i want to convert Hex value To EBCDIC value.
i tried to convert hex to ascii and then to ebcdic but it doesn't give desired results .

it doesn't give corresponding ebcdic value instead it gives some junk values.
e.g;
Hex EBCDIC
-----------------
81 a
82 b
83 c
84 d
85 e
86 f
87 g
88 h
89 i


Hex EBCDIC
-----------------
C1 A
C2 B
C3 C
C4 D
C5 E
C6 F
C7 G
C8 H
C9 I


code is as:
//********************************************************************
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
static unsigned char a2e[256] = {
0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 37, 11, 12, 13, 14, 15,
16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
64, 79,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97,
240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111,
124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
215,216,217,226,227,228,229,230,231,232,233, 74,224, 90, 95,109,
121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
151,152,153,162,163,164,165,166,167,168,169,192,106,208,161, 7,
32, 33, 34, 35, 36, 21, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27,
48, 49, 26, 51, 52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62,225,
65, 66, 67, 68, 69, 70, 71, 72, 73, 81, 82, 83, 84, 85, 86, 87,
88, 89, 98, 99,100,101,102,103,104,105,112,113,114,115,116,117,
118,119,120,128,138,139,140,141,142,143,144,154,155,156,157,158,
159,160,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
184,185,186,187,188,189,190,191,202,203,204,205,206,207,218,219,
220,221,222,223,234,235,236,237,238,239,250,251,252,253,254,255
};
static unsigned char e2a[256] = {
0, 1, 2, 3,156, 9,134,127,151,141,142, 11, 12, 13, 14, 15,
16, 17, 18, 19,157,133, 8,135, 24, 25,146,143, 28, 29, 30, 31,
128,129,130,131,132, 10, 23, 27,136,137,138,139,140, 5, 6, 7,
144,145, 22,147,148,149,150, 4,152,153,154,155, 20, 21,158, 26,
32,160,161,162,163,164,165,166,167,168, 91, 46, 60, 40, 43, 33,
38,169,170,171,172,173,174,175,176,177, 93, 36, 42, 41, 59, 94,
45, 47,178,179,180,181,182,183,184,185,124, 44, 37, 95, 62, 63,
186,187,188,189,190,191,192,193,194, 96, 58, 35, 64, 39, 61, 34,
195, 97, 98, 99,100,101,102,103,104,105,196,197,198,199,200,201,
202,106,107,108,109,110,111,112,113,114,203,204,205,206,207,208,
209,126,115,116,117,118,119,120,121,122,210,211,212,213,214,215,
216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,
123, 65, 66, 67, 68, 69, 70, 71, 72, 73,232,233,234,235,236,237,
125, 74, 75, 76, 77, 78, 79, 80, 81, 82,238,239,240,241,242,243,
92,159, 83, 84, 85, 86, 87, 88, 89, 90,244,245,246,247,248,249,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57,250,251,252,253,254,255
};
char ASCIItoEBCDIC(const unsigned char c)
{
return a2e[c];
}
char EBCDICtoASCII(const unsigned char c)
{
return e2a[c];
}
int main()
{
char outarr[10];
int i = 0,j=0;
char hexval[10]="c1c2c3c4c5";
char newstrValue;
char hexToAscii(char first, char second)
{
char hex[5], *stop;
hex[0] = '0';
hex[1] = 'x';
hex[2] = first;
hex[3] = second;
hex[4] = 0;
return strtol(hex, &stop, 16);
}
for (i=0;i<10 ;i++)
{
outarr[j]= hexToAscii(hexval[i], hexval[i + 1]);

outarr[j]=ASCIItoEBCDIC(outarr[j]);
printf("ebcdic value is %c\n",outarr);
}

exit(0);
}
//********************************************************************

aftre compiling i get following result.

$>./ebcdictoascii
ebcdic value is :
ebcdic value is :
ebcdic value is :
ebcdic value is :
ebcdic value is :
ebcdic value is :
ebcdic value is :
ebcdic value is :
ebcdic value is :
ebcdic value is :
# 2  
Old 11-24-2008
I was even surprised your code compiled.

Here it is with several bugs fixed:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
static unsigned char a2e[256] = {
0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 37, 11, 12, 13, 14, 15,
16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
64, 79,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97,
240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111,
124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
215,216,217,226,227,228,229,230,231,232,233, 74,224, 90, 95,109,
121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
151,152,153,162,163,164,165,166,167,168,169,192,106,208,161, 7,
32, 33, 34, 35, 36, 21, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27,
48, 49, 26, 51, 52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62,225,
65, 66, 67, 68, 69, 70, 71, 72, 73, 81, 82, 83, 84, 85, 86, 87,
88, 89, 98, 99,100,101,102,103,104,105,112,113,114,115,116,117,
118,119,120,128,138,139,140,141,142,143,144,154,155,156,157,158,
159,160,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
184,185,186,187,188,189,190,191,202,203,204,205,206,207,218,219,
220,221,222,223,234,235,236,237,238,239,250,251,252,253,254,255
};
static unsigned char e2a[256] = {
0, 1, 2, 3,156, 9,134,127,151,141,142, 11, 12, 13, 14, 15,
16, 17, 18, 19,157,133, 8,135, 24, 25,146,143, 28, 29, 30, 31,
128,129,130,131,132, 10, 23, 27,136,137,138,139,140, 5, 6, 7,
144,145, 22,147,148,149,150, 4,152,153,154,155, 20, 21,158, 26,
32,160,161,162,163,164,165,166,167,168, 91, 46, 60, 40, 43, 33,
38,169,170,171,172,173,174,175,176,177, 93, 36, 42, 41, 59, 94,
45, 47,178,179,180,181,182,183,184,185,124, 44, 37, 95, 62, 63,
186,187,188,189,190,191,192,193,194, 96, 58, 35, 64, 39, 61, 34,
195, 97, 98, 99,100,101,102,103,104,105,196,197,198,199,200,201,
202,106,107,108,109,110,111,112,113,114,203,204,205,206,207,208,
209,126,115,116,117,118,119,120,121,122,210,211,212,213,214,215,
216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,
123, 65, 66, 67, 68, 69, 70, 71, 72, 73,232,233,234,235,236,237,
125, 74, 75, 76, 77, 78, 79, 80, 81, 82,238,239,240,241,242,243,
92,159, 83, 84, 85, 86, 87, 88, 89, 90,244,245,246,247,248,249,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57,250,251,252,253,254,255
};

unsigned char ASCIItoEBCDIC(const unsigned char c)
{
  return a2e[c];
}
unsigned char EBCDICtoASCII(const unsigned char c)
{
  return e2a[c];
}
unsigned char hexToAscii(char first, char second)
{
  char hex[5], *stop;
  hex[0]='0';
  hex[1]='x';
  hex[2]=first;
  hex[3]=second;
  hex[4]=0;
  return (unsigned char)strtol(hex, &stop, 16);
}
int main()
{
  unsigned char outarr[10];
  int i=0,j=0;
  char hexval[10]="4041424344";
  for (i=0; i<10 ;i+=2)
  {
    outarr[j]= hexToAscii(hexval[i], hexval[i + 1]);
    printf("ascii value is %d %c\n",outarr[j], outarr[j]);
    outarr[j]=ASCIItoEBCDIC(outarr[j]);
    printf("ebcdic value is %d\n", outarr[j]);
  }
  exit(0);
}

# 3  
Old 11-25-2008
for char hexval[10]="c1c2c3c4c5";
i am expecting EBCDIC values as:
A
B
C
D
E
but i am getting
ascii value is 193 Á
ebcdic value is 119
ascii value is 194 Â
ebcdic value is 120
ascii value is 195 Ã
ebcdic value is 128
ascii value is 196 Ä
ebcdic value is 138
ascii value is 197 Å
ebcdic value is 139



how can i get the values as A B C D E.......
# 4  
Old 11-25-2008
Re: to jlliagre

I was even surprised your code compiled.

Here it is with several bugs fixed:
.
.
.
.
thanks for your attention
But for
char hexval[10]="c1c2c3c4c5";
i am expecting EBCDIC values as:
A
B
C
D
E
but i am getting
ascii value is 193 Á
ebcdic value is 119
ascii value is 194 Â
ebcdic value is 120
ascii value is 195 Ã
ebcdic value is 128
ascii value is 196 Ä
ebcdic value is 138
ascii value is 197 Å
ebcdic value is 139



how can i get the values as A B C D E.......
# 5  
Old 11-25-2008
Why are you expecting 0xC1 to display A ?
0xC1 in "ASCII" (actually ISO-LATIN1) is Á.
# 6  
Old 11-25-2008
Re: to jlliagre

Why are you expecting 0xC1 to display A ?
0xC1 in "ASCII" (actually ISO-LATIN1) is Á.
*********************************
the ebcdic equivalent of 0xc1 is A
and i have to print EBCDIC equivalent Not Ascii equivalent


i changed following array
static unsigned char a2e[256] = {
0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 37, 11, 12, 13, 14, 15,16,
17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,64,
79,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97,30,31,
32,243,244,245,246,247,248,249,122, 94, 76,126,110,111,32,193,194,
195,196,197,198,199,200,201,209,46,60,40,124,214,38,216,217,226,
227,228,229,230,231,232,33, 36,42, 41, 59,109,45,47,130,131,132,133,
134,135,136,137,145,44,37,95,62,63,151,152,153,162,163,164,165,166,
167,168,58,35,64,39,61, 34,32, 97, 98, 99, 100, 101, 102, 103, 104, 105, 1,
43, 44, 9, 10, 27,48, 106, 107, 108, 109, 110, 111, 112, 113, 114, 58, 59,
4, 20, 62,225,65, 66, 115, 116, 117, 118, 119, 120, 121, 122, 82, 83, 84, 85,
86, 87,88, 89, 98, 99,100,101,102,103,104,96,112,113,114,115,116,117,118,65,66,
67,68,69,70,71,72,73,144,154,155,156,157,158,159,74,75,76,77,78,79,80,81,82,178,
179,180,181,182,183,184,185,83,84,85,86,87,88,89,90,204,205,206,207,218,219,30,
31,32,33,34,35,36,37,38,39,250,251,252,253,254,255
};


and printed outarr as character
i am getting correct values for alphabets
but not for numerals
i have used following table

Dec Hex ASCII EBCDIC Dec Hex ASCII EBCDIC
---------------------- -----------------------
0 0 NUL NUL 32 20 DS
1 1 SOH SOH 33 21 ! SOS
2 2 STX STX 34 22 " FS
3 3 ETX ETX 35 23 # WUS
4 4 EOT SEL 36 24 $ BYP/INP
5 5 ENQ HT 37 25 % LF
6 6 ACK RNL 38 26 & ETB
7 7 BEL DEL 39 27 ' ESC
8 8 BS GE 40 28 ( SA
9 9 TAB SPS 41 29 ) SFE
10 A LF RPT 42 2A * SM/SW
11 B VT VT 43 2B + CSP
12 C FF FF 44 2C , MFA
13 D CR CR 45 2D - ENQ
14 E SO SO 46 2E . ACK
15 F SI SI 47 2F / BEL
16 10 DLE DLE 48 30 0
17 11 DC1 DC1 49 31 1
18 12 DC2 DC2 50 32 2 SYN
19 13 DC3 DC3 51 33 3 IR
20 14 DC4 RES/ENP 52 34 4 PP
21 15 NAK NL 53 35 5 TRN
22 16 SYN BS 54 36 6 NBS
23 17 ETB POC 55 37 7 EOT
24 18 CAN CAN 56 38 8 SBS
25 19 EM EM 57 39 9 IT
26 1A SUB UBS 58 3A : RFF
27 1B ESC CU1 59 3B ; CU3
28 1C FS IFS 60 3C < DC4
29 1D GS IGS 61 3D = NAK
30 1E RS IRS 62 3E >
31 1F US ITB/IUS 63 3F ? SUB

Dec Hex ASCII EBCDIC Dec Hex ASCII EBCDIC
------------------------ ------------------------
64 40 @ SP 96 60 ` _
65 41 A RSP 97 61 a /
66 42 B 98 62 b
67 43 C 99 63 c
68 44 D 100 64 d
69 45 E 101 65 e
70 46 F 102 66 f
71 47 G 103 67 g
72 48 H 104 68 h
73 49 I 105 69 i
74 4A J 106 6A j |
75 4B K . 107 6B k ,
76 4C L < 108 6C l %
77 4D M ( 109 6D m _
78 4E N + 110 6E n >
79 4F O | 111 6F o ?
80 50 P & 112 70 p
81 51 Q 113 71 q
82 52 R 114 72 r
83 53 S 115 73 s
84 54 T 116 74 t
85 55 U 117 75 u
86 56 V 118 76 v
87 57 W 119 77 w
88 58 X 120 78 x
89 59 Y 121 79 y `
90 5A Z ! 122 7A z :
91 5B [ $ 123 7B { #
92 5C \ * 124 7C | @
93 5D ] ) 125 7D } '
94 5E ^ ; 126 7E ~ =
95 5F _ 127 7F DEL "

Dec Hex ASCII EBCDIC Dec Hex ASCII EBCDIC
------------------------ ------------------------
128 80 160 A0
129 81 a 161 A1
130 82 b 162 A2 s
131 83 c 163 A3 t
132 84 d 164 A4 u
133 85 e 165 A5 v
134 86 f 166 A6 w
135 87 g 167 A7 x
136 88 h 168 A8 y
137 89 i 169 A9 z
138 8A 170 AA
139 8B { 171 AB
140 8C 172 AC
141 8D 173 AD [
142 8E 174 AE
143 8F + 175 AF
144 90 176 B0
145 91 j 177 B1
146 92 k 178 B2
147 93 l 179 B3
148 94 m 180 B4
149 95 n 181 B5
150 96 o 182 B6
151 97 p 183 B7
152 98 q 184 B8
153 99 r 185 B9
154 9A 186 BA
155 9B } 187 BB
156 9C 188 BC
157 9D 189 BD
158 9E 190 BE
159 9F 191 BF

Dec Hex ASCII EBCDIC Dec Hex ASCII EBCDIC
------------------------ ------------------------
192 C0 224 E0
193 C1 A 225 E1
194 C2 B 226 E2 S
195 C3 C 227 E3 T
196 C4 D 228 E4 U
197 C5 E 229 E5 V
198 C6 F 230 E6 W
199 C7 G 231 E7 X
200 C8 H 232 E8 Y
201 C9 I 233 E9 Z
202 CA 234 EA
203 CB 235 EB
204 CC 236 EC
205 CD 237 ED
206 CE 238 EE
207 CF 239 EF
208 D0 240 F0 0
209 D1 J 241 F1 1
210 D2 K 242 F2 2
211 D3 L 243 F3 3
212 D4 M 244 F4 4
213 D5 N 245 F5 5
214 D6 O 246 F6 6
215 D7 P 247 F7 7
216 D8 Q 248 F8 8
217 D9 R 249 F9 9
218 DA 250 FA
219 DB 251 FB
220 DC 252 FC
221 DD 253 FD
222 DE 254 FE
223 DF 255 FF
For 0XC1 EBCDIC Value is A
hence at position 193 of array i have put value = 65
as 0X65 in ascii is A


it works for alphabets

Last edited by junaid.nehvi; 11-25-2008 at 10:02 AM..
# 7  
Old 11-25-2008
Your code is using 0xC1 as an ASCII value, not an EBCDIC one.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to convert dec to hex in python?

When I try to convert big numbers I get extra numbers at the end that doesn't move plus an L character too. How to remove the 4 extra characters at the end 000L? 8b8dbbc584d9c000L 8b8dc4ddd34c6000L 8b8dcdf621bf0000L 8b8dd70e7031a000L 8b8de026bea44000L #!/usr/bin/python ... (9 Replies)
Discussion started by: bigvito19
9 Replies

2. Shell Programming and Scripting

Convert hex to decimal

can someone help me in converting hex streams to decimal values using perl script Hex value: $my_hex_stream="0c07ac14001676"; Every hex value in the above stream should be converted in to decimal and separated by comma. The output should be: 12,07,172,20,00,22,118 (2 Replies)
Discussion started by: Arun_Linux
2 Replies

3. Shell Programming and Scripting

Convert to Hex in perl

Hi, i want to convert number 5860533159 to hexadecimal. i need to use perl. i used $foo = 5860533159; $hexval3 = sprintf("%#x", $foo); i am getting value as 0xffffffff. i need to get value as 0x15D50A3A7. when i converted using google calculator, i got the correct value, expected... (9 Replies)
Discussion started by: asak
9 Replies

4. Shell Programming and Scripting

Convert Hex - KSH

Hello, I woild like to convert hex on KSH not BASH: I tried to use: tmp=31 printf "\x"${tmp}"" it works on bash - Output is '1' but not on ksh. please advice on the right syntax. Thanks. (4 Replies)
Discussion started by: LiorAmitai
4 Replies

5. Shell Programming and Scripting

Convert hex to decimal or reverse is better?

Please Help Me! about the problem down under. I have 2 files with nearly the same characteristics, I have to convert one to the other format or the other format to one's format. I want to write it with awk. The first file contain lines like this: 300000001#A#Y#Y#Y#Y The other file contain... (4 Replies)
Discussion started by: Axel82
4 Replies

6. Shell Programming and Scripting

How to convert hex numbers to decimal ?

Hi, please tell me how to convert hex number to decimal 000000E7 000000000002640D 0000000000025B16 and seconds to minutes, hours, days, months, years bytes to kbytes, mbytes , gbytes read the following examples while read a b do printf "%5d %5d\n" "0x$a" "0x$b" done < "$FILE"... (15 Replies)
Discussion started by: jack2
15 Replies

7. UNIX for Dummies Questions & Answers

Conversion from EBCDIC to HEX

Hello, Is there any utility around able to deal with the conversion of some EBCDIC coded string into an hexadecimal value? Thanks (1 Reply)
Discussion started by: Indalecio
1 Replies

8. Shell Programming and Scripting

How to Convert Hex value to Dec ?

Hi All, I want to convert below Hex value to Dec value in each column .How to do it ? This data is in a 1 file. 4e20 0475 2710 010f 7530 69a2 7530 7e2f 4e20 02dd 7530 6299 4e20 0c0a 7530 69a2 4e20 0a0b 2710 0048 7530 7955 4e20 0d23 7530 622d 7530 9121 2710 001f 7530 7d3f (6 Replies)
Discussion started by: Nayanajith
6 Replies

9. Programming

to convert int to hex

Hi, Can you help me in converting int value to hex in a single command. Thanks (8 Replies)
Discussion started by: naan
8 Replies

10. UNIX for Dummies Questions & Answers

convert hex to ascii text

Is there a command to convert hex characters into their respective ascii values? (5 Replies)
Discussion started by: dangral
5 Replies
Login or Register to Ask a Question