Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0fd2b647a0ca491ab07adbde717f7d94770608e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# *****************************************************************************
# * Copyright (c) 2015-2016 Wind River Systems, Inc. and others.
# * All rights reserved. This program and the accompanying materials
# * are made available under the terms of the Eclipse Public License v1.0
# * which accompanies this distribution, and is available at
# * http:#www.eclipse.org/legal/epl-v10.html
# *
# * Contributors:
# *     Wind River Systems - initial API and implementation
# *****************************************************************************

import sys
PY3 = sys.version_info[0] == 3


if PY3:
    ints = (int,)
    strings = (str,)
    inttype = int
    longtype = int

    def str2bytes(data):
        if data and isinstance(data[0], str):
            return bytes([ord(c) for c in data])
        elif isinstance(data, bytes):
            return data
        return bytes(data)
else:
    ints = (int, long)  # @UndefinedVariable
    strings = (basestring, str)  # @UndefinedVariable
    inttype = int
    longtype = long  # @UndefinedVariable

    def str2bytes(data):
        if data and isinstance(data[0], str):
            return bytearray([ord(c) for c in data])
        elif isinstance(data, bytearray):
            return data
        return bytearray(data)


def bytes2str(data):
    if data and isinstance(data[0], int):
        return ''.join(chr(b) for b in data)
    return str(data)

Back to the top