回答
您可以使用Encoding.ASCII
.
GetBytes
到您的字符串轉換爲byte
陣列ASCII編碼(每個字符取一個byte
)。然後,調用BitConverter.ToUInt32
將該字節數組轉換爲uint
。但是,由於@ R. Bemrose在評論中指出,uint
只有4 byte
s,因此您需要先對陣列進行一些分區。
uint如何代表4個字節的字符串? 「ABCD」如何得到代表? – 2010-11-16 21:19:26
@whydna,[7位ASCII表](http://www.neurophys.wisc.edu/comp/docs/ascii/)用於在位表示和字符之間進行轉換。因此,「ABCD」變成了「01000001 01000010 01000011 01000100」,其中十進制是「1094861636」,即「uint」將包含的值。 – jball 2010-11-16 21:23:21
太棒了。所以我只有1個問題。因此,我將字符串轉換爲Ascii字節數組,然後通過BitConvert.ToUint32,它爲字符串「ABCD」返回值「1145258561」。把它轉換成HEX我得到了「44434241」,這真是太棒了!除了一件事 - 爲什麼它倒退呢? (41 = A,42 = B等)。我敢肯定,它與大端的小端序有關 – 2010-11-16 21:38:00
看看Convert.ToUInt32(string, int)
。例如:
uint parsed = Convert.ToUInt32(str, 16);
根據評論,這不是他正在尋找的答案... – 2010-11-16 21:16:01
是的,因爲他中途改變了這個問題。 – Femaref 2010-11-16 21:30:35
uint.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
這將轉換您的string
(用鹼16表示)到uint
。
uint val = Convert.ToUInt32(str, 16);
見我的意見,但如果你想只轉換ASCII字符串爲十六進制,這是我懷疑:
public string HexIt(string yourString)
{
string hex = "";
foreach (char c in yourString)
{
int tmp = c;
hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
}
return hex;
}
現在我想我明白你想要在評論bdukes答案。
如果你想爲字符串中的每個字符使用hex
代碼,你可以使用LINQ獲取它。
var str = "ABCD";
var hex = str.Select(c => ((int)c).ToString("X"))
.Aggregate(String.Empty, (x, y) => x + y);
hex
將是一個字符串41424344
- 1. Qt/C++ - 將字符串時間戳轉換爲uint
- 2. 將-1轉換爲uint C#
- 3. C# - 將uint轉換爲byte []
- 4. 不能轉換UINT *爲uint []
- 5. 將UINT轉換爲WS_STRING
- 6. Golang - 將uint轉換爲os.FileMode
- 7. 將枚舉轉換爲uint
- 8. 將字符串轉換爲long long C?
- 9. 將CFStringRef轉換爲C字符串?
- 10. C++將字符串轉換爲uint64_t
- 11. C++將char轉換爲字符串
- 12. 將字符串轉換爲DateTime C#ASP.NET
- 13. C:將int轉換爲字符串
- 14. 將字符串轉換爲int c
- 15. 將字符串轉換爲整數C#
- 16. C#:將字符串轉換爲DBType.AnsiStringFixedLength
- 17. c#將字符串轉換爲單個
- 18. C#:將字符串轉換爲Sbyte *
- 19. 將字符串轉換爲system.drawing.point c#
- 20. C#,將字符串轉換爲DateTimeOffset
- 21. 將字符串轉換爲smtpclient在c#
- 22. 將C#貨幣轉換爲字符串
- 23. 將字符串轉換爲Date - C#
- 24. 將字符串^轉換爲wstring C++
- 25. 將JSON字符串轉換爲C#JsonObject
- 26. 將字符串轉換爲RemoteEndPoint C#sendto
- 27. 將字符串轉換爲日期C++
- 28. 將double轉換爲字符串C++?
- 29. C++將字符串轉換爲int
- 30. 將字符串轉換爲字符串
注意:這原始的字符串是不是十六進制 – 2010-11-16 21:06:02
你是什麼意思是不是十六進制ASCII ...表示是一個字符串,順便說一句.. – 2010-11-16 21:06:55
見我的編輯新字符串 – 2010-11-16 21:07:27