Jump to content

Convert String to Byte Array


Recommended Posts

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
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

_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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

  • 8 years later...

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...