Posts

Post not yet marked as solved
8 Replies
2.5k Views
Hello I have to port a very simple application from Python to Swift, but I can't understand how to convert that function:def crc16(data, bits=8): crc = 0xFFFF for op, code in zip(data[0::2], data[1::2]): crc = crc ^ int(op+code, 16) for bit in range(0, bits): if (crc&0x0001) == 0x0001: crc = ((crc >> 1) ^ 0xA001) else: crc = crc >> 1 msb = crc >> 8 lsb = crc & 0x00FF return '{:X}{:X}'.format(lsb, msb)And the result is shown by that code:print crc16('11010003000C')The code have to calculate the CRC16 of a string.For example, for the string:11 01 00 03 00 0CThe CRC calculated must beCE9F (CE High, 9F Low)And for the string11 01 02 CD 0BThe result of the CRC must be6D 68I'm trying to make some steps, but I am far away from the results....There is a way to write it in swift?
Posted
by FreeWolF.
Last updated
.
Post not yet marked as solved
1 Replies
281 Views
Hello everyone. I'm slowly learning Swift and programming on MacOS (the one for IOS will come maybe later). I would like to develop something, but I need a goal to achieve, because nothing comes to mind on two feet. Could you recommend something not too complicated to develop that is not present on MacOS or is no longer updated? Of course I don't do it for money, I also publish the source without problems here, for the moment I would only like to have some goal to achieve while I learn. Thanks so much!
Posted
by FreeWolF.
Last updated
.
Post not yet marked as solved
3 Replies
505 Views
I am a total noob and I'm learning.I have a question: why I get an error after my application start?This is the code for the timer:Import UIKit var counter = 0 class ViewController: UIViewController { @IBOutlet weak var Label1: UILabel @IBOutlet weak var Switch1: UISwitch! var timer1 = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerAction), userInfo: nil , repeats: true) @objc func timerAction() { counter = counter + 1 Label1.text = String(counter) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } }Thanks a lot in advance!
Posted
by FreeWolF.
Last updated
.
Post not yet marked as solved
1 Replies
3.1k Views
Dear Sirs,I have a question about swift 5. With this release, is coming a library for the async programming?I just want to write a very simple app like this in python (in python I have a third party library called AsyncIO, but maybe something like that could exist for swift):TCP ECHO CLIENT:import asyncio async def tcp_echo_client(message, loop): reader, writer = await asyncio.open_connection('127.0.0.1', 8888, loop=loop) print('Send: %r' % message) writer.write(message.encode()) data = await reader.read(100) print('Received: %r' % data.decode()) print('Close the socket') writer.close() message = 'Hello World!' loop = asyncio.get_event_loop() loop.run_until_complete(tcp_echo_client(message, loop)) loop.close()TCP ECHO SERVER:import asyncio async def handle_echo(reader, writer): data = await reader.read(100) message = data.decode() addr = writer.get_extra_info('peername') print("Received %r from %r" % (message, addr)) print("Send: %r" % message) writer.write(data) await writer.drain() print("Close the client socket") writer.close() loop = asyncio.get_event_loop() coro = asyncio.start_server(handle_echo, '127.0.0.1', 8888, loop=loop) server = loop.run_until_complete(coro) # Serve requests until Ctrl+C is pressed print('Serving on {}'.format(server.sockets[0].getsockname())) try: loop.run_forever() except KeyboardInterrupt: pass # Close the server server.close() loop.run_until_complete(server.wait_closed()) loop.close()There is a way to write something simple like that in swift 5?
Posted
by FreeWolF.
Last updated
.