Questions tagged [ord]

ord() is a function in Python and PHP, which returns an ASCII code point (integer) of a given character (string of length one).

148 questions
40
votes
1 answer

How do I implement Ord for a struct?

I've seen a question similar to this one, but no one that tells me exactly how to implement Ord for a struct. For example, the following: struct SomeNum { name: String, value: u32, } impl Ord for SomeNum { fn cmp(&self, other:&Self) ->…
Dumbapples
  • 3,879
  • 3
  • 14
  • 10
35
votes
2 answers

What does the name of the ord() function stand for?

The official Python documentation explains ord(c) ord(c): Given a string representing one Unicode character, return an integer representing the Unicode code point of that character. For example, ord('a') returns the integer 97 and ord('€') (Euro…
AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
31
votes
1 answer

Why do I get "TypeError: ord() expected string of length 1, but int found" using `ord` on binary data in 3.x?

I have some code like: import os, struct, time # Create a packet by building it with a dummy checksum first, # then computing and replacing the checksum field. myChecksum = 0 pid = os.getpid() & 0xFFFF header = struct.pack("bbHHh",…
user2977469
  • 311
  • 1
  • 3
  • 3
14
votes
3 answers

Why can't the Ord trait provide default implementations for the required methods from the inherited traits using the cmp function?

I have a newtype and I want to implement Ord: use std::cmp::{Ord, Ordering}; struct MyType(isize); impl Ord for MyType { fn cmp(&self, &other: Self) -> Ordering { let MyType(ref lhs) = *self; let MyType(ref rhs) = *other; …
Kapichu
  • 3,346
  • 4
  • 19
  • 38
13
votes
4 answers

Python - Increment Characters in a String by 1

I've searched on how to do this in python and I can't find an answer. If you have a string: >>> value = 'abc' How would you increment all characters in a string by 1? So the input that I'm looking for is: >>> value = 'bcd' I know I can do it…
brazjul
  • 339
  • 2
  • 5
  • 19
9
votes
2 answers

Python's chr() and ord() in Rust

In Python you can convert an integer into a character and a character into an integer with ord() and chr(): >>>a = "a" >>>b = ord(a) + 1 >>>b = chr(b) I am looking for a way to do the same thing in Rust, but I have found nothing similar yet.
Maxtron
  • 101
  • 4
7
votes
1 answer

Multiple characters in Python ord function

Programming beginner here. (Python 2.7) Is there a work around for using more than a single character for Python's ord function? For example, I have a hex string '\xff\x1a' which I'd like the decimal value for so that I can sum it with other hex…
micuzzo
  • 165
  • 3
  • 12
5
votes
2 answers

How to get unicode code points in perl v5.24?

I want to document the hex unicode code points for strings that are cut and pasted into bash as an argument. ord does not do this; ord seems to only work within ascii bounds. Most of what I've found regarding ord, is at least six years old, or…
5
votes
2 answers

python how to compute a simple checksum as quickly as zlib.adler32

I wish to compute a simple checksum : just adding the values of all bytes. The quickest way I found is: checksum = sum([ord(c) for c in buf]) But for 13 Mb data buf, it takes 4.4 s : too long (in C, it takes 0.5 s) If I use : checksum =…
Eric H.
  • 2,152
  • 4
  • 22
  • 34
5
votes
2 answers

Using ord() to convert letters to ints (Very basic)

Python beginner here. Trying to learn by reading code here and there. Came across this in a program designed to open Excel files in python. This function does a simple job--converts an Excel column letter label ('Z', or 'BB', or 'CCC') to an int…
jonquest89
  • 51
  • 1
  • 1
  • 3
4
votes
1 answer

Python: Performing Modulo on Strings

I have several billion strings in the format word0.word1.word2, and I wish to perform modulo n on those strings so that I can feed each to a database writer for storage. I know I can perform a form a modulo 10 on the first character of the strings…
duhaime
  • 25,611
  • 17
  • 169
  • 224
4
votes
5 answers

Python: get int value from a char string

This is one of those silly questions and I don't really know how to formulate it, so I'll give an example. I got v = chr(0xae) + chr(0xae) where #AEAE is, in decimal, the value of 44718. My question is how I get the integer value of v? I know about…
ov1d1u
  • 958
  • 1
  • 17
  • 38
3
votes
1 answer

Get ord() values from single-character columns in Pandas

With a dataframe like this: >>> df = pd.DataFrame([ ['a', 'b', 'c'], ['d', 'e', 'f'], ['x', 'y', 'z'], ]) We can get the ord() mapping for each character: >>> ordinals = df.apply(lambda x: [ord(c) for c in x]) >>> ordinals 0 1 …
Jivan
  • 21,522
  • 15
  • 80
  • 131
3
votes
1 answer

Why does ord() fail when porting from Python 2 to Python 3?

I am trying to port a Python library called heroprotocol from Python 2 to Python 3. This library is used to parse replay files from an online game called Heroes of the Storm, for the purpose of getting data from the file (i.e. who played against…
J. Klima
  • 385
  • 2
  • 10
3
votes
3 answers

How to get ASCII cyrillic character code in Python?

ord() returns unicode code, and I need ascii. >>> s = "Йог" #cyrillic >>> for char in s: ... print(ord(char)) ... 1049 #unicode 1086 #unicode 1075 #unicode and I need ASCII. How to get it? (below)
Nika_Rika
  • 613
  • 2
  • 6
  • 29
1
2 3
9 10