Qhimm.com Forums

Miscellaneous Forums => Scripting and Reverse Engineering => Topic started by: -gone away- on 2006-01-09 16:54:34

Title: Converting Data - ah... plz help, you know you want to ;)
Post by: -gone away- on 2006-01-09 16:54:34
Okay im trying to convert data from an array<Byte> ^myData to a variety of different formats in a String (e.g. hex & ascii), and currently the only way ive worked out how to do it is long and tedious and quite frankly im ashamed :weep:  of it.

So does anyone know a short easy way of doing this?
Title: Converting Data - ah... plz help, you know you want to ;)
Post by: dziugo on 2006-01-09 17:21:43
Can you give some example, or paste your code?

dziugo
Title: Converting Data - ah... plz help, you know you want to ;)
Post by: -gone away- on 2006-01-09 17:37:23
I think i might of jumped the gun on this one.. just found an relatively easy way to do it...

Code: [Select]
String ^FileEditor::BinaryToString(array<Byte> ^data)
{
String ^tmpStr;

for (int i=0 ; i < data->GetLength(0) ; i++ )
{
tmpStr = tmpStr + ::Convert::ToChar(data[i]);
}

return tmpStr;
}

All other methods i tried and failed... however there is one hitch, i now need to shift all character ascii values by 20h so its no longer in final fantasys format.
-- edit --
opps.. thats better
Title: Converting Data - ah... plz help, you know you want to ;)
Post by: Alhexx on 2006-01-09 17:59:33
Well, that depends on what the string should look like.
If you're using C/C++, you can use the sprintf function for that:
Code: [Select]
int PrintArrayToString(unsigned char* pArray, int iArrayLength, char* szString, int iStringLength)
{
// Check pointers
if((pArray == NULL) || (szString == NULL))
return -1;

// Check buffer sizes
if(iStringLength < (iArrayLength * 2 + 1))
return -2;

// Print String
for(int i = 0; i < iArrayLength; i++)
sprintf(szString, "%s%X", szString, pArray[i]);

// Okay
return 0;
}


Example:
If your Array is :={222, 173, 192, 222}, then
PrintArrayToString returns "DEADC0DE"  8)
Note: 222 = 0xDE ; 173 = 0xAD ; 192 = 0xC0

However, there is also another way, which is a bit more complicated:

Code: [Select]
int PAS(unsigned char* pArray, int iArrayLength, char* szString, int iStringLength)
{
// Check pointers
if((pArray == NULL) || (szString == NULL))
return -1;

// Check buffer sizes
if(iStringLength < (iArrayLength * 2 + 1))
return -2;

// Temporary variable
int iChar = 0;

// Print String
for(int i = 0; i < iArrayLength; i++)
{
iChar = int((pArray[i] >> 4) & 0xF);
szString[2 * i] = char((iChar < 0xA ? 0x30 : 0x37) + iChar);

iChar = int(pArray[i] & 0xF);
szString[(2 * i) + 1] = char((iChar < 0xA ? 0x30 : 0x37) + iChar);
}

// Add NULLCHAR
szString[(iArrayLength * 2) + 1] = 0;

// Okay
return 0;
}


This function looks a little weird, however, produces the same output as the one above.

Happy Coding! :D

 - Alhexx

 - edit -
Basically, my first function does exactly the same what your code does; I was using pure C while you used objects :)
Title: Converting Data - ah... plz help, you know you want to ;)
Post by: -gone away- on 2006-01-09 18:11:51
Cheers... thanks for the response, just came back to add something.

simply add +32 to data to get the text correct, i accidently did it the otherway around (-32 :o ). i didn't think it would be that easy.
Title: Converting Data - ah... plz help, you know you want to ;)
Post by: dziugo on 2006-01-09 18:16:11
Ok, now I'm confused.

Assuming that I have an array:
array<Byte> ^data = {0x41, 0x43, 0x45};

Should it convert into:
"ACE"
or
"414345"
or
"ace"
or
"616365"
or... or what?

dziugo
Title: Converting Data - ah... plz help, you know you want to ;)
Post by: -gone away- on 2006-01-09 18:22:35
It should convert it to 'ACE'. before it would only convert to 414345 or show up "system.byte[]" for some unknown reason. From binary to Ascii Characters that can be put in a 'S'tring (not 's'tring).

Code: [Select]
String ^FileEditor::BinaryToString(array<Byte> ^data)
{
String ^tmpStr;

for (int i=0 ; i < data->GetLength(0) ; i++ )
{
if (data[i]+32 < 255)
{
tmpStr = tmpStr + ::Convert::ToChar(data[i]+32);
}
}

return tmpStr;
}


The above code.. just added a little bit will convert the names of monsters from a byte array to a string.

---- edit ----

Im making a scene editor .. if you havent guessed. This is for me and skillster to help us mod some stuff that current ones cannot. it will include everything later.
Title: Converting Data - ah... plz help, you know you want to ;)
Post by: Qhimm on 2006-01-10 13:48:12
Um, I hope you don't think that "+32" will properly handle the FF7 text?
Title: Converting Data - ah... plz help, you know you want to ;)
Post by: Cyberman on 2006-01-10 19:45:53
If you are converting FF7 text then you have to come up with a totally different plan. :)

First you SHOULD make an FF7Char type (yes Virginia this is a GOOD idea).  Then you need to make built in conversions to and from WCHAR and type CHAR for it.  This is a must for your own sanity :D

FF7 char type isn't as simple as adding 32 as Qhimm has delicately pointed out.  This is because they aren't all just shifted by 32 characters. Other symbols are encoded so.. you need to account for that. (IE color information symbols).  It's a completely different encoding scheme you need to map to and from. So a FF7Char type just might be a much better choice.

You might want to add a few special type tests for it such as.  FF7Char::iscolor() FF7Char::ischar() FF7Char::isalnum() etc.  

Cyb
Title: Converting Data - ah... plz help, you know you want to ;)
Post by: -gone away- on 2006-01-11 02:26:02
Quote from: Qhimm
Um, I hope you don't think that "+32" will properly handle the FF7 text?


It only needs to be simple, its just to display the names of monsters form the files you read.

I was almost going to do it the way cyberman said, thats what i meant by

Code: [Select]
and currently the only way ive worked out how to do it is long and tedious and quite frankly im ashamed  of it.

but then i thought better of it, maybe later when im out of other things to do. I dont think there would be much use in game for multicolored monster names.. except maybe catuar or bosses.

btw - you really need to change your portrait, every time you respond i think im going to get punched in the head.  :lol:  :lol:  :lol: