Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, August 16, 2012 1:28 PM
Hi ,
I will prepend a string with hexadecimal values to an xml message. Below you can find the code. the MessageBody represent the xml string. Them I must build a string like
fix hex value +length message +fix hex value
after that comes the xml string
The first hex value = 0x1F
Second hex value = X’000000000000000000000000000000000000000000000000’
How can I do this ?
StreamReader reader = new StreamReader(inmsg.BodyPart.Data);
string messageBody = reader.ReadToEnd();
int icount = messageBody.Length + 31;
string messagelength = icount.ToString("D6");
.................
.................
byte[] firstString = System.Text.Encoding.UTF8.GetBytes(messageBody);
MemoryStream outStream = new MemoryStream();
outStream.Write(firstString, 0, firstString.Length);
outStream.Seek(0, SeekOrigin.Begin);
inmsg.BodyPart.Data = outStream;
pc.ResourceTracker.AddResource(outStream);
return inmsg;
All replies (6)
Friday, August 17, 2012 3:32 PM ✅Answered
There is no such thing as "hexadecimal zeroes"; 'hexadecimal' is just a representation form of a numerical value. So the number 23 can be represented as '23' (in decimal), or '0x17' (in hexadecimal), or '10111b' (in binary).
What I assume you want to do, is prefix the xml body with a series of bytes with a certain value. The first byte has value 31 (0x1F in hexadecimal), then you have a number of bytes that represent the length of the message, then a series of bytes with value 0, and then the xml body. This is fairly easy if you use a List of bytes:
List byteList = new List();
string fileLength = "002248";
byteList.Add(0x1F);
byteList.AddRange(fileLength.Select(c => (byte)c));
byteList.AddRange(Enumerable.Repeat((byte)0, 24));
// Use AddRange to add the bytes of the message body to the byteList
byte[] result = byteList.ToArray();
Hey, look! This system allows signatures of more than 60 cha
Friday, August 17, 2012 3:48 PM ✅Answered
Well, the thing is you really aren't trying to write hexadecimal digits into the string, you are attempting to write their byte representations to the string.
Also, there is no 'string literal' definition for hex digits that I am aware of in C#.
Try the following:
StringBuilder sb = new StringBuilder();
sb.Append('\u001F');
sb.Append([length]);
for (int index = 0; index < [count of zeros]; index++)
{
sb.Append('\u0000');
}
string result = sb.ToString();
Note the following:
1. StringBuilder is in the System.Text namespace, so you'll either need to be namespace explicit with the StringBuilder declaration or you'll need a using directive for System.Text in your source file.
2. [count of zeros] should be replaced with the number of null characters you wish to encode in the string.
3. [length] should be the length string
The result will be the prefix you are looking for, I think.
Thursday, August 16, 2012 1:58 PM
Hi ,
I will prepend a string with hexadecimal values to an xml message. Below you can find the code. the MessageBody represent the xml string. Them I must build a string like
fix hex value +length message +fix hex value
after that comes the xml string
The first hex value = 0x1F
Second hex value = X’000000000000000000000000000000000000000000000000’
How can I do this ?
StreamReader reader = new StreamReader(inmsg.BodyPart.Data);
string messageBody = reader.ReadToEnd();int icount = messageBody.Length + 31;
string messagelength = icount.ToString("D6");.................
.................
byte[] firstString = System.Text.Encoding.UTF8.GetBytes(messageBody);
MemoryStream outStream = new MemoryStream();
outStream.Write(firstString, 0, firstString.Length);
outStream.Seek(0, SeekOrigin.Begin);
inmsg.BodyPart.Data = outStream;
pc.ResourceTracker.AddResource(outStream);
return inmsg;
you can use normal string variables to append values:
string str1 = "0x1F";
string str2 = "X’000000000000000000000000000000000000000000000000’";
then you can append these string together.
regards
joon
Thursday, August 16, 2012 3:19 PM
Hi Joon,
I do that, but the result in the text file was like this
0x1F002452X’000000000000000000000000000000000000000000000000’
What I want is like this
002448
any suggestions ?
Thursday, August 16, 2012 9:13 PM
When you say the first value is 0x1F, if this is actually the contents of the string, it will display as 0x1F. To encode a character by its ASCII value (which I believe is what you want to do here), you need to use \u001F.
And as to the X'000000...000' part, what are you trying to do here? Write a string of null characters?
Friday, August 17, 2012 9:53 AM
How I use \u001F in the code above ? For the X'000000...000' part I will write hexadecimal zeroes in the string.