The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to verify weather it is a ebcdic file or not c_chaitanya UNIX for Dummies Questions & Answers 0 04-03-2008 05:10 AM
ascii to ebcdic conversion er_ashu Shell Programming and Scripting 1 02-01-2008 05:43 PM
Display EBCDIC as Characters LouPelagalli AIX 1 08-09-2005 02:07 PM
Comparing EBCDIC files RishiPahuja Shell Programming and Scripting 2 07-08-2005 02:28 AM
Unix to EBCDIC conversion Bab00shka UNIX for Dummies Questions & Answers 2 01-20-2005 12:58 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-24-2008
junaid.nehvi junaid.nehvi is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 19
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 (permalink)  
Old 11-24-2008
jlliagre jlliagre is offline Forum Advisor  
ɹǝsn sıɹɐlosuǝdo
  
 

Join Date: Dec 2007
Location: Paris
Posts: 1,389
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 (permalink)  
Old 11-25-2008
junaid.nehvi junaid.nehvi is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 19
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.......
  #4 (permalink)  
Old 11-25-2008
junaid.nehvi junaid.nehvi is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 19
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 (permalink)  
Old 11-25-2008
jlliagre jlliagre is offline Forum Advisor  
ɹǝsn sıɹɐlosuǝdo
  
 

Join Date: Dec 2007
Location: Paris
Posts: 1,389
Why are you expecting 0xC1 to display A ?
0xC1 in "ASCII" (actually ISO-LATIN1) is Á.
  #6 (permalink)  
Old 11-25-2008
junaid.nehvi junaid.nehvi is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 19
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 (permalink)  
Old 11-25-2008
jlliagre jlliagre is offline Forum Advisor  
ɹǝsn sıɹɐlosuǝdo
  
 

Join Date: Dec 2007
Location: Paris
Posts: 1,389
Your code is using 0xC1 as an ASCII value, not an EBCDIC one.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On




All times are GMT -4. The time now is 11:31 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0