Convert Decimal to ASCII

Posted by Dan Snyder on Stack Overflow See other posts from Stack Overflow or by Dan Snyder
Published on 2010-06-14T15:15:09Z Indexed on 2010/06/14 15:22 UTC
Read the original article Hit count: 312

Filed under:
|
|
|
|

I'm having difficulty using reinterpret_cast. Before I show you my code I'll let you know what I'm trying to do.

I'm trying to get a filename from a vector full of data being used by a MIPS I processor I designed. Basically what I do is compile a binary from a test program for my processor, dump all the hex's from the binary into a vector in my c++ program, convert all of those hex's to decimal integers and store them in a DataMemory vector which is the data memory unit for my processor. I also have instruction memory. So When my processor runs a SYSCALL instruction such as "Open File" my C++ operating system emulator receives a pointer to the beginning of the filename in my data memory. So keep in mind that data memory is full of ints, strings, globals, locals, all sorts of stuff. When I'm told where the filename starts I do the following:

Convert the whole decimal integer element that is being pointed to to its ASCII character representation, and then search from left to right to see if the string terminates, if not then just load each character consecutively into a "filename" string. Do this until termination of the string in memory and then store filename in a table. My difficulty is generating filename from my memory.

Here is an example of what I'm trying to do:

C++ Syntax (Toggle Plain Text)

   1.Index Vector     NewVector   ASCII    filename
   2.0     240faef0   128123792   'abc7'   'a'
   3.0     240faef0   128123792   'abc7'   'ab'
   4.0     240faef0   128123792   'abc7'   'abc'
   5.0     240faef0   128123792   'abc7'   'abc7'
   6.1     1234567a   243225      'k2s0'   'abc7k'
   7.1     1234567a   243225      'k2s0'   'abc7k2'
   8.1     1234567a   243225      'k2s0'   'abc7k2s'
   9.            //EXIT LOOP//
  10.1     1234567a   243225      'k2s0'   'abc7k2s'

Index Vector NewVector ASCII filename 0 240faef0 128123792 'abc7' 'a' 0 240faef0 128123792 'abc7' 'ab' 0 240faef0 128123792 'abc7' 'abc' 0 240faef0 128123792 'abc7' 'abc7' 1 1234567a 243225 'k2s0' 'abc7k' 1 1234567a 243225 'k2s0' 'abc7k2' 1 1234567a 243225 'k2s0' 'abc7k2s' //EXIT LOOP// 1 1234567a 243225 'k2s0' 'abc7k2s' Here is the code that I've written so far to get filename (I'm just applying this to element 1000 of my DataMemory vector to test functionality. 1000 is arbitrary.): C++ Syntax (Toggle Plain Text)

   1.int i = 0;
   2.int step = 1000;//top->a0;
   3.string filename;
   4.char *temp = reinterpret_cast<char*>( DataMemory[1000] );//convert to char
   5.cout << "a0:" << top->a0 << endl;//pointer supplied
   6.cout << "Data:" << DataMemory[top->a0] << endl;//my vector at pointed to location
   7.cout << "Data(1000):" << DataMemory[1000] << endl;//the element I'm testing
   8.cout << "Characters:" << &temp << endl;//my temporary char array
   9.
  10.while(&temp[i]!=0)
  11.{
  12.     filename+=temp[i];//add most recent non-terminated character to string
  13.     i++;
  14.     if(i==4)//when 4 chatacters have been added..
  15.     {
  16.          i=0;
  17.          step+=1;//restart loop at the next element in DataMemory
  18.          temp = reinterpret_cast<char*>( DataMemory[step] );
  19.     }
  20. }
  21. cout << "Filename:" << filename << endl;

int i = 0; int step = 1000;//top->a0; string filename; char *temp = reinterpret_cast( DataMemory[1000] );//convert to char cout << "a0:" << top->a0 << endl;//pointer supplied cout << "Data:" << DataMemory[top->a0] << endl;//my vector at pointed to location cout << "Data(1000):" << DataMemory[1000] << endl;//the element I'm testing cout << "Characters:" << &temp << endl;//my temporary char array while(&temp[i]!=0) { filename+=temp[i];//add most recent non-terminated character to string i++; if(i==3)//when 4 chatacters have been added.. { i=0; step+=1;//restart loop at the next element in DataMemory temp = reinterpret_cast( DataMemory[step] ); } } cout << "Filename:" << filename << endl;

So the issue is that when I do the conversion of my decimal element to a char array I assume that 8 hex #'s will give me 4 characters. Why isn't this this case? Here is my output:

C++ Syntax (Toggle Plain Text)

   1.a0:0
   2.Data:0
   3.Data(1000):4428576
   4.Characters:0x7fff5fbff128
   5.Segmentation fault

© Stack Overflow or respective owner

Related posts about c++

Related posts about string