Jump to content
IphoneDev

Convert String to Byte Array

Recommended Posts

IphoneDev

Hi,

I wondered if there was some command in autoit like the Convert.ToByte for C#.

Need to output something to a serial device through the COM port already got that working but need to send it as a Byte value.

Thanks!

Edited by IphoneDev

Share this post


Link to post
Share on other sites
Fulano

Something like StringToBinary?

Looked at the C# docs, and Convert.ToByte did not do what I thought it did.

Edited by Fulano

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Share this post


Link to post
Share on other sites
IphoneDev

I don't think thats it :s

This is how I do it in C#

serialPort1.Write(new byte[] { Convert.ToByte(string) }, 0, 1);
Edited by IphoneDev

Share this post


Link to post
Share on other sites
IphoneDev

AutoIt outputs this

0x39332E36373239383231313433363732

This is my AutoIt code:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <CommMG.au3> ;or if you save the commMg.dll in the @scripdir use #include @SciptDir & '\commmg.dll'
#include <String.au3>
$error = ""

$rand = Random(0, 100)
$rand = StringToBinary($rand)
ConsoleWrite($rand & @CR)
_CommSetPort(3, $error, 9600, 8, 0, 1, 0)
_CommSendString($rand, 8)
_CommCloseport()
Edited by IphoneDev

Share this post


Link to post
Share on other sites
Fulano

Because AutoIt is a weakly typed language conversions are usually handled in the background. To get this kind of conversion you may need to try calling Convert.ToByte directly - something like they were doing here with the VB Dictionary object.

I'm short on time right now or I'd test it myself. If the question is still open when I get back to my windows box on Monday then I'll play around with it and see what I can find out.


#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Share this post


Link to post
Share on other sites
jvanegmond

_CommSendString accepts string as input, you don't have to manually convert the string into a byte array.

Your code becomes:

#include <CommMG.au3> ;or if you save the commMg.dll in the @scripdir use #include @SciptDir & '\commmg.dll'

#include <String.au3>

$error = ""

$rand = Random(0, 100)

ConsoleWrite($rand & @CR)

_CommSetPort(3, $error, 9600, 8, 0, 1, 0)

_CommSendString($rand, 8)

_CommCloseport()

See if this works.

Share this post


Link to post
Share on other sites
trancexx

So the title should have been: "Convert String to Byte Array".

Thanks Manadar.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
IphoneDev

Already tried that, it did not work.

Just to assure myself I tried again, still no luck.

Share this post


Link to post
Share on other sites
trancexx

Is there any error/exception/whatever handling in C#?


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
IphoneDev

Yes, atleast when its only running in the IDE.

Still trying to find out how to convert a string to byte array, no luck so far.

Anyone else?

Share this post


Link to post
Share on other sites
trancexx

Yes, atleast when its only running in the IDE.

There is in AutoIt too. Use it. Check errors. I'm sure martin is setting @errors properly.

Still trying to find out how to convert a string to byte array, no luck so far.

Anyone else?

You really make no sense now.

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
Fulano

Correct me if I'm wrong, but looking at the documentation here it looks like you are looking for a simple String->Ascii Array conversion.

If that's the case, there's a function for that.


#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Share this post


Link to post
Share on other sites
tukangusil7

By coincidence, I recently dealt with this issue.

To copy a string to an array bytes, just use a StringToBinary() in the third parameter of DllStructSetData().

#include <StringConstants.au3>

Opt("MustDeclareVars", 1)

Local Const $ciSizeOfWChar = 2

; Copy $sText to an array of bytes, which is the first element of $taiData struct
Local $taiData = DllStructCreate("BYTE[256]")
Local $sText = "Ani text"
DllStructSetData($taiData, 1, StringToBinary($sText, $SB_UTF16LE))

; Replace the third character in the array of bytes, which is "i", to "y",
; thus "Ani text" becomes "Any text".
Local $piData = DllStructGetPtr($taiData, 1)  ; Pointer to the BYTE[0]
Local $tchChar = DllStructCreate("WCHAR", $piData + (3 - 1) * $ciSizeOfWChar)
DllStructSetData($tchChar, 1, "y")

; Display the result
Local $bvResult = DllStructGetData($taiData, 1)  ; We get a binary variant here
Local $sResult = BinaryToString($bvResult, $SB_UTF16LE)  ; Convert the binary variant to string
MsgBox(0, "", $sResult)

Good luck.

Edited by tukangusil7

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.