data-conversion

Convert 4 bytes to int

。_饼干妹妹 提交于 2019-11-26 15:59:03
I'm reading a binary file like this: InputStream in = new FileInputStream( file ); byte[] buffer = new byte[1024]; while( ( in.read(buffer ) > -1 ) { int a = // ??? } What I want to do it to read up to 4 bytes and create a int value from those but, I don't know how to do it. I kind of feel like I have to grab 4 bytes at a time, and perform one "byte" operation ( like >> << >> & FF and stuff like that ) to create the new int What's the idiom for this? EDIT Ooops this turn out to be a bit more complex ( to explain ) What I'm trying to do is, read a file ( may be ascii, binary, it doesn't matter

iOS convert large numbers to smaller format

眉间皱痕 提交于 2019-11-26 15:16:11
问题 How can I convert all numbers that are more than 3 digits down to a 4 digit or less number? This is exactly what I mean: 10345 = 10.3k 10012 = 10k 123546 = 123.5k 4384324 = 4.3m Rounding is not entirely important, but an added plus. I have looked into NSNumberFormatter but have not found the proper solution, and I have yet to find a proper solution here on SO. Any help is greatly appreciated, thanks! 回答1: -(NSString*) suffixNumber:(NSNumber*)number { if (!number) return @""; long long num =

How to convert letters with accents, umlauts, etc to their ASCII counterparts in Perl?

一曲冷凌霜 提交于 2019-11-26 14:23:26
问题 I'm writing a program that works with documents in Perl and a lot of the documents have characters such as ä, ö, ü, é, etc (both capital and lowercase). I'd like to replace them with ASCII counterparts a, o, u, e, etc . How would I do it in Perl? One of the solutions I thought of is to have a hash with keys being the umlaut and accent characters, and the values being ASCII counterparts, but that requires me to have a list of all umlaut and accent characters, which I don't have, and if I built

How do I convert this list of dictionaries to a csv file?

為{幸葍}努か 提交于 2019-11-26 14:07:45
I have a list of dictionaries that looks something like this: toCSV = [{'name':'bob','age':25,'weight':200},{'name':'jim','age':31,'weight':180}] What should I do to convert this to a csv file that looks something like this: name,age,weight bob,25,200 jim,31,180 Matthew Flaschen import csv toCSV = [{'name':'bob','age':25,'weight':200}, {'name':'jim','age':31,'weight':180}] keys = toCSV[0].keys() with open('people.csv', 'wb') as output_file: dict_writer = csv.DictWriter(output_file, keys) dict_writer.writeheader() dict_writer.writerows(toCSV) EDIT: My prior solution doesn't handle the order. As

Convert nested JSON array into separate columns in CSV file

风流意气都作罢 提交于 2019-11-26 04:55:32
问题 I have a JSON file that looks like this: { \"id\": 10011, \"title\": \"Test procedure\", \"slug\": \"slug\", \"url\": \"http://test.test\", \"email\": \"test@test.com\", \"link\": \"http://test.er\", \"subject\": \"testing\", \"level\": 1, \"disciplines\": [ \"discipline_a\", \"discipline_b\", \"discipline_c\" ], \"areas\": [ \"area_a\", \"area_b\" ] }, I was trying to use the following command to convert that into the CSV file: (Get-Content \"PATH_TO\\test.json\" -Raw | ConvertFrom-Json)|

Java: How to convert List to Map

一世执手 提交于 2019-11-26 00:32:52
问题 Recently I have conversation with a colleague about what would be the optimal way to convert List to Map in Java and if there any specific benefits of doing so. I want to know optimal conversion approach and would really appreciate if any one can guide me. Is this good approach: List<Object[]> results; Map<Integer, String> resultsMap = new HashMap<Integer, String>(); for (Object[] o : results) { resultsMap.put((Integer) o[0], (String) o[1]); } 回答1: List<Item> list; Map<Key,Item> map = new

Convert a string to an integer?

倾然丶 夕夏残阳落幕 提交于 2019-11-25 22:54:32
问题 How do I convert a string to an integer in JavaScript? 回答1: The simplest way would be to use the native Number function: var x = Number("1000") If that doesn't work for you, then there are the parseInt , unary plus , parseFloat with floor , and Math.round methods. parseInt: var x = parseInt("1000", 10); // you want to use radix 10 // so you get a decimal number even with a leading 0 and an old browser ([IE8, Firefox 20, Chrome 22 and older][1]) unary plus if your string is already in the form

Java string to date conversion

为君一笑 提交于 2019-11-25 22:11:22
问题 What is the best way to convert a String in the format \'January 2, 2010\' to a Date in Java? Ultimately, I want to break out the month, the day, and the year as integers so that I can use Date date = new Date(); date.setMonth().. date.setYear().. date.setDay().. date.setlong currentTime = date.getTime(); to convert the date into time. 回答1: That's the hard way, and those java.util.Date setter methods have been deprecated since Java 1.1 (1997). Simply format the date using SimpleDateFormat

How can I convert a Unix timestamp to DateTime and vice versa?

喜欢而已 提交于 2019-11-25 21:43:24
问题 There is this example code, but then it starts talking about millisecond / nanosecond problems. The same question is on MSDN, Seconds since the Unix epoch in C# . This is what I\'ve got so far: public Double CreatedEpoch { get { DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime(); TimeSpan span = (this.Created.ToLocalTime() - epoch); return span.TotalSeconds; } set { DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime(); this.Created = epoch.AddSeconds(value); }

Java: How to convert List to Map

白昼怎懂夜的黑 提交于 2019-11-25 18:59:10
Recently I have conversation with a colleague about what would be the optimal way to convert List to Map in Java and if there any specific benefits of doing so. I want to know optimal conversion approach and would really appreciate if any one can guide me. Is this good approach: List<Object[]> results; Map<Integer, String> resultsMap = new HashMap<Integer, String>(); for (Object[] o : results) { resultsMap.put((Integer) o[0], (String) o[1]); } Jim Garrison List<Item> list; Map<Key,Item> map = new HashMap<Key,Item>(); for (Item i : list) map.put(i.getKey(),i); Assuming of course that each Item