Sponsored Content
Full Discussion: Convert HEX to EBCDIC IN C
Top Forums Programming Convert HEX to EBCDIC IN C Post 302261253 by jlliagre on Monday 24th of November 2008 06:33:00 AM
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);
}

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
INTERCAL::Charset::EBCDIC(3pm)				User Contributed Perl Documentation			    INTERCAL::Charset::EBCDIC(3pm)

NAME
Charset::EBCDIC - allows to use EBCDIC string constants in ASCII programs (and v.v.) SYNOPSIS
use Charset::EBCDIC 'ascii2abcdic'; my $a = ebcdic2ascii "(EBCDIC text)"; DESCRIPTION
Charset::EBCDIC defines functions to convert between a subset of ASCII and a subset of nonstandard EBCDIC (since there isn't such a thing as a standard EBCDIC we defined our own variant which is guaranteed to be incompatible with all versions of EBCDIC used by IBM hardware - however, when we have chosen a code for a character, we have made sure that at least one - but certainly not all - IBM models used that same code, so the choice cannot be criticised). If you really want to know, several variants of EBCDIC are listed in RFC 1345, which is available from the usual sources. Two functions, ebcdic2ascii and ascii2ebcdic are exportable but not exported by default. They do the obvious thing to their first argument and return the transformed string. EBCDIC CHARACTER TABLE
The following are the characters recognised. The ones shown as 2 letter abbreviations cannot be translated to ASCII (except for the control characters, which do have an ASCII equivalent). + 0 1 2 3 4 5 6 7 8 9 a b c d e f Notes 00 OV TA LF CR OV=overstrike 10 TA=tab 20 LF=linefeed 30 CR=carr-return 40 SP CT . < ( + ! SP=space 50 & ] $ * ) ; NO CT=cents 60 - / XO | , % _ > ? NO=not-sign 70 : # @ ' = " XO=XOR(1) 80 a b c d e f g h i 90 j k l m n o p q r { [ a0 ~ s t u v w x y z RE RE=registered b0 ^ PO CO PO=pound c0 A B C D E F G H I CO=copyright d0 J K L M N O P Q R } e0 S T U V W X Y Z f0 0 1 2 3 4 5 6 7 8 9 DE DE=delete(1) The symbol for the INTECAL XOR operator, "V overstrike -". COPYRIGHT
This module is part of CLC-INTERCAL. Copyright (C) 1999, 2000, 2002, 2006, 2007 Claudio Calvelli, all rights reserved See the files README and COPYING in the distribution for information. SEE ALSO
A qualified psychiatrist. perl v5.8.8 2008-03-29 INTERCAL::Charset::EBCDIC(3pm)
All times are GMT -4. The time now is 04:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy