-1

I am writing c++ code to convert ebcdic to ascii
My main() is shown below

int main()
{
   char text[100];
   int position;
   int count;

   printf("Enter some text\n");
   cin >> text;

   char substring[] = "\\x";
   if(strlen(text)  2 != 0)
   {
      cout << "Length of the string is not even" << endl;
   }
   else
   {
      position = 1;
      int len_string;
      len_string = strlen(text)/2;
      cout<<"len_string"<<len_string<<endl;

      for (count = 0; count < len_string;count++)
      {
         insert_substring(text, substring, position);
     printf("text is s\n",text);
     position  = position + 4;
      }
   }

   ebcdicToAscii((unsigned char*)text);
   cout << "Converted text" <<text << endl;

   char str[]="\xF5\x40\x40\x40\x40\xD4"; //Hardcoded string
   ebcdicToAscii((unsigned char*)str);
   printf ("converted str is s\n", str);

   return 0;
}

Output:

    Enter some text
    F54040404040D4
    len_string7
    text is \xF54040404040D4
    text is \xF5\x4040404040D4
    text is \xF5\x40\x40404040D4
    text is \xF5\x40\x40\x404040D4
    text is \xF5\x40\x40\x40\x4040D4
    text is \xF5\x40\x40\x40\x40\x40D4
    text is \xF5\x40\x40\x40\x40\x40\xD4
    Converted text**?*?*?*?*?*
    converted str is 5    M

Before conversion I need to append \x infront of string

Example:

F540404040D4 must be inserting escape sequence \x

I have written the logic so I got the output:

\xF5\x40\x40/x40\x40\xD4

Now conversion of ebcdic to ascii starts using

ebcdicToAscii((unsigned char*)text);

But I am not getting desired output.

At the same time when I hardcode the string as

\xF5\x40\x40/x40\x40\xD4

the output is as expected

i.e 5 M

I am confused. Please Guide me. I have not shown called functions in code assuming that it is giving proper return.

Jongware
  • 22,200
  • 8
  • 54
  • 100
imniiik
  • 1
  • 2

1 Answers1

1

You shouln't insert \x in inputted string and by the way with or without inserting, that will not work.

Here:

char str[]="\xF5\x40\x40\x40\x40\xD4";

it's just indication, that for example F5 is hexademical number and character with this ascii code should be used (not just symbols F and 5). Look here for more info: What does \x mean in c/c++?

You should construct string from your input, that will store, not just symbols, but use each 2 bytes for ascii code.

For conversion you can for example use following code:

#include <iostream>
#include <string>

int main()
{
   const std::string s ="F540404040D4";
   std::string converted;
   converted.reserve(s.size() / 2);
   for (size_t i = 0; i < s.size(); i += 2)
   {
      const std::string tmp = s.substr(i, 2);
      const int a = std::strtol(tmp.c_str(), 0, 16);
      converted += static_cast<char>(a);
   }
   std::cout << converted.size() << std::endl;
}
Community
  • 1
  • 1
ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • I want append '\x' in between characters forcefully and later have to pass this converted tring to ASCII value example: \xf5 -->5 – imniiik Jul 30 '15 at 11:57
  • without appending \x can I get the desired output: 5 M ?? is it possible?? – imniiik Jul 30 '15 at 12:02
  • @NikhilS. there is no point to add '\x' you will just add two symbols to string. Look at example of conversion. – ForEveR Jul 30 '15 at 12:21
  • @ForEveR: OP is confused between the literal notation in source code as `\xXX` with *user input* of that same sequence. – Jongware Jul 30 '15 at 12:26
  • 1
    @Jongware yeah, I know this, but I explained this in first part of my answer, as I think. – ForEveR Jul 30 '15 at 12:30