# Convert byte data to character data

Page 1 of 1

## 1 Replies - 1079 Views - Last Post: 25 August 2013 - 12:02 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=327611&amp;s=d405631e9ffea7c62f528d94da4a5dd4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jcwiekalo

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 24-August 13

# Convert byte data to character data

Posted 24 August 2013 - 11:23 AM

I'm working on solving a set of programming exercises from overthewire.org for fun and programming-related experience. The first problem has the following information to solve it:

1. connect to a server/port
2. receive 4 unsigned integers
3. sum the integers
4. send sum of integers to server
5. receive username/password to login via SSH

```#!/usr/bin/python

"""
overthewire.org vortex lab #1 solution in Python
"""

# imports

import socket

# variables

host = "vortex.labs.overthewire.org"
port = 5842
rcvbytes = 4
size = 1024

# TASK 1 - create socket connection

sckt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sckt.connect((host, port))

# TASK 2 - receive data from server

data = sckt.recv(16)
print("values received:", data)

# convert data to integer values

print("converted values:", list(data))

# TASK 3 - sum all array values

arraytotal = sum(data)
print("total of integers:", arraytotal)

# TASK 4 - send results to server

sckt.send(b'arraytotal')

# TASK 5 - receive username/password from server

data = sckt.recv(size)

# close socket

sckt.close()

# print data
print('username/password:', str(data))
print('username/password:', list(data))

```

results from running code:

D:\myCode>python myvortex2.py

values received: b'\xb00e:'
converted values: [176, 48, 101, 58]
total of integers: 383
username/password: b'\xe2C\x1bO\x8e\xf5\xd8\x04\xc2\xd4\t.'
username/password: [226, 67, 27, 79, 142, 245, 216, 4, 194, 212, 9, 46]

=====
I (think) I've successfully completed tasks 1 - 4 and have received the values associated with TASK 5, but can't convert it to letters/numbers to sign in with it.

Is This A Good Question/Topic? 0

## Replies To: Convert byte data to character data

### #2 sillygod

• New D.I.C Head

Reputation: 0
• Posts: 14
• Joined: 23-January 13

## Re: Convert byte data to character data

Posted 25 August 2013 - 12:02 AM

well... I think first, you should post where the question comes from because I can't figure out what's the problem you encoutner. I spent some time and then find it.
However, I still can't totally understand what it say after read the question. Maybe I need to improve my english...
Whatever, I finally solve the question. here is my code,

```import socket

st = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
st.connect(('vortex.labs.overthewire.org', 5842))
ans = sum([  int.from_bytes(st.recv(4), byteorder='little') for i in range(4)])

st.sendall(ans.to_bytes(ans.bit_length()//8+1, byteorder='little'))
reply = st.recv(1024)

print(reply)

```

environment: python3.2.3

Originally, I think you should use c++ to solve this question because the integer has some different between python and c++. I change my mind after I find those built-in function in python3.2. there is a function can rule the byteorder to read the integer serialized.( you should use little endian according to the question )
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }