Sign in to follow this  
Followers 0
IphoneDev

Convert String to Byte Array

13 posts in this topic

#1 ·  Posted (edited)

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



#2 ·  Posted (edited)

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

#3 ·  Posted (edited)

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

What's something?


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

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

_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

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

Thanks Manadar.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

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

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


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

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

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

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

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
Sign in to follow this  
Followers 0