Hi,
I have some formats like:
1. T_414_A,T_416_B/T_444_C/XX/YY
2. XX/YY
3. T_412_A/XX/YY/'asas' etc etc
where i have all T_number_alpha formats in an array.
I get a string in any of the formats stated above, which i need to split based on "/" delimiter and check if it matches with the array which i told above if it matches i should write "00"(if it is of type T_number_alpha) to a string and if its XX i should append "10" to the same string then if its YY then i should "11" to the same string and finally i should convert that to hex and write to output text file.
Response at the earliest would me appreciated.
Thanks & Regards,
P.Karthik
Loading
VulpesPosted Oct 13, 2011, 9:20 AM
Here's a better example:
String ^bin_data = "00010110100001111";
int len = bin_data->Length; // 17
int octets = len / 8; // 2
if (len % 8 != 0) octets++; // 3
int i = Convert::ToInt32(bin_data, 2);
String ^hex = "0x" + i.ToString("X" + (2 * octets).ToString());
Console::WriteLine(hex); // 0x002D0F
So an octet which is one byte is represented by 2 hex digits. Here we have 3 octets, so we generate 6 hex digits, padded with leading 0's.
VulpesPosted Oct 13, 2011, 10:34 AM
Karthik AgarwalPosted Oct 13, 2011, 10:14 AM
what are you trying to do in the above line, i don't understand exactly
Karthik AgarwalPosted Oct 13, 2011, 9:01 AM
VulpesPosted Oct 13, 2011, 8:53 AM
String ^bin_data = "10110100001111";
int len = bin_data->Length; // 14
int hexDigits = len / 4; // 3
if (len % 4 != 0) hexDigits++; // 4
int i = Convert::ToInt32(bin_data, 2);
String ^hex = "0x" + i.ToString("X" + hexDigits.ToString()); // EDIT
Console::WriteLine(hex); // 0x2D0F
Karthik AgarwalPosted Oct 13, 2011, 8:37 AM
Just treat that as binary stream that's it.
VulpesPosted Oct 13, 2011, 8:32 AM
The three samples all gave binary strings ending in 1011 which, of course, is 11 decimal or 0xB in hex.
Are you saying that you want to treat each octet of binary digits as a separate hex number?
Karthik AgarwalPosted Oct 13, 2011, 7:42 AM
And one more thing is i have a string which is binary data, I wanna convert the binary to hex but the problem is i have a stream of data as follows:
String ^ bin_data = "10110100001111"
Now i should start converting from the right end i.e i should take last 8bits from the right and convert it to hex and take the other 8bits(if it doesn't contain 8 bits append 0's to left of that) and convert that also into hex.
Note: i don't know how many bits i can get. may get 26 or 58 0r any number but should take 8bits starting from the right
VulpesPosted Oct 13, 2011, 7:14 AM
#include "stdafx.h"
using namespace System;
using namespace System::Text;
using namespace System::IO;
int main(array
{
array
array
StreamWriter ^sw = gcnew StreamWriter("karthik.txt"); // or whatever the file's called
StringBuilder ^sb = gcnew StringBuilder();
for each (String ^sample in samples)
{
StringBuilder ^sb = gcnew StringBuilder();
array
for each(String ^item in items)
{
if (item->StartsWith("T_") && Array::IndexOf(formats, item) > -1)
{
sb->Append("00");
}
else if (item == "XX")
{
sb->Append("10");
}
else if (item == "YY")
{
sb->Append("11");
}
}
String ^bin = sb->ToString();
Console::WriteLine(bin);
int i = Convert::ToInt32(bin, 2);
String ^hex = "0x" + i.ToString("X");
sw->WriteLine(hex);
}
sw->Close();
Console::ReadKey();
return 0;
}