I'm trying to send a 28 character string to a remote ip address and port. I've done this successfully in vb.net using the following code snippets:
Dim swon As String = "A55A6B0550000000FFFBDE0030C8"
Dim sendBytes As [Byte]()
sendBytes = Encoding.ASCII.GetBytes(swon)
netStream.Write(sendBytes, 0, sendBytes.Length)
I now have to convert this across to c++ and have the following so far:
char *swon = "A55A6B0550000000FFFBDE0030C8";
array<Byte>^ sendBuffer = gcnew array<Byte>(bufferSize);
sendBuffer = BitConverter::GetBytes( swon );
tcpStream->Write(sendBuffer, 0, sendBuffer->Length);
but am getting stuck at this point. I'm sure I'm missing a simple syntax error but I can't figure it out!
To clarify, I'm not getting an error, but I don't think the string is being converted to bytes correctly as when I convert back, I just get a '01'
Cheers,
Chris
I don't understand why you are not just using the exact same .Net framework classes in your ++/CLI code. eg. System::String for swon, Encoding::ASCII to produce the array of bytes.
Anything you did in VB you can map directly over to C++/CLI without using different classes - that's the easest port for you. When you are in MSDN online, just select the C++ view to get examples of stuff you want to do. Try that on this page, for example: http://msdn.microsoft.com/en-us/library/system.text.encoding.ascii.aspx
Steve is correct that the same logic can be duplicated in C++. But the C++ char* already is ASCII, no conversion is necessary. Just a copy is all that's needed.
const char swon[] = { "A55A6B0550000000FFFBDE0030C8" };
array<Byte>^ sendBuffer = gcnew array<Byte>((sizeof swon) - 1);
pin_ptr<Byte> startBuffer = &sendBuffer[0];
memcpy(startBuffer, swon, sendBuffer->Length);
tcpStream->Write(sendBuffer, 0, sendBuffer->Length);
Related
I am sending JSON object through TcpSocket. I deserialize it after destination receives. Usually first a few objects are sent and deserialized without issue! And then suddenly one comes with an extra curly braket only at the end then run time exception.
Seriosuly, what the hell is this ?
System.Text.Json.JsonException: ''}' is invalid after a single JSON value. Expected end of data. Path: $ | LineNumber: 0 | BytePositionInLine: 32.'
{"Value":3,"Name":"Blood Sugar"}}
while(true)
{
seperateSocketForEachRequest.Receive(byteMessage);
seperateSocketForEachRequest.Send(Encoding.UTF8.GetBytes("FF"));
string stringMessage = Encoding.UTF8.GetString(byteMessage);
stringMessage = stringMessage.Substring(0, stringMessage.IndexOf('\0'));
Object message = JsonSerializer.Deserialize<Object>(stringMessage);
}
////////////////////////////////////////
while (Form.isGenerate)
{
Data newData = dataType.generate(person.generatingParameters);
Thread.Sleep(500);
clientSocket.Send(Encoding.UTF8.GetBytes(JsonSerializer.Serialize<Data>(newData)));
byte[] messageReceivedByte = new Byte[1024];
clientSocket.Receive(messageReceivedByte);
}
I found the issue. It is caused of data transmission. Apparently same buffer is used for writing data received from socket and new data is written over old data. Therefore, when value of data is 2 digit number, no issue, when a data comes with 1 digit, boom.
{"Value":76,"Name":"Blood Sugar"}
{"Value":99,"Name":"Blood Sugar"}
{"Value":76,"Name":"Blood Sugar"}
{"Value":1,"Name":"Blood Sugar"}}
I'm trying to translate a JavaScript application of TOTP to VB.Net: http://blog.tinisles.com/2011/10/google-authenticator-one-time-password-algorithm-in-javascript/
I have encountered a problem during translation of the HMAC-part:
//Javascript:
var hmacObj = new jsSHA("Hello World!", 'HEX');
var hmac = hmacObj.getHMAC("secret", 'HEX', 'SHA-1', "HEX");
This is a codesnippet of my translation in VB.Net
'VB.Net:
Dim hmacObjTest As New HMACSHA1(System.Text.Encoding.UTF8.GetBytes("secret"))
Dim hmacTest As Byte() = hmacObjTest.ComputeHash(System.Text.Encoding.UTF8.GetBytes("Hello World!"))
Dim hmacHexTest As New StringBuilder()
For i As Integer = 0 To hmacTest.Length - 1
hmacHexTest.Append(hmacTest(i).ToString("x2"))
Next i
Dim strTest As String = "HMAC = " & hmacHexTest.ToString()
The problem is that i get different output from the two languages:
Output JS: 5efed98b0787c83f9cb0135ba283c390ca49320e //Tested from jsSha demo: http://caligatio.github.io/jsSHA/
Output VB.Net: 87b0154b8420c0b58869ca103f481e824d8876ea
The outputs are not at all the same like they are in this question: hmacsha1 output hex strings different between vb.net and python
Does anyone know where I might be doing something wrong?
Hashes don't work on strings - they work on the binary representation of the string. Now you use UTF-8 as encoding for the dotnet version, while the JavaScript version is very likely not to use UTF-8 - so you get different binary representations, resulting in different hashes.
Use either webttolkit or the hackish var utfstring = unescape(encodeURIComponent(rawstring)); to convert to UTF-8 before calcualting the hash.
I need to append binary data to file but before this data is an xml header. Whole file wont be proper xml file but it must proper xml header like following:
<EncryptedFileHeader>
<Algorithm>name</Algorithm>
<KeySize>256</KeySize>
<SubblockLength>64</SubblockLength>
<CipherMode>ECB</CipherMode>
<sessionKey>sessionKey</sessionKey>
</EncryptedFileHeader>
*binary data*
The xml header I do with JAXB marshalling easily, and even easier would be to add this binary data in base64 and store in note inside xml. But this is a clue. I have to store it as binary to save this overhead 33% space used by base64.
So the question is how to add this data and of course later read this back (serialize/deserialize) ?
Another question is how to remove from the first line of document?
I tried to use:
marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
but it throws an exception:
javax.xml.bind.PropertyException: name: com.sun.xml.bind.xmlDeclaration value: false
at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(AbstractMarshallerImpl.java:358)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(MarshallerImpl.java:527)
Thanks
Actualy I solved this by serializing xml header with JAXB, then appending binary data (bytearray) to existing file.
Reading from file with buffered reader as follows:
BufferedReader reader = new BufferedReader(new FileReader("filepath"));
String line, results = "";
while ((line = reader.readLine()) != null) {
results += line;
}
reader.close();
String[] splited = results.split("</EncryptedFileHeader>");
splited[0] += "</EncryptedFileHeader>";
String s0 = splited[0];
String s1 = new String(splited[1]);
ByteArrayInputStream bais = new ByteArrayInputStream(s0.getBytes());
Now i got a problem with second splited string s1, which consist data from "byteArrayOutputStream.toByteArray();". Now I have to transfer data from this string to byte array. From:
'��A����g�X���
to something like:
[39, -63, -116, 65, -123, -114, 27, -115, -2, 103, -64, 88, -99, -96, -26, -12]
I tried (on the same machine):
byte[] bytes = s1.getBytes();
but bytes array is different and returns 34 bytes instead 16. I read a lot about encodings but still have no idea.
EDIT:
The problem with different number of bytes was due to the different representation of new line by character and byte streams.
I am trying to convert a PCM 8 bit 8 KHz Mono file to DSP TrueSpeech 1 bit 8 kHz Mono using NAudio, and I get the following error:
A first chance exception of type 'NAudio.MmException' occurred in NAudio.dll
AcmNotPossible calling acmStreamOpen
I understand that there may be an intermediate step that I am missing -- any insight would be appreciated. Here is the code I am using:
WaveFormat outWaveFormat;
outWaveFormat = new TrueSpeechWaveFormat();
Debug.Print("Sample Rate: " + outWaveFormat.SampleRate); //displays "8000"
Debug.Print("Bit Rate: " + outWaveFormat.BitsPerSample); //displays "1"
FileInfo f = new FileInfo(inputFile);
String outputFileName = this.txtDest.Text + #"\" + f.Name;
using (WaveFileReader reader = new WaveFileReader(inputFile))
{
try
{
using (WaveStream convertedStream = new WaveFormatConversionStream (outWaveFormat, reader))
{
WaveFileWriter.CreateWaveFile(outputFileName, convertedStream);
}
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
}
Two reasons this might be happening:
you don't have a TrueSpeech encoder. I don't think newer versions of Windows include TrueSpeech anymore - it is effectively obsolete. You can run the NAudioDemo application to see what ACM codecs are on your machine.
your input format cannot convert to the target format in one step. Are you sure your input is PCM. Also I would expect that the TrueSpeech codec wants 16 bit input not 8 bit.
There is a third reason this can happen, although I don't think it affects TrueSpeech and that is that WaveFileWriter.CreateWaveFile assumes that AverageBytesPerSecond is an exact multiple of BlockAlign, which is not always true.
I need to convert UTF8 string to ISO-8859-1 string using VB.NET.
Any example?
emphasized textI have tried Latin function and not runs. I receive incorrect string.
My case is that I need to send SMS using API.
Now I have this code:
baseurl = "http://www.myweb.com/api/sendsms.php"
client = New WebClient
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
client.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-1")
client.QueryString.Add("user", user)
client.QueryString.Add("password", pass)
client.QueryString.Add("alias", myAlias)
client.QueryString.Add("dest", mobile)
textoSms = Me.mmTexto.Text
textoSms = System.Web.HttpUtility.UrlEncode(textoSms)
client.QueryString.Add("message", textoSms)
data = client.OpenRead(baseurl)
reader = New StreamReader(data)
s = reader.ReadToEnd()
data.Close()
reader.Close()
But not runs...I receive incorrect messages. For example
if I write: mañana returns maa ana
If I write aigüa returns aiga
How about:
Dim converted as Byte() = Encoding.Convert(utf8, Encoding.UTF8, _
Encoding.GetEncoding(28591))
That assumes that when you say "UTF8 string" you mean "binary data which is the UTF-8 representation of some text". If you mean something else, please specify :)
Note that ISO-8859-1 only represents a tiny proportion of full Unicode. IIRC, you'll end up with "?" for any character from the source data which isn't available in ISO-8859-1.
The encoding ISO-8859-1 is more commonly called Latin-1. You can get this encoding by doing the following
Dim latin1 = Text.Encoding.GetEncoding(&H6FAF)
The full conversion can be done by the following
Public Function ConvertUtf8ToLatin1(Dim bytes As Byte()) As Bytes()
Dim latin1 = Text.Encoding.GetEncoding(&H6FAF)
Return Encoding.Convert(Encoding.UTF8, latin1, bytes)
End Function
EDIT
As Jon pointed out, it may be easier for people to remember the decimal number 28591 rather than the hex number &H6FAF.
Because System.Text.Encoding.GetEncoding("ISO-8859-1") does not support ñ is my guess, in that case you need to use another encoding type for you SMS.
Please read The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
http://msdn.microsoft.com/en-us/library/system.text.encoding.convert.aspx
Try this with the variable "input" as the UTF-8 String;
VB.NET:
Dim result As Byte() = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("iso-8859-1"), input);
C#:
byte[] result = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("iso-8859-1"), input);
Dont know if this should be posted here but i made a small function in C# to check if a string support the target encoding type.
Hope it can be of any help...
/// <summary>
/// Function for checking if a string can support the target encoding type
/// </summary>
/// <param name="text">The text to check</param>
/// <param name="targetEncoding">The target encoding</param>
/// <returns>True if the encoding supports the string and false if it does not</returns>
public bool SupportsEncoding(string text, Encoding targetEncoding)
{
var btext = Encoding.Unicode.GetBytes(text);
var bencodedtext = Encoding.Convert(Encoding.Unicode, targetEncoding, btext);
var checktext = targetEncoding.GetString(bencodedtext);
return checktext == text;
}
//Call the function demo with ISO-8859-1/Latin-1
if (SupportsEncoding("some text...", Encoding.GetEncoding("ISO-8859-1")))
{
//The encoding is supported
}
else
{
//The encoding is not supported
}